static papi_status_t authorized(service_t *svc, int32_t id) { papi_status_t result = PAPI_NOT_AUTHORIZED; /* assume the worst */ char file[32]; REQUEST *r; snprintf(file, sizeof (file), "%d-0", id); if ((r = getrequest(file)) != NULL) { uid_t uid = getuid(); struct passwd *pw = NULL; char *user = "******"; /* assume an intruder */ if ((pw = getpwuid(uid)) != NULL) user = pw->pw_name; /* use the process owner */ if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */ papi_status_t s; s = papiAttributeListGetString(svc->attributes, NULL, "user-name", &user); if (s != PAPI_OK) /* true root/lp are almighty */ result = PAPI_OK; } if ((result != PAPI_OK) && (strcmp(user, r->user) == 0)) result = PAPI_OK; freerequest(r); } else result = PAPI_NOT_FOUND; return (result); }
void* sock_consume(void *arg){ // NOTE: arg will be a pointer to the linked list of available sockets. int newsock = 0; linkedlist* socks = (linkedlist*)(arg); while(still_running){ pthread_mutex_lock(socks->listlock); while((socks->tail)->socket == -1){ pthread_cond_wait(socks->listempty, socks->listlock); } newsock = list_remove(&socks->head); pthread_mutex_unlock(socks->listlock); char* buffer = malloc(sizeof(char)*1024); if(getrequest(newsock,buffer,1024)==0){ char* filename = malloc(sizeof(char)*strlen(buffer)); if(buffer[0] == '/') { strcpy(filename, (const char*)(buffer + 1)); printf("_%s_\n_%s_\n",buffer, filename); } else{ strcpy(filename, (const char*)(buffer)); } char * request = NULL; //What is going to be passed to sendata (either the 200 or 404) struct stat *finfo = malloc(sizeof(struct stat)); if(0 == stat((const char*)filename, finfo)){ int size = (int)(finfo->st_size); //Fill request with the HTTP header. char* header = malloc(sizeof(int)*(strlen(HTTP_200))); sprintf(header, HTTP_200, size); int reqsize = sizeof(int)*strlen(header) + size; request = malloc(reqsize); strcpy(request,(const char*) header); int reqpos = strlen(header); //Fill the rest of request with things from the file. int fdesc = open((const char*) filename, O_SYNC); char* buf = malloc(sizeof(char)*1024); int moredata = read(fdesc, (void*)buf, 1024); for(; moredata>0; moredata = read(fdesc, (void*)buf, 1024)){ strncpy(request+reqpos, (const char*) buf, moredata); reqpos+=moredata; } request[reqpos] = '\0'; senddata(newsock, (const char*) request, reqsize); close(fdesc); free(header); }else{ request = malloc(sizeof(char)*strlen(HTTP_404)); strcpy(request, (const char*)(HTTP_404)); senddata(newsock, (const char*) request, strlen(request)); } //Do the logging of stuff to the file. free(request); free(finfo); free(filename); } free(buffer); } return NULL; }
static int command_sendcontent(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { ngx_buf_t *b; ngx_chain_t out; Tcl_Obj *content; int rc; int len; ngx_http_request_t *r = getrequest(clientData); if (objc != 2) { Tcl_WrongNumArgs(interp, 1, objv, "content"); return TCL_ERROR; } content = objv[1]; b = ngx_calloc_buf(r->pool); if (b == NULL) { return TCL_ERROR; } out.buf = b; out.next = NULL; b->start = (u_char*)Tcl_GetByteArrayFromObj(content, &len); b->pos = b->start; b->end = b->last = b->start + len; b->memory = 1; b->last_buf = 1; b->last_in_chain = 1; rc = ngx_tcl_cleanup_add_Tcl_Obj(r->pool, content); if (rc != TCL_OK) { return rc; } if (!r->header_sent) { r->headers_out.content_length_n = len; if (r->headers_out.status == 0) { r->headers_out.status = 200; } ngx_http_send_header(r); /* TODO: CHECK RETURN */ } rc = ngx_http_output_filter(r, &out); if (rc != NGX_OK) { ngx_tcl_set_error_code(interp, rc); return TCL_ERROR; } printf("ngx_http_output_filter returns %i\n", rc); fflush(stdout); return TCL_OK; }
char * printer_name_from_uri_id(char *uri, int32_t id) { REQUEST *request = NULL; char *result = ""; if (uri != NULL) { if ((result = strrchr(uri, '/')) != NULL) { result += 1; } else result = (char *)uri; if ((strcmp(result, "jobs") == 0) || (strcmp(result, "any") == 0) || (strcmp(result, "all") == 0)) result = ""; } if ((result[0] == NULL) && (id != -1)) { char path[32]; snprintf(path, sizeof (path), "%d-0", id); if ((request = getrequest(path)) != NULL) result = request->destination; } result = strdup(result); if (request != NULL) freerequest(request); return (result); }
void *processthread(void *para){ int clientsocket; char buffer[1024]; int iDataNum = 0; int recvnum = 0; clientsocket = *((int *)para); printf("<<<<<<<<<<<<<<<<<<<<<<<<BEGIN [%d] >>>>>>>>>>>>>>>>>>>>>>>>>\n", clientsocket); struct HttpRequest httprequest; httprequest.content = NULL; httprequest.path = NULL; httprequest.path = (char *)malloc(1024); httprequest.rangeflag = 0; httprequest.rangestart = 0; while(1){ iDataNum = recv(clientsocket, buffer + recvnum, sizeof(buffer)-recvnum-1, 0); if(iDataNum <= 0){ close(clientsocket); pthread_exit(NULL); return 0; } recvnum += iDataNum; buffer[recvnum] = '\0'; if(strstr(buffer, "\r\n\r\n") != NULL || strstr(buffer, "\n\n") != NULL) break; } printf("request: %s\n", buffer); switch(getrequest(buffer, &httprequest)){ case GET_COMMON: processgetcommon(clientsocket, &httprequest); break; case GET_CGI: processgetcgi(clientsocket, &httprequest); break; case POST: processpost(clientsocket, &httprequest); break; case HEAD: processhead(clientsocket, &httprequest); break; default: break; } insertlognode(pfilelog, &httprequest); if(httprequest.path != NULL) free(httprequest.path); if(httprequest.content != NULL) free(httprequest.content); close(clientsocket); printf("<<<<<<<<<<<<<<END [%d] >>>>>>>>>>>>>>>>>\n", clientsocket); pthread_exit(NULL); }
papi_status_t hold_release_job(papi_service_t handle, char *printer, int32_t job_id, int flag) { papi_status_t status; service_t *svc = handle; REQUEST *r = NULL; char *file; char *dest; if ((svc == NULL) || (printer == NULL) || (job_id < 0)) return (PAPI_BAD_ARGUMENT); if ((status = authorized(svc, job_id)) != PAPI_OK) return (status); dest = printer_name_from_uri_id(printer, job_id); status = lpsched_start_change(svc, dest, job_id, &file); if (status != PAPI_OK) return (status); if ((r = getrequest(file)) != NULL) { r->actions &= ~ACT_RESUME; switch (flag) { case 0: r->actions |= ACT_HOLD; break; case 1: r->actions |= ACT_RESUME; break; case 2: r->actions |= ACT_IMMEDIATE; break; } if (putrequest(file, r) < 0) { detailed_error(svc, gettext("failed to write job: %s: %s"), file, strerror(errno)); freerequest(r); return (PAPI_DEVICE_ERROR); } freerequest(r); } else { detailed_error(svc, gettext("failed to read job: %s: %s"), file, strerror(errno)); return (PAPI_DEVICE_ERROR); } status = lpsched_end_change(svc, dest, job_id); return (status); }
struct block downloadfile(CURL *curl_handle, const char *url, FILE *hfile) { const char *mode = strrchr(url, '\\'); findRchr(url, '\\'); if (firstRun) firstRun = false; else curl_easy_reset(curl_handle); if (!mode) return getrequest(curl_handle, url, hfile); if (strcasestr(mode + 1, "post")) return postrequest(curl_handle, url, NULL); else if (strcasestr(mode + 1, "multipart")) { curl_httppost *data = multipartform(url); return postrequest(curl_handle, url, data); } return getrequest(curl_handle, url, hfile); }
int ngx_http_tcl_getv_cmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { ngx_http_request_t *r = getrequest(clientData); ngx_http_variable_value_t *vv; Tcl_Obj *varObj; ngx_str_t varname; int rc; int len; if (objc < 2 || objc > 3) { Tcl_WrongNumArgs(interp, 1, objv, "varname ?default?"); return TCL_ERROR; } varObj = objv[1]; /* TODO: check return */ rc = SetVarFromAny(interp, varObj); if (rc != TCL_OK) { return rc; } Tcl_GetStringFromObj(varObj, &len); varname.len = len; varname.data = (u_char*)varObj->internalRep.ptrAndLongRep.ptr; vv = ngx_http_get_variable(r, &varname, vartype_get_hash(varObj)); if (vv->not_found) { if (objc == 3) { Tcl_SetObjResult(interp, objv[2]); return TCL_OK; } Tcl_ResetResult(interp); Tcl_AppendResult(interp, "variable \"", Tcl_GetString(varObj), "\" doesn't exist", NULL); return TCL_ERROR; } Tcl_SetObjResult(interp, Tcl_NewStringObj((char*)vv->data, vv->len)); return TCL_OK; }
papi_status_t papiJobMove(papi_service_t handle, char *printer, int32_t job_id, char *destination) { papi_status_t result = PAPI_OK; long bits; service_t *svc = handle; char req_id[64]; char *queue; char *user = NULL; if ((svc == NULL) || (printer == NULL) || (job_id < 0) || (destination == NULL)) return (PAPI_BAD_ARGUMENT); queue = printer_name_from_uri_id(printer, job_id); snprintf(req_id, sizeof (req_id), "%s-%d", queue, job_id); free(queue); if (papiAttributeListGetString(svc->attributes, NULL, "user-name", &user) == PAPI_OK) { REQUEST *r = getrequest(req_id); if ((r != NULL) && (r->user != NULL) && (strcmp(r->user, user) != 0)) result = PAPI_NOT_AUTHORIZED; freerequest(r); } if (result == PAPI_OK) { short status = MOK; char *dest = printer_name_from_uri_id(destination, -1); if ((snd_msg(svc, S_MOVE_REQUEST, req_id, dest) < 0) || (rcv_msg(svc, R_MOVE_REQUEST, &status, &bits) < 0)) status = MTRANSMITERR; free(dest); result = lpsched_status_to_papi_status(status); } return (result); }
//worker threads begin here void consumer(struct thread_args *myarg) { struct socket_pool_t *sockets = (struct socket_pool_t *) myarg->sockets; //infinite loop while(1) { pthread_mutex_lock(sockets->socket_pool_lock); //cond wait loop while(get_new_socket(sockets) == -1){ pthread_cond_wait(&sockets->hold,&sockets->socket_pool_lock); } pthread_mutex_unlock(&sockets->socket_pool_lock); //get new socket from socket queue struct socket_node *new_sock = NULL; new_sock->socket = get_new_socket(sockets); //get new request from socket char* filename = calloc(4096,sizeof(char)); int request = getrequest(new_sock->socket, filename, 1024); //bad request if(request != 0) { fprintf(stderr, "Could not process request.\n"); close(new_sock->socket); free(filename); continue; } //Couldn't figure out how to get it to physically retrieve the files, //But this is the section of code where it should go out and find the file, and //Return either 200 or 404 //Write to myargs->weblog, lock the weblog before writing to it close(new_sock->socket); free(filename); } }
static int command_sendheader(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { ngx_http_request_t *r = getrequest(clientData); int rc; if (objc != 1) { Tcl_WrongNumArgs(interp, 1, objv, NULL); return TCL_ERROR; } rc = ngx_http_send_header(r); if (rc != NGX_OK) { ngx_tcl_set_error_code(interp, rc); return TCL_ERROR; } return TCL_OK; }
papi_status_t papiJobCommit(papi_service_t handle, char *printer, int32_t id) { papi_status_t status = PAPI_OK; service_t *svc = handle; REQUEST *r = NULL; char *metadata_file; char *dest; if ((svc == NULL) || (printer == NULL)) return (PAPI_BAD_ARGUMENT); dest = printer_name_from_uri_id(printer, id); /* tell the scheduler that we want to change the job */ status = lpsched_start_change(svc, dest, id, &metadata_file); if (status != PAPI_OK) return (status); if ((r = getrequest(metadata_file)) != NULL) { r->actions &= ~ACT_RESUME; r->actions |= ACT_RESUME; dellist(&r->file_list, DUMMY_FILE); if (putrequest(metadata_file, r) < 0) { detailed_error(svc, gettext("failed to write job: %s: %s"), metadata_file, strerror(errno)); freerequest(r); return (PAPI_DEVICE_ERROR); } } else { detailed_error(svc, gettext("failed to read job: %s: %s"), metadata_file, strerror(errno)); return (PAPI_DEVICE_ERROR); } status = lpsched_end_change(svc, dest, id); freerequest(r); return (status); }
papi_status_t papiJobCancel(papi_service_t handle, char *printer, int32_t job_id) { papi_status_t result = PAPI_OK; service_t *svc = handle; char req_id[64]; char *dest; char *user = NULL; if ((svc == NULL) || (printer == NULL) || (job_id < 0)) return (PAPI_BAD_ARGUMENT); dest = printer_name_from_uri_id(printer, job_id); snprintf(req_id, sizeof (req_id), "%s-%d", dest, job_id); free(dest); if (papiAttributeListGetString(svc->attributes, NULL, "user-name", &user) == PAPI_OK) { REQUEST *r = getrequest(req_id); if ((result = authorized(handle, job_id)) != PAPI_OK) result = PAPI_NOT_AUTHORIZED; if ((r != NULL) && (r->user != NULL) && (strcmp(r->user, user) != 0)) result = PAPI_NOT_AUTHORIZED; freerequest(r); } if (result == PAPI_OK) { short status = MOK; if ((snd_msg(svc, S_CANCEL_REQUEST, req_id) < 0) || (rcv_msg(svc, R_CANCEL_REQUEST, &status) < 0)) status = MTRANSMITERR; result = lpsched_status_to_papi_status(status); } return (result); }
static int response_set_status(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { ngx_http_request_t *r = getrequest(clientData); int status; int rc; if (objc != 2) { Tcl_WrongNumArgs(interp, 1, objv, "status"); return TCL_ERROR; } rc = Tcl_GetIntFromObj(interp, objv[1], &status); if (rc != TCL_OK) { return rc; } r->headers_out.status = status; return TCL_OK; }
void handlerequest(int socket, struct sockaddr_in saddr){ char filename[1024];//like most our buffers, a bit excessive, but we dont want any overflows char *info; int error404 = 0; struct stat statresult; if (getrequest(socket, filename, 1023)){ printf("failure\n"); } //condition tree to get and format the data else { if(filename[0]!='/'){//if filename doesent start w/ a slash if(!(stat(filename, &statresult))){ //if file exist info = twohundred(filename, statresult); } else{ info = fourofour(); error404=1; } } else{//if it does start w/ a slash if(!(stat(&(filename[1]), &statresult))){//if the file exist info = twohundred(&(filename[1]), statresult); } else{ info = fourofour(); error404 = 1; } } senddata(socket, info, strlen(info)+1);//sending the data (remember the null char) //fprintf(stderr,"%d", strlen(info)); logit( saddr, filename, info , error404);//log our work close(socket);//goodbye free(info); } return; }
int main(int argc, char *argv[]) { int c, poll = 0, reqversion = 0, rc = -1; char *cacertfile = NULL, *keyfile = NULL, *challenge = NULL, *savedrequestfile = NULL, *requestfile = NULL, *dn = NULL, *spkacfile = NULL, *endrequest = NULL; scep_t scep; BIO *repbio; char *url = "http://localhost/cgi-bin"; scepmsg_t *msg; unsigned char *checkNonce = NULL; /* initialize what you can */ scepinit(); scep_clear(&scep); /* we are a client */ scep.client = 1; /* parse command line */ while (EOF != (c = getopt(argc, argv, "dc:e:r:s:k:w:pu:2a:q:"))) switch (c) { case 'd': debug++; break; case 'e': endrequest = optarg; break; case 'c': cacertfile = optarg; break; case 's': savedrequestfile = optarg; case 'r': /* the request file will also contain the self */ /* signed certificate */ requestfile = optarg; break; case 'k': keyfile = optarg; break; case 'w': challenge = optarg; break; case 'p': poll = 1; break; case 'q': scep.community = optarg; break; case 'u': url = optarg; break; case '2': reqversion = 1; break; case 'a': spkacfile = optarg; break; } /* stop immediately if request or key is missing */ /* (even in the case of a version 2 proxied request, we need */ /* a request as the carrier of the proxy entities public key) */ if (keyfile == NULL) { BIO_printf(bio_err, "%s:%d: key file is required argument\n", __FILE__, __LINE__); goto err; } if (requestfile == NULL) { BIO_printf(bio_err, "%s:%d: request file is required " "argument\n", __FILE__, __LINE__); goto err; } /* we are preparing the request message */ msg = &scep.request; /* decode the URL */ if (parseurl(&scep, url) < 0) { BIO_printf(bio_err, "%s:%d: cannot parse url\n", __FILE__, __LINE__); goto err; } if (debug) BIO_printf(bio_err, "%s:%d: decoded URL %s|%d|%s\n", __FILE__, __LINE__, scep.h.httphost, scep.h.httpport, scep.h.httppath); /* read the client key and request information */ if (read_clientstuff(&scep, requestfile, keyfile) < 0) { BIO_printf(bio_err, "%s:%d: failed to read client stuff\n", __FILE__, __LINE__); goto err; } /* now we have to decide about the payload we want to have */ /* with our scep request: */ /* - for a version 1 request, this will always be the original */ /* certificate signing request */ /* - for a version 2 request, it will be a payload structure */ switch (reqversion) { case 0: /* for a version 1 client, client pubkey and client req */ /* coincide */ scep.requestorpubkey = scep.clientpubkey; scep.requestorreq = scep.clientreq; if (debug) BIO_printf(bio_err, "%s:%d: end request coincides " "with SCEP client\n", __FILE__, __LINE__); break; case 1: msg->rd.payload = payload_new(); rc = -1; if (spkacfile) { if (debug) BIO_printf(bio_err, "%s:%d: reading spki " "from %s\n", __FILE__, __LINE__, spkacfile); rc = read_requestorstuff(&scep, 1, spkacfile); } else if (endrequest) { if (debug) BIO_printf(bio_err, "%s:%d: reading X509 req " "from %s\n", __FILE__, __LINE__, endrequest); rc = read_requestorstuff(&scep, 0, endrequest); } if (rc < 0) { BIO_printf(bio_err, "%s:%d: could not read end " "request data\n", __FILE__, __LINE__); goto err; } if (debug) BIO_printf(bio_err, "%s:%d: end request read\n", __FILE__, __LINE__); break; } /* set the transaction id value */ scep.transId = key_fingerprint(scep.requestorpubkey); if (debug) BIO_printf(bio_err, "%s:%d: transaction ID is %s\n", __FILE__, __LINE__, scep.transId); /* read the CA certificate file */ if (read_castuff(&scep, cacertfile) < 0) { BIO_printf(bio_err, "%s:%d: read CA certificate info\n", __FILE__, __LINE__); } if (debug) BIO_printf(bio_err, "%s:%d: CA certificate read\n", __FILE__, __LINE__); /* for SPKI requests, there should be exactly one more argument */ /* namely the distinguished name */ if (spkacfile) { if ((argc - optind) != 1) { BIO_printf(bio_err, "%s:%d: DN argument needed\n", __FILE__, __LINE__); goto err; } dn = argv[optind]; if (debug) BIO_printf(bio_err, "%s:%d: DN argument is '%s'\n", __FILE__, __LINE__, dn); /* convert the DN into attributes and add them to the */ /* payload */ if (payload_dn_to_attrs(msg->rd.payload, dn) < 0) { BIO_printf(bio_err, "%s:%d: failed to add DN attrs\n", __FILE__, __LINE__); goto err; } } /* skip creation of a request message when polling */ if (poll) goto pollinginit; /* pack the request as a PKSCReq message, of type PKCSReq */ switch (reqversion) { case 0: msg->messageType = SCEP_MESSAGE_TYPE_PKCSREQ; msg->rd.req = scep.clientreq; break; case 1: /* build a version 2 payload */ if (debug) BIO_printf(bio_err, "%s:%d: building version 2 " "payload\n", __FILE__, __LINE__); if (scep.requestorreq) payload_set_req(msg->rd.payload, scep.requestorreq); if (scep.requestorspki) payload_set_spki(msg->rd.payload, scep.requestorspki); /* set the correct message type */ if (scep.community) { /* compute the authenticator from the original */ /* request and the community */ msg->messageType = SCEP_MESSAGE_TYPE_V2PROXY; } else { msg->messageType = SCEP_MESSAGE_TYPE_V2REQUEST; } break; } /* write the request to the request file, for later perusal */ if (savedrequestfile) { BIO *reqbio; reqbio = BIO_new(BIO_s_file()); BIO_write_filename(reqbio, savedrequestfile); switch (reqversion) { case 0: /* version 1 request has a X509_REQ payload */ PEM_write_bio_X509_REQ(reqbio, msg->rd.req); break; case 1: /* version 2 requests have a "real" payload */ i2d_payload_bio(reqbio, msg->rd.payload); break; } BIO_free(reqbio); } goto common; pollinginit: /* when polling, the request is a GetCertInitial message */ msg->messageType = SCEP_MESSAGE_TYPE_GETCERTINITIAL; /* the contents is the pair issuer and subject */ msg->rd.is = (issuer_and_subject_t *)malloc( sizeof(issuer_and_subject_t)); msg->rd.is->issuer = X509_get_subject_name(scep.cacert); msg->rd.is->subject = NULL; /* when polling we should read the request from request file */ /* (only needed for the distinguished name of the client) */ if (debug) BIO_printf(bio_err, "%s:%d: getting subject X509_NAME\n", __FILE__, __LINE__); switch (reqversion) { case 0: msg->rd.is->subject = X509_REQ_get_subject_name(scep.clientreq); break; case 1: if (scep.requestorreq) msg->rd.is->subject = X509_REQ_get_subject_name(scep.requestorreq); if (scep.requestorspki) { if (debug) BIO_printf(bio_err, "%s:%d: converting DN '%s' " "to X509_NAME\n", __FILE__, __LINE__, dn); msg->rd.is->subject = ldap_to_x509(dn); } break; } if (msg->rd.is->subject == NULL) { BIO_printf(bio_err, "%s:%d: no subject found\n", __FILE__, __LINE__); goto err; } if (debug) BIO_printf(bio_err, "%s:%d: issuer and subject found\n", __FILE__, __LINE__); common: /* create a self signed certificate for use with SCEP */ if (selfsigned(&scep) < 0) { BIO_printf(bio_err, "%s:%d: failed to create self signed " "certificate\n", __FILE__, __LINE__); goto err; } if (debug) BIO_printf(bio_err, "%s:%d: self signed certificate created\n", __FILE__, __LINE__); /* set the senderNonce */ scep.senderNonceLength = 16; scep.senderNonce = (unsigned char *)malloc(scep.senderNonceLength); RAND_bytes(scep.senderNonce, scep.senderNonceLength); if (debug) BIO_printf(bio_err, "%s:%d: senderNonce set\n", __FILE__, __LINE__); checkNonce = scep.senderNonce; /* all messages sent from the client are base 64 encoded */ msg->base64 = 1; /* encode */ if (encode(&scep) < 0) { BIO_printf(bio_err, "%s:%d: encoding the request failed\n", __FILE__, __LINE__); goto err; } if (debug) BIO_printf(bio_err, "%s:%d: encoded bytes: %d\n", __FILE__, __LINE__, scep.request.length); /* send the request to the server, read the reply */ repbio = getrequest(&scep); if (repbio == NULL) { BIO_printf(bio_err, "%s:%d: failed to read correct reply\n", __FILE__, __LINE__); goto err; } /* analyze the reply */ if (decode(&scep, repbio) < 0) { BIO_printf(bio_err, "%s:%d: decoding the reply failed\n", __FILE__, __LINE__); goto err; } /* display some information about the reply */ printf("transaction id: %s\n", scep.transId); printf("PKIstatus: %s\n", (scep.reply.pkiStatus) ? scep.reply.pkiStatus : "(null)"); printf("reply message type: %s\n", scep.reply.messageType); if (scep.reply.failinfo) { printf("failinfo: %s\n", scep.reply.failinfo); } /* make sure we get a CertRep message back */ if (strcmp(scep.reply.messageType, SCEP_MESSAGE_TYPE_CERTREP)) { BIO_printf(bio_err, "%s:%d: only CertRep message acceptable " " in response to PKCSReq/GetCertInitial\n", __FILE__, __LINE__); goto err; } /* check for the Nonces */ if (memcmp(checkNonce, scep.recipientNonce, 16)) { BIO_printf(bio_err, "%s:%d: recipientNonce != sent " "senderNonce\n", __FILE__, __LINE__); goto err; } if (debug) BIO_printf(bio_err, "%s:%d: Nonce check OK\n", __FILE__, __LINE__); if (scep.reply.pkiStatus == NULL) { BIO_printf(bio_err, "no pkiStatus returned\n"); exit(1); } switch (atoi(scep.reply.pkiStatus)) { case PKI_SUCCESS: /* Success */ scep.clientcert = extract_cert(&scep); if (debug) BIO_printf(bio_err, "%s:%d: certificate returned %p\n", __FILE__, __LINE__, scep.clientcert); if (scep.clientcert) { BIO *cb; cb = BIO_new(BIO_s_file()); BIO_set_fp(cb, stdout, BIO_NOCLOSE); PEM_write_bio_X509(cb, scep.clientcert); BIO_free(cb); } exit(EXIT_SUCCESS); break; case PKI_FAILURE: /* Failure */ if (debug) BIO_printf(bio_err, "%s:%d: request failed: %s\n", __FILE__, __LINE__, scep.reply.failinfo); exit(1); break; case PKI_PENDING: /* Pending */ if (debug) BIO_printf(bio_err, "%s:%d: request still pending\n", __FILE__, __LINE__); exit(2); break; } /* error return */ err: ERR_print_errors(bio_err); exit(EXIT_FAILURE); }
void* worker_start(void* info){ while(TRUE){ pthread_mutex_lock(&lock); while(queuecount==0){ pthread_cond_wait(&cond, &lock); if(!still_running) return NULL; } struct node* request = tail; if(queuecount==1){ head = NULL; tail = NULL; }else{ tail = tail->prev; } queuecount--; printf("queuecount: %d\n", queuecount); pthread_mutex_unlock(&lock); char* filename = (char*)malloc(1024); getrequest(request->sock, filename, 1024); printf("filename: %s\n", filename); //ignore leading '/' if(filename[0]=='/') memmove(filename, filename+1, strlen(filename)); printf("attempting to find file: %s\n",filename); struct stat fstats; stat(filename, &fstats); FILE* request_file = fopen(filename, "r"); char* header; char* request_result; if(request_file){ int strsize = fstats.st_size + 128; //make sure we have enough for HTTP_200 + filesize header = (char*)malloc(strsize); header[strsize-1] = '\0'; sprintf(header, HTTP_200, fstats.st_size); fread(header+strlen(header), 1, strsize-1, request_file); fclose(request_file); request_result = "200"; }else{ header = HTTP_404; request_result = "404"; } pthread_mutex_lock(&file_lock); FILE* log = fopen("weblog.txt","a"); if(senddata(request->sock, header, strlen(header))){ time_t now = time(NULL); char* time = ctime(&now); time[strlen(time)-1] = '\0';//move null-terminator up to get rid of trailing '\n' fprintf(log, "%s:%d %s \"GET %s\" %s %ld\n",request->ip,request->port, time, filename, request_result, fstats.st_size); } pthread_mutex_unlock(&file_lock); free(request); free(filename); if(header!=HTTP_404){ free(header); } } }
static int command_sendfile(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { ngx_buf_t *b; ngx_chain_t out; ngx_http_request_t *r = getrequest(clientData); ngx_file_info_t *fi; ngx_fd_t fd; const char *filename; int filename_len; int rc; if (objc != 2) { Tcl_WrongNumArgs(interp, 1, objv, "file"); return TCL_ERROR; } filename = Tcl_GetStringFromObj(objv[1], &filename_len); fd = ngx_open_file(filename, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); if (fd == NGX_INVALID_FILE) { Tcl_SetResult(interp, (char *) Tcl_PosixError(interp), TCL_VOLATILE); return TCL_ERROR; } b = ngx_calloc_buf(r->pool); if (b == NULL) { return TCL_ERROR; } b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); if (b->file == NULL) { return TCL_ERROR; } fi = &b->file->info; ngx_fd_info(fd, fi); if (!r->header_sent) { r->headers_out.content_length_n = ngx_file_size(fi); r->headers_out.last_modified_time = ngx_file_mtime(fi); if (ngx_http_set_etag(r) != NGX_OK) { return TCL_ERROR; } ngx_http_send_header(r); /* TODO: CHECK RETURN */ } r->allow_ranges = 1; b->file_pos = 0; b->file_last = ngx_file_size(fi); b->in_file = 1; b->last_buf = 1; b->last_in_chain = 1; b->file->fd = fd; b->file->name.data = (u_char*)filename; b->file->name.len = filename_len; b->file->log = r->connection->log; out.buf = b; out.next = NULL; rc = ngx_http_output_filter(r, &out); if (rc != NGX_OK) { ngx_tcl_set_error_code(interp, rc); return TCL_ERROR; } printf("ngx_http_output_filter returns %i\n", rc); fflush(stdout); return TCL_OK; }
papi_status_t papiJobStreamAdd(papi_service_t handle, char *printer, int32_t id, papi_stream_t *stream) { papi_status_t status; service_t *svc = handle; job_stream_t *s = NULL; char *metadata_file = NULL; char *dest; char path[MAXPATHLEN]; /* allocate space for the stream */ if ((*stream = s = calloc(1, sizeof (*s))) == NULL) return (PAPI_TEMPORARY_ERROR); dest = printer_name_from_uri_id(printer, id); /* create/open data file (only root or lp can really do this */ snprintf(path, sizeof (path), "/var/spool/lp/temp/%d-XXXXXX", id); if ((s->fd = mkstemp(path)) < 0) { detailed_error(svc, gettext("unable to create sink (%s): %s"), path, strerror(errno)); free(s); return (PAPI_NOT_AUTHORIZED); } /* add data file to job */ status = lpsched_start_change(svc, dest, id, &metadata_file); if (status != PAPI_OK) { close(s->fd); free(s); unlink(path); return (status); } if ((s->request = getrequest(metadata_file)) == NULL) { detailed_error(svc, gettext("unable to load request: %s: %s"), metadata_file, strerror(errno)); close(s->fd); free(s); unlink(path); return (PAPI_NOT_POSSIBLE); } addlist(&(s->request->file_list), path); if (putrequest(metadata_file, s->request) < 0) { detailed_error(svc, gettext("unable to save request: %s: %s"), metadata_file, strerror(errno)); close(s->fd); free(s); unlink(path); return (PAPI_NOT_POSSIBLE); } status = lpsched_end_change(svc, dest, id); if (status != PAPI_OK) return (status); return (PAPI_OK); }
static papi_status_t authorized(service_t *svc, int32_t id) { papi_status_t result = PAPI_NOT_AUTHORIZED; /* assume the worst */ char file[32]; REQUEST *r; snprintf(file, sizeof (file), "%d-0", id); if ((r = getrequest(file)) != NULL) { uid_t uid = getuid(); struct passwd *pw = NULL; char *user = "******"; /* assume an intruder */ if ((pw = getpwuid(uid)) != NULL) user = pw->pw_name; /* use the process owner */ if ((uid == 0) || (uid == 71)) { /* root/lp can forge this */ papi_status_t s; s = papiAttributeListGetString(svc->attributes, NULL, "user-name", &user); if (s != PAPI_OK) /* true root/lp are almighty */ result = PAPI_OK; } if (result != PAPI_OK) { if (strcmp(user, r->user) == 0) result = PAPI_OK; else { /* * user and r->user might contain the * host info also */ char *token1 = strtok(r->user, "@"); char *token2 = strtok(NULL, "@"); char *token3 = strtok(user, "@"); char *token4 = strtok(NULL, "@"); /* * token1 and token3 contain usernames * token2 and token4 contain hostnames */ if ((token1 == NULL) || (token3 == NULL)) result = PAPI_NOT_AUTHORIZED; else if ((token4 != NULL) && (strcmp(token4, "localhost") == 0) && (strcmp(token3, "root") == 0) || (strcmp(token3, "lp") == 0)) { /* * root/lp user on server can * cancel any requset */ result = PAPI_OK; } else if (strcmp(token1, token3) == 0) { /* * usernames are same * compare the hostnames */ if ((token4 != NULL) && (token2 != NULL) && (strcmp(token4, "localhost") == 0)) { /* * Its server machine */ static char host[256]; if (gethostname(host, sizeof (host)) == 0) { if ((host != NULL) && (strcmp(host, token2) == 0)) result = PAPI_OK; } } else if ((token4 != NULL) && (token2 != NULL) && (strcmp(token4, token2) == 0)) { result = PAPI_OK; } else if ((token4 == NULL) && (token2 != NULL)) { /* * When the request is sent from * client to server using ipp * token4 is NULL */ result = PAPI_OK; } } } } freerequest(r); } else result = PAPI_NOT_FOUND; return (result); }
void userd() { int m_socket; #ifdef HAVE_IPV6_SMTH struct sockaddr_in6 sin; #else struct sockaddr_in sin; #endif int sinlen = sizeof(sin); int opt = 1; bzero(&sin, sizeof(sin)); #ifdef HAVE_IPV6_SMTH if ((m_socket = socket(PF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0) #else if ((m_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) #endif { bbslog("3system", "userd:socket %s", strerror(errno)); exit(-1); } setsockopt(m_socket, SOL_SOCKET, SO_REUSEADDR, &opt, 4); memset(&sin, 0, sinlen); #ifdef HAVE_IPV6_SMTH sin.sin6_family = AF_INET6; sin.sin6_port = htons(USERD_PORT); inet_pton(AF_INET6, "::1", &sin.sin6_addr); #else sin.sin_family = AF_INET; sin.sin_port = htons(USERD_PORT); #ifdef HAVE_INET_ATON inet_aton("127.0.0.1", &sin.sin_addr); #elif defined HAVE_INET_PTON inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr); #else /* Is it OK? */ my_inet_aton("127.0.0.1", &sin.sin_addr); #endif #endif /* IPV6 */ if (0 != bind(m_socket, (struct sockaddr *) &sin, sizeof(sin))) { bbslog("3system", "userd:bind %s", strerror(errno)); exit(-1); } if (0 != listen(m_socket, 5)) { bbslog("3system", "userd:listen %s", strerror(errno)); exit(-1); } while (1) { int sock, id; sock = getrequest(m_socket); #ifndef SECONDSITE if (!strcmp(cmd, "NEW")) id = getnewuserid(username); else #endif if (!strcmp(cmd, "SET")) { setuserid2(num, username); id = 0; } else if (!strcmp(cmd, "DEL")) { setuserid2(num, ""); id = 0; } else continue; putrequest(sock, id); } return; }
papi_status_t papiJobModify(papi_service_t handle, char *printer, int32_t job_id, papi_attribute_t **attributes, papi_job_t *job) { papi_status_t status; job_t *j = NULL; service_t *svc = handle; char *file = NULL; char *dest; REQUEST *r = NULL; char lpfile[BUFSIZ]; if ((svc == NULL) || (printer == NULL) || (job_id < 0) || (attributes == NULL)) return (PAPI_BAD_ARGUMENT); if ((*job = j = calloc(1, sizeof (*j))) == NULL) return (PAPI_TEMPORARY_ERROR); dest = printer_name_from_uri_id(printer, job_id); status = lpsched_start_change(svc, dest, job_id, &file); if (status != PAPI_OK) return (status); if ((r = getrequest(file)) != NULL) { job_attributes_to_lpsched_request(handle, r, (papi_attribute_t **)attributes); #ifdef LP_USE_PAPI_ATTR /* * store the job attributes in the PAPI job attribute file * that was created by the origonal job request. We need to * modify the attributes in the file as per the new attributes */ snprintf(lpfile, sizeof (lpfile), "%s%d-%s", "/var/spool/lp/temp/", job_id, LP_PAPIATTRNAME); status = psm_modifyAttrsFile(attributes, lpfile); if (status != PAPI_OK) { detailed_error(svc, "unable to modify the attributes file: %s: %s", lpfile, strerror(errno)); return (PAPI_DEVICE_ERROR); } #endif if (putrequest(file, r) < 0) { detailed_error(svc, gettext("failed to write job: %s: %s"), file, strerror(errno)); freerequest(r); return (PAPI_DEVICE_ERROR); } } else { detailed_error(svc, gettext("failed to read job: %s: %s"), file, strerror(errno)); return (PAPI_DEVICE_ERROR); } status = lpsched_end_change(svc, dest, job_id); lpsched_request_to_job_attributes(r, j); papiAttributeListAddInteger(&j->attributes, PAPI_ATTR_REPLACE, "job-id", job_id); freerequest(r); return (status); }
int ngx_http_tcl_setv_cmd(ClientData clientData, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[]) { Tcl_Obj *varObj; Tcl_Obj *valObj; ngx_http_request_t *r = getrequest(clientData); ngx_http_core_main_conf_t *cmcf; ngx_http_variable_t *v; ngx_http_variable_value_t *vv; int len; int rc; if (objc != 3) { Tcl_WrongNumArgs(interp, 1, objv, "key value"); return TCL_ERROR; } varObj = objv[1]; valObj = objv[2]; rc = SetVarFromAny(interp, varObj); if (rc != TCL_OK) { return rc; } cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module); v = ngx_hash_find(&cmcf->variables_hash, vartype_get_hash(varObj), vartype_get_name(varObj), vartype_get_namelen(varObj)); if (v == NULL) { Tcl_ResetResult(interp); Tcl_AppendResult(interp, "variable \"", Tcl_GetString(varObj), "\" not found", NULL); return TCL_ERROR; } if (v->flags & NGX_HTTP_VAR_CHANGEABLE) { if (v->set_handler != NULL) { vv = ngx_palloc(r->pool, sizeof(ngx_http_variable_value_t)); if (vv == NULL) { return TCL_ERROR; } vv->valid = 1; vv->not_found = 0; vv->no_cacheable = 0; vv->data = (u_char*)Tcl_GetStringFromObj(valObj, &len); vv->len = len; v->set_handler(r, vv, v->data); return TCL_OK; } if (v->flags & NGX_HTTP_VAR_INDEXED) { vv = &r->variables[v->index]; vv->valid = 1; vv->not_found = 0; vv->no_cacheable = 0; vv->data = (u_char*)Tcl_GetStringFromObj(valObj, &len); vv->len = len; return TCL_OK; } } Tcl_ResetResult(interp); Tcl_AppendResult(interp, "variable \"", Tcl_GetString(varObj), "\" is not changeable", NULL); return TCL_ERROR; }
//processes data and updates the log void processandlog(int sock, char * ipadd, int portnum) { struct stat checkfile; int getstat = 0; int fd = 0; int sendfail = 0; int filesize = 0; int sendreturn = 0; char requestedfile [1024] = ""; int fail = 0; int getreq = getrequest(sock, requestedfile, 1024); if (!getreq) { if (requestedfile[0] == '/') { getstat = stat(&(requestedfile[1]), &checkfile); } else { getstat = stat(requestedfile, &checkfile); } if (!getstat) { filesize = checkfile.st_size; int datasize = 63 + intsize(filesize) + filesize; char data [datasize]; sprintf(data, HTTP_200, (int)checkfile.st_size); if (requestedfile[0] == '/') { fd = open(&(requestedfile[1]), O_RDONLY); } else { fd = open(requestedfile, O_RDONLY); } read(fd, &(data[(63 + intsize(filesize))]), checkfile.st_size); data[datasize] = '\0'; sendreturn = senddata(sock, data, datasize); if (sendreturn == -1) { printf("%s", "Data sending failed! So sad"); sendfail = 1; } shutdown(sock, SHUT_RDWR); close(fd); } else { fail = 1; } } else { fail = 1; //getrequest fails means that we won't ever find the file, return file not found } if(fail){ sendreturn = senddata(sock, HTTP_404, strlen(HTTP_404)); if (sendreturn == -1) { printf("%s", "Data sending failed! So sad"); sendfail = 1; } shutdown(sock, SHUT_RDWR); } if(!sendfail) { pthread_mutex_lock(&log_mutex); FILE * log = fopen("weblog.txt", "a"); fwrite(ipadd, strlen(ipadd), 1, log); char portstr[intsize(portnum)]; sprintf(portstr, ":%d ", portnum); fwrite(portstr, strlen(portstr), 1, log); time_t now = time(NULL); char * time = ctime(&now); fwrite(time, strlen(time)-1, 1, log); fwrite(" \"GET " , 6, 1, log); fwrite(requestedfile, strlen(requestedfile), 1, log); if(fail){ fwrite("\" 404 ", 6, 1, log); } else { fwrite("\" 200 ", 6, 1, log); } char buffer[intsize(sendreturn)]; sprintf(buffer, "%d\n", sendreturn); fwrite(buffer, strlen(buffer), 1, log); fclose(log); pthread_mutex_unlock(&log_mutex); } else { //since we want to write to log for every request, it makes sense to keep track of senddata failures pthread_mutex_lock(&log_mutex); FILE * log = fopen("weblog.txt", "a"); fwrite("Senddata failed\n", 16, 1, log); fclose(log); pthread_mutex_unlock(&log_mutex); } }
void *worker(void *v){ // locked for every malloc and free because their thread safety is reliant on library implementation struct node ***sock_list = (struct node ***)v; int bufferlen = 1024; LOCK(&sock_mut); char *reqbuffer = malloc(sizeof(char) * bufferlen); UNLOCK(&sock_mut); struct stat statresult; struct node **head_ptr = sock_list[0]; struct node **tail_ptr = sock_list[1]; while(still_running){ int socket; struct sockaddr_in client_address; LOCK(&sock_mut); //locked over all threads while((*head_ptr==NULL)&&(still_running==TRUE)){ WAIT(&consumer,&sock_mut); } if(still_running==FALSE){ socket = -1; }else{ socket = (*head_ptr)->socket; client_address = (*head_ptr)->client_address; killHead(head_ptr,tail_ptr); } UNLOCK(&sock_mut); //had to copy to here instead of its own function so I can print using client_address time_t t; if(socket!=-1){ //still_running!=FALSE int success = getrequest(socket, reqbuffer, bufferlen); LOCK(&sock_mut); char *final_text = malloc(sizeof(char) * 10000); char *output = malloc(sizeof(char) * 1000); UNLOCK(&sock_mut); if (success == 0) { int bytes; if (stat(reqbuffer + 1, &statresult) >= 0) { // file exists (200) // opening file and reading contents LOCK(&sock_mut); // needs to be locked because otherwise different threads might be rewinding or opening/closing the same file //taken from www.cplusplus.com FILE * pFile; long lSize; char * buffer; size_t result; pFile = fopen (reqbuffer + 1, "rb" ); if (pFile==NULL) { fputs ("File error",stderr); exit (1); } // obtain file size: fseek (pFile , 0 , SEEK_END); lSize = ftell (pFile); rewind (pFile); // allocate memory to contain the whole file: buffer = malloc (sizeof(char) * (lSize + 1)); if (buffer == NULL) {fputs ("Memory error",stderr); exit (2);} // copy the file into the buffer: result = fread (buffer,1,lSize,pFile); if (result != lSize) {fputs ("Reading error",stderr); exit (3);} /* the whole file is now loaded in the memory buffer. */ fclose (pFile); UNLOCK(&sock_mut); sprintf(final_text, HTTP_200, strlen(buffer)); // adding HTTP message strcat(final_text, buffer); // file contents bytes = senddata(socket, final_text, strlen(final_text)); strcpy(output, inet_ntoa(client_address.sin_addr)); // creating the log char *temp = intToString(ntohs(client_address.sin_port)); strcat(output,temp); LOCK(&sock_mut); free(temp); free(buffer); UNLOCK(&sock_mut); // adding date and rest of info to the log time(&t); strcat(output, " "); strcat(output, ctime(&t)); output[strlen(output) - 1] = ' '; strcat(output, "\"GET /"); strcat(output, reqbuffer); strcat(output, "\" 200 "); } else { // file does not exist (404) // sending data bytes = senddata(socket, HTTP_404, strlen(HTTP_404)); strcpy(output, inet_ntoa(client_address.sin_addr)); char *temp = intToString(ntohs(client_address.sin_port)); strcat(output,temp); LOCK(&sock_mut); free(temp); UNLOCK(&sock_mut); // adding date and rest of info to the log time(&t); strcat(output, " "); strcat(output, ctime(&t)); strcat(output, "\"GET /"); strcat(output, reqbuffer); strcat(output, "\" 404 "); } // logging to file logToFile(output, "weblog.txt", bytes); LOCK(&sock_mut); free(final_text); free(output); UNLOCK(&sock_mut); } shutdown(socket, 2); //shutting down the socket at the end of use } } LOCK(&sock_mut); free(reqbuffer); UNLOCK(&sock_mut); return NULL; }