static char *ssl_expr_eval_func_file(request_rec *r, char *filename) { FILE *fp; char *buf; int len; if ((fp = ap_pfopen(r->pool, filename, "r")) == NULL) { ssl_expr_error = "Cannot open file"; return ""; } fseek(fp, 0, SEEK_END); len = ftell(fp); if (len == 0) { buf = (char *)ap_palloc(r->pool, sizeof(char) * 1); *buf = NUL; } else { if ((buf = (char *)ap_palloc(r->pool, sizeof(char) * (len+1))) == NULL) { ssl_expr_error = "Cannot allocate memory"; ap_pfclose(r->pool, fp); return ""; } fseek(fp, 0, SEEK_SET); if (fread(buf, len, 1, fp) == 0) { ssl_expr_error = "Cannot read from file"; fclose(fp); return (""); } buf[len] = NUL; } ap_pfclose(r->pool, fp); return buf; }
char *SSL_make_ciphersuite(pool *p, SSL *ssl) { STACK_OF(SSL_CIPHER) *sk; SSL_CIPHER *c; int i; int l; char *cpCipherSuite; char *cp; if (ssl == NULL) return ""; if ((sk = SSL_get_ciphers(ssl)) == NULL) return ""; l = 0; for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { c = sk_SSL_CIPHER_value(sk, i); l += strlen(c->name)+2+1; } if (l == 0) return ""; cpCipherSuite = (char *)ap_palloc(p, l+1); cp = cpCipherSuite; for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) { c = sk_SSL_CIPHER_value(sk, i); l = strlen(c->name); memcpy(cp, c->name, l); cp += l; *cp++ = '/'; *cp++ = (c->valid == 1 ? '1' : '0'); *cp++ = ':'; } *(cp-1) = NUL; return cpCipherSuite; }
/* record a sucked input chunk */ static void ssl_io_suck_record(request_rec *r, char *buf, int len) { struct ssl_io_suck_st *ss; if ((ss = ap_ctx_get(r->ctx, "ssl::io::suck")) == NULL) return; if (((ss->bufptr + ss->buflen) - (ss->pendptr + ss->pendlen)) < len) { /* "expand" buffer: actually we cannot really expand the buffer here, because Apache's pool system doesn't support expanding chunks of memory. Instead we have to either reuse processed data or allocate a new chunk of memory in advance if we really need more memory. */ int newlen; char *newptr; if (( (ss->pendptr - ss->bufptr) + ((ss->bufptr + ss->buflen) - (ss->pendptr + ss->pendlen)) ) >= len) { /* make memory available by reusing already processed data */ memmove(ss->bufptr, ss->pendptr, ss->pendlen); ss->pendptr = ss->bufptr; } else { /* too bad, we have to allocate a new larger buffer */ newlen = (ss->buflen * 2) + len; newptr = ap_palloc(r->pool, newlen); ss->bufptr = newptr; ss->buflen = newlen; memcpy(ss->bufptr, ss->pendptr, ss->pendlen); ss->pendptr = ss->bufptr; } } memcpy(ss->pendptr+ss->pendlen, buf, len); ss->pendlen += len; return; }
/* prepare request_rec structure for input sucking */ static void ssl_io_suck_start(request_rec *r) { struct ssl_io_suck_st *ss; ss = ap_ctx_get(r->ctx, "ssl::io::suck"); if (ss == NULL) { ss = ap_palloc(r->pool, sizeof(struct ssl_io_suck_st)); ap_ctx_set(r->ctx, "ssl::io::suck", ss); ss->buflen = 8192; ss->bufptr = ap_palloc(r->pool, ss->buflen); } ss->pendptr = ss->bufptr; ss->pendlen = 0; ss->active = FALSE; return; }
static char *get_dbm_pw(request_rec *r, char *user, char *auth_dbmpwfile) { DBM *f; datum d, q; char *pw = NULL; q.dptr = user; #ifndef NETSCAPE_DBM_COMPAT q.dsize = strlen(q.dptr); #else q.dsize = strlen(q.dptr) + 1; #endif if (!(f = dbm_open(auth_dbmpwfile, O_RDONLY, 0664))) { ap_log_error(APLOG_MARK, APLOG_ERR, r->server, "could not open dbm auth file: %s", auth_dbmpwfile); return NULL; } d = dbm_fetch(f, q); if (d.dptr) { pw = ap_palloc(r->pool, d.dsize + 1); strncpy(pw, d.dptr, d.dsize); pw[d.dsize] = '\0'; /* Terminate the string */ } dbm_close(f); return pw; }
API_EXPORT(piped_log *) ap_open_piped_log (pool *p, const char *program) { piped_log *pl; pl = ap_palloc (p, sizeof (*pl)); pl->p = p; pl->program = ap_pstrdup (p, program); pl->pid = -1; ap_block_alarms (); if (pipe (pl->fds) == -1) { int save_errno = errno; ap_unblock_alarms(); errno = save_errno; return NULL; } ap_register_cleanup (p, pl, piped_log_cleanup, piped_log_cleanup_for_exec); if (piped_log_spawn (pl) == -1) { int save_errno = errno; ap_kill_cleanup (p, pl, piped_log_cleanup); close (pl->fds[0]); close (pl->fds[1]); ap_unblock_alarms (); errno = save_errno; return NULL; } ap_unblock_alarms (); return pl; }
const char * fcgi_util_fs_is_path_ok(pool * const p, const char * const fs_path, struct stat *finfo) { const char *err; if (finfo == NULL) { finfo = (struct stat *)ap_palloc(p, sizeof(struct stat)); if (stat(fs_path, finfo) < 0) return ap_psprintf(p, "stat(%s) failed: %s", fs_path, strerror(errno)); } if (finfo->st_mode == 0) return ap_psprintf(p, "script not found or unable to stat()"); if (S_ISDIR(finfo->st_mode)) return ap_psprintf(p, "script is a directory!"); /* Let the wrapper determine what it can and can't execute */ if (! fcgi_wrapper) { #ifdef WIN32 err = fcgi_util_check_access(p, fs_path, finfo, _S_IEXEC, fcgi_user_id, fcgi_group_id); #else err = fcgi_util_check_access(p, fs_path, finfo, X_OK, fcgi_user_id, fcgi_group_id); #endif if (err) { return ap_psprintf(p, "access for server (uid %ld, gid %ld) not allowed: %s", (long)fcgi_user_id, (long)fcgi_group_id, err); } } return NULL; }
twodim* td_create_twodim(ap_pool *pool) { twodim *td; td = (twodim*) ap_palloc(pool, sizeof(twodim) ); td_set(pool, td); return td; }
/* Create a copy of a "struct hostent" record; it was presumably returned * from a call to gethostbyname() and lives in static storage. * By creating a copy we can tuck it away for later use. */ API_EXPORT(struct hostent *) ap_pduphostent(pool *p, const struct hostent *hp) { struct hostent *newent; char **ptrs; char **aliases; struct in_addr *addrs; int i = 0, j = 0; if (hp == NULL) return NULL; /* Count number of alias entries */ if (hp->h_aliases != NULL) for (; hp->h_aliases[j] != NULL; ++j) continue; /* Count number of in_addr entries */ if (hp->h_addr_list != NULL) for (; hp->h_addr_list[i] != NULL; ++i) continue; /* Allocate hostent structure, alias ptrs, addr ptrs, addrs */ newent = (struct hostent *) ap_palloc(p, sizeof(*hp)); aliases = (char **) ap_palloc(p, (j + 1) * sizeof(char *)); ptrs = (char **) ap_palloc(p, (i + 1) * sizeof(char *)); addrs = (struct in_addr *) ap_palloc(p, (i + 1) * sizeof(struct in_addr)); *newent = *hp; newent->h_name = ap_pstrdup(p, hp->h_name); newent->h_aliases = aliases; newent->h_addr_list = (char **) ptrs; /* Copy Alias Names: */ for (j = 0; hp->h_aliases[j] != NULL; ++j) { aliases[j] = ap_pstrdup(p, hp->h_aliases[j]); } aliases[j] = NULL; /* Copy address entries */ for (i = 0; hp->h_addr_list[i] != NULL; ++i) { ptrs[i] = (char *) &addrs[i]; addrs[i] = *(struct in_addr *) hp->h_addr_list[i]; } ptrs[i] = NULL; return newent; }
static char *parse_log_misc_string(pool *p, log_format_item *it, const char **sa) { const char *s; char *d; it->func = constant_item; it->conditions = NULL; s = *sa; while (*s && *s != '%') { s++; } /* * This might allocate a few chars extra if there's a backslash * escape in the format string. */ it->arg = ap_palloc(p, s - *sa + 1); d = it->arg; s = *sa; while (*s && *s != '%') { if (*s != '\\') { *d++ = *s++; } else { s++; switch (*s) { case '\\': *d++ = '\\'; s++; break; case 'n': *d++ = '\n'; s++; break; case 't': *d++ = '\t'; s++; break; default: /* copy verbatim */ *d++ = '\\'; /* * Allow the loop to deal with this *s in the normal * fashion so that it handles end of string etc. * properly. */ break; } } } *d = '\0'; *sa = s; return NULL; }
void td_set(ap_pool *pool, twodim *td) { if (td) { td->r = 1; td->c = 1; td->a = (char**) ap_palloc(pool, sizeof(char*) ); if(td->a) td->a[0]=NULL; } }
static void *make_agent_log_state(pool *p, server_rec *s) { agent_log_state *cls = (agent_log_state *) ap_palloc(p, sizeof(agent_log_state)); cls->fname = ""; cls->agent_fd = -1; return (void *) cls; }
static void *create_imap_dir_config(pool *p, char *dummy) { imap_conf_rec *icr = (imap_conf_rec *) ap_palloc(p, sizeof(imap_conf_rec)); icr->imap_menu = NULL; icr->imap_default = NULL; icr->imap_base = NULL; return icr; }
ssl_ds_array *ssl_ds_array_make(pool *p, int size) { ssl_ds_array *a; if ((a = (ssl_ds_array *)ap_palloc(p, sizeof(ssl_ds_array))) == NULL) return NULL; a->pPool = p; if ((a->pSubPool = ap_make_sub_pool(p)) == NULL) return NULL; a->aData = ap_make_array(a->pSubPool, 2, size); return a; }
static void do_set_header( void *_c, const char *key, const char *value, bool add ) { mcontext *c = (mcontext*)_c; if( add ) ap_table_add(c->r->headers_out,key,value); else if( strcmpi(key,"Content-Type") == 0 ) { int len = (int)strlen(value); char *ct = (char*)ap_palloc(c->r->pool,len+1); memcpy(ct,value,len+1); c->r->content_type = ct; } else ap_table_set(c->r->headers_out,key,value); }
ssl_ds_table *ssl_ds_table_make(pool *p, int size) { ssl_ds_table *t; if ((t = (ssl_ds_table *)ap_palloc(p, sizeof(ssl_ds_table))) == NULL) return NULL; t->pPool = p; if ((t->pSubPool = ap_make_sub_pool(p)) == NULL) return NULL; t->aKey = ap_make_array(t->pSubPool, 2, MAX_STRING_LEN); t->aData = ap_make_array(t->pSubPool, 2, size); return t; }
static BOOL SendResponseHeaderEx(isapi_cid *cid, const char *stat, const char *head, DWORD statlen, DWORD headlen) { int termarg; char *termch; if (!stat || statlen == 0 || !*stat) { stat = "Status: 200 OK"; } else { char *newstat; newstat = ap_palloc(cid->r->pool, statlen + 9); strcpy(newstat, "Status: "); ap_cpystrn(newstat + 8, stat, statlen + 1); stat = newstat; } if (!head || headlen == 0 || !*head) { head = "\r\n"; } else { if (head[headlen]) { /* Whoops... not NULL terminated */ head = ap_pstrndup(cid->r->pool, head, headlen); } } /* Parse them out, or die trying */ cid->status = ap_scan_script_header_err_strs(cid->r, NULL, &termch, &termarg, stat, head, NULL); cid->ecb->dwHttpStatusCode = cid->r->status; /* All the headers should be set now */ ap_send_http_header(cid->r); /* Any data left should now be sent directly, * it may be raw if headlen was provided. */ if (termch && (termarg == 1)) { if (headlen == -1 && *termch) ap_rputs(termch, cid->r); else if (headlen > (size_t) (termch - head)) ap_rwrite(termch, headlen - (termch - head), cid->r); } if (cid->status == HTTP_INTERNAL_SERVER_ERROR) return FALSE; return TRUE; }
static void *make_config_log_state(pool *p, server_rec *s) { multi_log_state *mls; mls = (multi_log_state *) ap_palloc(p, sizeof(multi_log_state)); mls->config_logs = ap_make_array(p, 1, sizeof(config_log_state)); mls->default_format_string = NULL; mls->default_format = NULL; mls->server_config_logs = NULL; mls->formats = ap_make_table(p, 4); ap_table_setn(mls->formats, "CLF", DEFAULT_LOG_FORMAT); return mls; }
int td_set_dimensions(ap_pool *pool, twodim *td, int rows, int cols) { int i, size; if (!td) td = td_create_twodim(pool); size = rows*cols; if (size<=0) return 0; td->r = rows; td->c = cols; td->a = (char**) ap_palloc(pool, sizeof(char*)*size); if(!td->a) return 0; for(i=0;i<size;i++) { td->a[i] = ap_palloc(pool, sizeof(char)); if(!td->a[i]) return 0; td->a[i][0]=0; } return 1; }
void ssl_config_global_create(void) { pool *pPool; SSLModConfigRec *mc; mc = ap_ctx_get(ap_global_ctx, "ssl_module"); if (mc == NULL) { /* * allocate an own subpool which survives server restarts */ pPool = ap_make_sub_pool(NULL); mc = (SSLModConfigRec *)ap_palloc(pPool, sizeof(SSLModConfigRec)); mc->pPool = pPool; mc->bFixed = FALSE; /* * initialize per-module configuration */ mc->nInitCount = 0; mc->nSessionCacheMode = SSL_SCMODE_UNSET; mc->szSessionCacheDataFile = NULL; mc->nSessionCacheDataSize = 0; mc->pSessionCacheDataMM = NULL; mc->tSessionCacheDataTable = NULL; mc->nMutexMode = SSL_MUTEXMODE_UNSET; mc->szMutexFile = NULL; mc->nMutexFD = -1; mc->nMutexSEMID = -1; mc->aRandSeed = ap_make_array(pPool, 4, sizeof(ssl_randseed_t)); mc->tPrivateKey = ssl_ds_table_make(pPool, sizeof(ssl_asn1_t)); mc->tPublicCert = ssl_ds_table_make(pPool, sizeof(ssl_asn1_t)); mc->tTmpKeys = ssl_ds_table_make(pPool, sizeof(ssl_asn1_t)); #ifdef SSL_EXPERIMENTAL_ENGINE mc->szCryptoDevice = NULL; #endif (void)memset(mc->pTmpKeys, 0, SSL_TKPIDX_MAX*sizeof(void *)); #ifdef SSL_VENDOR mc->ctx = ap_ctx_new(pPool); ap_hook_use("ap::mod_ssl::vendor::config_global_create", AP_HOOK_SIG2(void,ptr), AP_HOOK_MODE_ALL, mc); #endif /* * And push it into Apache's global context */ ap_ctx_set(ap_global_ctx, "ssl_module", mc); }
/* ** dav_fs_build_fname_key ** ** Given a pathname, build a DAV_TYPE_FNAME lock database key. */ static dav_datum dav_fs_build_fname_key(pool *p, const char *pathname) { dav_datum key; /* ### does this allocation have a proper lifetime? need to check */ /* ### can we use a buffer for this? */ /* size is TYPE + pathname + null */ key.dsize = strlen(pathname) + 2; key.dptr = ap_palloc(p, key.dsize); *key.dptr = DAV_TYPE_FNAME; memcpy(key.dptr + 1, pathname, key.dsize - 1); if (key.dptr[key.dsize - 2] == '/') key.dptr[--key.dsize - 1] = '\0'; return key; }
API_EXPORT(piped_log *) ap_open_piped_log (pool *p, const char *program) { piped_log *pl; FILE *dummy; if (!ap_spawn_child(p, piped_log_child, (void *)program, kill_after_timeout, &dummy, NULL, NULL)) { perror ("ap_spawn_child"); fprintf (stderr, "Couldn't fork child for piped log process\n"); exit (1); } pl = ap_palloc (p, sizeof (*pl)); pl->p = p; pl->write_f = dummy; return pl; }
/* * Given a string, replace any bare " with \" . */ API_EXPORT(char *) ap_escape_quotes (pool *p, const char *instring) { int newlen = 0; const char *inchr = instring; char *outchr, *outstring; /* * Look through the input string, jogging the length of the output * string up by an extra byte each time we find an unescaped ". */ while (*inchr != '\0') { newlen++; if (*inchr == '"') { newlen++; } /* * If we find a slosh, and it's not the last byte in the string, * it's escaping something - advance past both bytes. */ if ((*inchr == '\\') && (inchr[1] != '\0')) { inchr++; } inchr++; } outstring = ap_palloc(p, newlen + 1); inchr = instring; outchr = outstring; /* * Now copy the input string to the output string, inserting a slosh * in front of every " that doesn't already have one. */ while (*inchr != '\0') { if ((*inchr == '\\') && (inchr[1] != '\0')) { *outchr++ = *inchr++; *outchr++ = *inchr++; } if (*inchr == '"') { *outchr++ = '\\'; } if (*inchr != '\0') { *outchr++ = *inchr++; } } *outchr = '\0'; return outstring; }
void ssl_io_suck(request_rec *r, SSL *ssl) { int rc; int len; char *buf; int buflen; char c; int sucked; if ((rc = ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK)) == OK) { if (ap_should_client_block(r)) { /* read client request block through Apache API */ buflen = HUGE_STRING_LEN; buf = ap_palloc(r->pool, buflen); ap_hard_timeout("SSL I/O request body pre-sucking", r); sucked = 0; ssl_io_suck_start(r); while ((len = ap_get_client_block(r, buf, buflen)) > 0) { ssl_io_suck_record(r, buf, len); sucked += len; ap_reset_timeout(r); } ssl_io_suck_end(r); ap_kill_timeout(r); /* suck trailing data (usually CR LF) which is still in the Apache BUFF layer */ ap_hard_timeout("SSL I/O request trailing data pre-sucking", r); while (ap_bpeekc(r->connection->client) != EOF) { c = ap_bgetc(r->connection->client); ssl_io_suck_record(r, &c, 1); sucked++; } ap_kill_timeout(r); ssl_log(r->server, SSL_LOG_TRACE, "I/O: sucked %d bytes of input data from SSL/TLS I/O layer " "for delayed injection into Apache I/O layer", sucked); } } return; }
static const char *set_default_order(cmd_parms *cmd, void *m, char *direction, char *key) { char temp[4]; autoindex_config_rec *d_cfg = (autoindex_config_rec *) m; ap_cpystrn(temp, "k=d", sizeof(temp)); if (!strcasecmp(direction, "Ascending")) { temp[2] = D_ASCENDING; } else if (!strcasecmp(direction, "Descending")) { temp[2] = D_DESCENDING; } else { return "First keyword must be 'Ascending' or 'Descending'"; } if (!strcasecmp(key, "Name")) { temp[0] = K_NAME; } else if (!strcasecmp(key, "Date")) { temp[0] = K_LAST_MOD; } else if (!strcasecmp(key, "Size")) { temp[0] = K_SIZE; } else if (!strcasecmp(key, "Description")) { temp[0] = K_DESC; } else { return "Second keyword must be 'Name', 'Date', 'Size', or " "'Description'"; } if (d_cfg->default_order == NULL) { d_cfg->default_order = ap_palloc(cmd->pool, 4); d_cfg->default_order[3] = '\0'; } ap_cpystrn(d_cfg->default_order, temp, sizeof(temp)); return NULL; }
int suphp_source_child(void *rp, child_info *cinfo) { request_rec *r = (request_rec *) rp; suphp_conf *conf; pool *p = r->main ? r->main->pool : r->pool; char **argv, **env; table *empty_table = ap_make_table(p, 0); conf = ap_get_module_config(r->server->module_config, &suphp_module); /* We want to log output written to stderr */ ap_error_log2stderr(r->server); /* prepare argv for new process */ argv = ap_palloc(p, 4 * sizeof(char *)); argv[0] = ap_pstrdup(p, conf->php_path); argv[1] = "-s"; argv[2] = ap_pstrdup(p, r->filename); argv[3] = NULL; /* prepare environment */ env = ap_create_environment(p, empty_table); /* We cannot use ap_call_exec because of the interference with suExec */ /* So we do everything ourselves */ /* mandatory cleanup before execution */ ap_cleanup_for_exec(); execve(ap_pstrdup(p, conf->php_path), argv, env); /* We are still here? Okay - exec failed */ ap_log_error(APLOG_MARK, APLOG_ERR, NULL, "exec of %s failed", conf->php_path); exit(0); /* NOT REACHED */ return (0); }
/* retrieve subject CommonName of certificate */ BOOL SSL_X509_getCN(pool *p, X509 *xs, char **cppCN) { X509_NAME *xsn; X509_NAME_ENTRY *xsne; int i, nid; xsn = X509_get_subject_name(xs); for (i = 0; i < sk_X509_NAME_ENTRY_num(xsn->entries); i++) { xsne = sk_X509_NAME_ENTRY_value(xsn->entries, i); nid = OBJ_obj2nid(xsne->object); if (nid == NID_commonName) { *cppCN = ap_palloc(p, xsne->value->length+1); ap_cpystrn(*cppCN, (char *)xsne->value->data, xsne->value->length+1); (*cppCN)[xsne->value->length] = NUL; #ifdef CHARSET_EBCDIC ascii2ebcdic(*cppCN, *cppCN, strlen(*cppCN)); #endif return TRUE; } } return FALSE; }
/* Build the begin request and environ request */ apr_status_t jxr_build_request_header_msg(request_rec* r, char** hdrmsg, int header_type) { server_rec *main_server = r->server; table *table; apr_size_t hdr_len; // header_type has to be either ENV_PRMS or HDR_PRMS table = (header_type==ENV_PRMS) ? r->subprocess_env : r->headers_in; // Find the length of each header hdr_len = jxr_build_header(0, header_type, table); *hdrmsg = ap_palloc(r->pool, hdr_len); if (! *hdrmsg) { compat_log_rerror(APLOG_MARK, APLOG_WARNING, apr_get_os_error(), r, "mod_jaxer: can't alloc memory for output bucket"); return HTTP_INTERNAL_SERVER_ERROR; } jxr_build_header(*hdrmsg, header_type, table); return APR_SUCCESS; }
static const char *anon_set_string_slots(cmd_parms *cmd, anon_auth_config_rec * sec, char *arg) { auth_anon *first; if (!(*arg)) return "Anonymous string cannot be empty, use Anonymous_NoUserId instead"; /* squeeze in a record */ first = sec->auth_anon_passwords; if ( (!(sec->auth_anon_passwords = (auth_anon *) ap_palloc(cmd->pool, sizeof(auth_anon)))) || (!(sec->auth_anon_passwords->password = arg)) ) return "Failed to claim memory for an anonymous password..."; /* and repair the next */ sec->auth_anon_passwords->next = first; return NULL; }
API_EXPORT(char **) ap_create_environment(pool *p, table *t) { array_header *env_arr = ap_table_elts(t); table_entry *elts = (table_entry *) env_arr->elts; char **env = (char **) ap_palloc(p, (env_arr->nelts + 2) * sizeof(char *)); int i, j; char *tz; char *whack; j = 0; if (!ap_table_get(t, "TZ")) { tz = getenv("TZ"); if (tz != NULL) { env[j++] = ap_pstrcat(p, "TZ=", tz, NULL); } } for (i = 0; i < env_arr->nelts; ++i) { if (!elts[i].key) { continue; } env[j] = ap_pstrcat(p, elts[i].key, "=", elts[i].val, NULL); whack = env[j]; if (ap_isdigit(*whack)) { *whack++ = '_'; } while (*whack != '=') { if (!ap_isalnum(*whack) && *whack != '_') { *whack = '_'; } ++whack; } ++j; } env[j] = NULL; return env; }