int lua_apr_stat(lua_State *L) { const char *path, *name, *dir; apr_pool_t *memory_pool; lua_apr_stat_context context = { 0 }; apr_status_t status; memory_pool = to_pool(L); path = luaL_checkstring(L, 1); name = apr_filepath_name_get(path); dir = apr_pstrndup(memory_pool, path, name - path); context.firstarg = 2; context.lastarg = lua_gettop(L); check_stat_request(L, &context); status = apr_stat(&context.info, path, context.wanted, memory_pool); if (status != APR_SUCCESS && !APR_STATUS_IS_INCOMPLETE(status)) return push_error_status(L, status); /* XXX apr_stat() doesn't fill in finfo.name (tested on Linux) */ if (!(context.info.valid & APR_FINFO_NAME)) { context.info.valid |= APR_FINFO_NAME; context.info.name = name; } return push_stat_results(L, &context, dir); }
/* Helper function to print out an error involving a short option */ static apr_status_t cerr(apr_getopt_t *os, const char *err, int ch, apr_status_t status) { if (os->errfn) (os->errfn)(os->errarg, "%s: %s: %c\n", apr_filepath_name_get(*os->argv), err, ch); return status; }
/* Helper function to print out an error involving a long option */ static apr_status_t serr(apr_getopt_t *os, const char *err, const char *str, apr_status_t status) { if (os->errfn) (os->errfn)(os->errarg, "%s: %s: %s\n", apr_filepath_name_get(*os->argv), err, str); return status; }
static process_rec *init_process(int *argc, const char * const * *argv) { process_rec *process; apr_pool_t *cntx; apr_status_t stat; const char *failed = "apr_app_initialize()"; stat = apr_app_initialize(argc, argv, NULL); if (stat == APR_SUCCESS) { failed = "apr_pool_create()"; stat = apr_pool_create(&cntx, NULL); } if (stat != APR_SUCCESS) { /* For all intents and purposes, this is impossibly unlikely, * but APR doesn't exist yet, we can't use it for reporting * these earliest two failures; */ char ctimebuff[APR_CTIME_LEN]; apr_ctime(ctimebuff, apr_time_now()); fprintf(stderr, "[%s] [crit] (%d) %s: %s failed " "to initial context, exiting\n", ctimebuff, stat, (*argv)[0], failed); apr_terminate(); exit(1); } apr_pool_tag(cntx, "process"); ap_open_stderr_log(cntx); /* Now we have initialized apr and our logger, no more * exceptional error reporting required for the lifetime * of this server process. */ process = apr_palloc(cntx, sizeof(process_rec)); process->pool = cntx; apr_pool_create(&process->pconf, process->pool); apr_pool_tag(process->pconf, "pconf"); process->argc = *argc; process->argv = *argv; process->short_name = apr_filepath_name_get((*argv)[0]); return process; }
static int proc_exec(lua_State *L) { apr_status_t status; lua_apr_proc *process; const char *p, **args; int i, nargs = 0; lua_settop(L, 2); process = proc_check(L, 1); if (!lua_isnoneornil(L, 2)) { luaL_checktype(L, 2, LUA_TTABLE); nargs = lua_objlen(L, 2); } /* Allocate and initialize the array of arguments. */ args = apr_palloc(process->memory_pool, sizeof args[0] * (nargs + 2)); if (args == NULL) return push_error_memory(L); args[0] = apr_filepath_name_get(process->path); args[nargs+1] = NULL; /* Copy the arguments? */ if (nargs > 0) { for (i = 0; i <= nargs; i++) { lua_pushinteger(L, i); lua_gettable(L, 2); p = lua_tostring(L, -1); if (p != NULL) /* argument */ args[i] = apr_pstrdup(process->memory_pool, p); else if (i != 0) /* invalid value */ luaL_argcheck(L, 0, 2, lua_pushfstring(L, "invalid value at index %d", i)); lua_pop(L, 1); } } /* Create the child process using the given command line arguments. */ status = apr_proc_create(&process->handle, process->path, args, process->env, process->attr, process->memory_pool); return push_status(L, status); }
/* * main */ int main(int argc, const char * const argv[]) { apr_off_t max; apr_time_t current, repeat, delay, previous; apr_status_t status; apr_pool_t *pool, *instance; apr_getopt_t *o; apr_finfo_t info; int retries, isdaemon, limit_found, intelligent, dowork; char opt; const char *arg; char *proxypath, *path; interrupted = 0; repeat = 0; isdaemon = 0; dryrun = 0; limit_found = 0; max = 0; verbose = 0; realclean = 0; benice = 0; deldirs = 0; intelligent = 0; previous = 0; /* avoid compiler warning */ proxypath = NULL; if (apr_app_initialize(&argc, &argv, NULL) != APR_SUCCESS) { return 1; } atexit(apr_terminate); if (argc) { shortname = apr_filepath_name_get(argv[0]); } if (apr_pool_create(&pool, NULL) != APR_SUCCESS) { return 1; } apr_pool_abort_set(oom, pool); apr_file_open_stderr(&errfile, pool); apr_signal(SIGINT, setterm); apr_signal(SIGTERM, setterm); apr_getopt_init(&o, pool, argc, argv); while (1) { status = apr_getopt(o, "iDnvrtd:l:L:p:", &opt, &arg); if (status == APR_EOF) { break; } else if (status != APR_SUCCESS) { usage(); } else { switch (opt) { case 'i': if (intelligent) { usage(); } intelligent = 1; break; case 'D': if (dryrun) { usage(); } dryrun = 1; break; case 'n': if (benice) { usage(); } benice = 1; break; case 't': if (deldirs) { usage(); } deldirs = 1; break; case 'v': if (verbose) { usage(); } verbose = 1; break; case 'r': if (realclean) { usage(); } realclean = 1; deldirs = 1; break; case 'd': if (isdaemon) { usage(); } isdaemon = 1; repeat = apr_atoi64(arg); repeat *= SECS_PER_MIN; repeat *= APR_USEC_PER_SEC; break; case 'l': if (limit_found) { usage(); } limit_found = 1; do { apr_status_t rv; char *end; rv = apr_strtoff(&max, arg, &end, 10); if (rv == APR_SUCCESS) { if ((*end == 'K' || *end == 'k') && !end[1]) { max *= KBYTE; } else if ((*end == 'M' || *end == 'm') && !end[1]) { max *= MBYTE; } else if ((*end == 'G' || *end == 'g') && !end[1]) { max *= GBYTE; } else if (*end && /* neither empty nor [Bb] */ ((*end != 'B' && *end != 'b') || end[1])) { rv = APR_EGENERAL; } } if (rv != APR_SUCCESS) { apr_file_printf(errfile, "Invalid limit: %s" APR_EOL_STR APR_EOL_STR, arg); usage(); } } while(0); break; case 'p': if (proxypath) { usage(); } proxypath = apr_pstrdup(pool, arg); if (apr_filepath_set(proxypath, pool) != APR_SUCCESS) { usage(); } break; } /* switch */ } /* else */ } /* while */ if (o->ind != argc) { usage(); } if (isdaemon && (repeat <= 0 || verbose || realclean || dryrun)) { usage(); } if (!isdaemon && intelligent) { usage(); } if (!proxypath || max <= 0) { usage(); } if (apr_filepath_get(&path, 0, pool) != APR_SUCCESS) { usage(); } baselen = strlen(path); #ifndef DEBUG if (isdaemon) { apr_file_close(errfile); apr_proc_detach(APR_PROC_DETACH_DAEMONIZE); } #endif do { apr_pool_create(&instance, pool); now = apr_time_now(); APR_RING_INIT(&root, _entry, link); delcount = 0; unsolicited = 0; dowork = 0; switch (intelligent) { case 0: dowork = 1; break; case 1: retries = STAT_ATTEMPTS; status = APR_SUCCESS; do { if (status != APR_SUCCESS) { apr_sleep(STAT_DELAY); } status = apr_stat(&info, path, APR_FINFO_MTIME, instance); } while (status != APR_SUCCESS && !interrupted && --retries); if (status == APR_SUCCESS) { previous = info.mtime; intelligent = 2; } dowork = 1; break; case 2: retries = STAT_ATTEMPTS; status = APR_SUCCESS; do { if (status != APR_SUCCESS) { apr_sleep(STAT_DELAY); } status = apr_stat(&info, path, APR_FINFO_MTIME, instance); } while (status != APR_SUCCESS && !interrupted && --retries); if (status == APR_SUCCESS) { if (previous != info.mtime) { dowork = 1; } previous = info.mtime; break; } intelligent = 1; dowork = 1; break; } if (dowork && !interrupted) { if (!process_dir(path, instance) && !interrupted) { purge(path, instance, max); } else if (!isdaemon && !interrupted) { apr_file_printf(errfile, "An error occurred, cache cleaning " "aborted." APR_EOL_STR); return 1; } if (intelligent && !interrupted) { retries = STAT_ATTEMPTS; status = APR_SUCCESS; do { if (status != APR_SUCCESS) { apr_sleep(STAT_DELAY); } status = apr_stat(&info, path, APR_FINFO_MTIME, instance); } while (status != APR_SUCCESS && !interrupted && --retries); if (status == APR_SUCCESS) { previous = info.mtime; intelligent = 2; } else { intelligent = 1; } } } apr_pool_destroy(instance); current = apr_time_now(); if (current < now) { delay = repeat; } else if (current - now >= repeat) { delay = repeat; } else { delay = now + repeat - current; } /* we can't sleep the whole delay time here apiece as this is racy * with respect to interrupt delivery - think about what happens * if we have tested for an interrupt, then get scheduled * before the apr_sleep() call and while waiting for the cpu * we do get an interrupt */ if (isdaemon) { while (delay && !interrupted) { if (delay > APR_USEC_PER_SEC) { apr_sleep(APR_USEC_PER_SEC); delay -= APR_USEC_PER_SEC; } else { apr_sleep(delay); delay = 0; } } } } while (isdaemon && !interrupted); if (!isdaemon && interrupted) { apr_file_printf(errfile, "Cache cleaning aborted due to user " "request." APR_EOL_STR); return 1; } return 0; }
int main(int argc, const char *const argv[]) { apr_pool_t *pool; apr_status_t rv = APR_SUCCESS; apr_getopt_t *opt; const char *opt_arg; char ch; apr_file_t *infile; apr_dbm_t *outdbm; apr_app_initialize(&argc, &argv, NULL); atexit(apr_terminate); verbose = 0; format = NULL; input = NULL; output = NULL; apr_pool_create(&pool, NULL); if (argc) { shortname = apr_filepath_name_get(argv[0]); } else { shortname = "httxt2dbm"; } apr_file_open_stderr(&errfile, pool); rv = apr_getopt_init(&opt, pool, argc, argv); if (rv != APR_SUCCESS) { apr_file_printf(errfile, "Error: apr_getopt_init failed."NL NL); return 1; } if (argc <= 1) { usage(); return 1; } while ((rv = apr_getopt(opt, "vf::i::o::", &ch, &opt_arg)) == APR_SUCCESS) { switch (ch) { case 'v': if (verbose) { apr_file_printf(errfile, "Error: -v can only be passed once" NL NL); usage(); return 1; } verbose = 1; break; case 'f': if (format) { apr_file_printf(errfile, "Error: -f can only be passed once" NL NL); usage(); return 1; } format = apr_pstrdup(pool, opt_arg); break; case 'i': if (input) { apr_file_printf(errfile, "Error: -i can only be passed once" NL NL); usage(); return 1; } input = apr_pstrdup(pool, opt_arg); break; case 'o': if (output) { apr_file_printf(errfile, "Error: -o can only be passed once" NL NL); usage(); return 1; } output = apr_pstrdup(pool, opt_arg); break; } } if (rv != APR_EOF) { apr_file_printf(errfile, "Error: Parsing Arguments Failed" NL NL); usage(); return 1; } if (!input) { apr_file_printf(errfile, "Error: No input file specified." NL NL); usage(); return 1; } if (!output) { apr_file_printf(errfile, "Error: No output DBM specified." NL NL); usage(); return 1; } if (!format) { format = "default"; } if (verbose) { apr_file_printf(errfile, "DBM Format: %s"NL, format); } if (!strcmp(input, "-")) { rv = apr_file_open_stdin(&infile, pool); } else { rv = apr_file_open(&infile, input, APR_READ|APR_BUFFERED, APR_OS_DEFAULT, pool); } if (rv != APR_SUCCESS) { apr_file_printf(errfile, "Error: Cannot open input file '%s': (%d) %s" NL NL, input, rv, apr_strerror(rv, errbuf, sizeof(errbuf))); return 1; } if (verbose) { apr_file_printf(errfile, "Input File: %s"NL, input); } rv = apr_dbm_open_ex(&outdbm, format, output, APR_DBM_RWCREATE, APR_OS_DEFAULT, pool); if (APR_STATUS_IS_ENOTIMPL(rv)) { apr_file_printf(errfile, "Error: The requested DBM Format '%s' is not available." NL NL, format); return 1; } if (rv != APR_SUCCESS) { apr_file_printf(errfile, "Error: Cannot open output DBM '%s': (%d) %s" NL NL, output, rv, apr_strerror(rv, errbuf, sizeof(errbuf))); return 1; } if (verbose) { apr_file_printf(errfile, "DBM File: %s"NL, output); } rv = to_dbm(outdbm, infile, pool); if (rv != APR_SUCCESS) { apr_file_printf(errfile, "Error: Converting to DBM: (%d) %s" NL NL, rv, apr_strerror(rv, errbuf, sizeof(errbuf))); return 1; } apr_dbm_close(outdbm); if (verbose) { apr_file_printf(errfile, "Conversion Complete." NL); } return 0; }
AP_DECLARE(int) ap_scan_script_header_err_core(request_rec *r, char *buffer, int (*getsfunc) (char *, int, void *), void *getsfunc_data) { char x[MAX_STRING_LEN]; char *w, *l; int p; int cgi_status = HTTP_UNSET; apr_table_t *merge; apr_table_t *cookie_table; if (buffer) { *buffer = '\0'; } w = buffer ? buffer : x; /* temporary place to hold headers to merge in later */ merge = apr_table_make(r->pool, 10); /* The HTTP specification says that it is legal to merge duplicate * headers into one. Some browsers that support Cookies don't like * merged headers and prefer that each Set-Cookie header is sent * separately. Lets humour those browsers by not merging. * Oh what a pain it is. */ cookie_table = apr_table_make(r->pool, 2); apr_table_do(set_cookie_doo_doo, cookie_table, r->err_headers_out, "Set-Cookie", NULL); while (1) { int rv = (*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data); if (rv == 0) { ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r, "Premature end of script headers: %s", apr_filepath_name_get(r->filename)); return HTTP_INTERNAL_SERVER_ERROR; } else if (rv == -1) { ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r, "Script timed out before returning headers: %s", apr_filepath_name_get(r->filename)); return HTTP_GATEWAY_TIME_OUT; } /* Delete terminal (CR?)LF */ p = strlen(w); /* Indeed, the host's '\n': '\012' for UNIX; '\015' for MacOS; '\025' for OS/390 -- whatever the script generates. */ if (p > 0 && w[p - 1] == '\n') { if (p > 1 && w[p - 2] == CR) { w[p - 2] = '\0'; } else { w[p - 1] = '\0'; } } /* * If we've finished reading the headers, check to make sure any * HTTP/1.1 conditions are met. If so, we're done; normal processing * will handle the script's output. If not, just return the error. * The appropriate thing to do would be to send the script process a * SIGPIPE to let it know we're ignoring it, close the channel to the * script process, and *then* return the failed-to-meet-condition * error. Otherwise we'd be waiting for the script to finish * blithering before telling the client the output was no good. * However, we don't have the information to do that, so we have to * leave it to an upper layer. */ if (w[0] == '\0') { int cond_status = OK; /* PR#38070: This fails because it gets confused when a * CGI Status header overrides ap_meets_conditions. * * We can fix that by dropping ap_meets_conditions when * Status has been set. Since this is the only place * cgi_status gets used, let's test it explicitly. * * The alternative would be to ignore CGI Status when * ap_meets_conditions returns anything interesting. * That would be safer wrt HTTP, but would break CGI. */ if ((cgi_status == HTTP_UNSET) && (r->method_number == M_GET)) { cond_status = ap_meets_conditions(r); } apr_table_overlap(r->err_headers_out, merge, APR_OVERLAP_TABLES_MERGE); if (!apr_is_empty_table(cookie_table)) { /* the cookies have already been copied to the cookie_table */ apr_table_unset(r->err_headers_out, "Set-Cookie"); r->err_headers_out = apr_table_overlay(r->pool, r->err_headers_out, cookie_table); } return cond_status; } /* if we see a bogus header don't ignore it. Shout and scream */ #if APR_CHARSET_EBCDIC /* Chances are that we received an ASCII header text instead of * the expected EBCDIC header lines. Try to auto-detect: */ if (!(l = strchr(w, ':'))) { int maybeASCII = 0, maybeEBCDIC = 0; unsigned char *cp, native; apr_size_t inbytes_left, outbytes_left; for (cp = w; *cp != '\0'; ++cp) { native = apr_xlate_conv_byte(ap_hdrs_from_ascii, *cp); if (apr_isprint(*cp) && !apr_isprint(native)) ++maybeEBCDIC; if (!apr_isprint(*cp) && apr_isprint(native)) ++maybeASCII; } if (maybeASCII > maybeEBCDIC) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, r->server, "CGI Interface Error: Script headers apparently ASCII: (CGI = %s)", r->filename); inbytes_left = outbytes_left = cp - w; apr_xlate_conv_buffer(ap_hdrs_from_ascii, w, &inbytes_left, w, &outbytes_left); } } #endif /*APR_CHARSET_EBCDIC*/ if (!(l = strchr(w, ':'))) { char malformed[(sizeof MALFORMED_MESSAGE) + 1 + MALFORMED_HEADER_LENGTH_TO_SHOW]; strcpy(malformed, MALFORMED_MESSAGE); strncat(malformed, w, MALFORMED_HEADER_LENGTH_TO_SHOW); if (!buffer) { /* Soak up all the script output - may save an outright kill */ while ((*getsfunc) (w, MAX_STRING_LEN - 1, getsfunc_data)) { continue; } } ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, 0, r, "%s: %s", malformed, apr_filepath_name_get(r->filename)); return HTTP_INTERNAL_SERVER_ERROR; } *l++ = '\0'; while (*l && apr_isspace(*l)) { ++l; } if (!strcasecmp(w, "Content-type")) { char *tmp; /* Nuke trailing whitespace */ char *endp = l + strlen(l) - 1; while (endp > l && apr_isspace(*endp)) { *endp-- = '\0'; } tmp = apr_pstrdup(r->pool, l); ap_content_type_tolower(tmp); ap_set_content_type(r, tmp); } /* * If the script returned a specific status, that's what * we'll use - otherwise we assume 200 OK. */ else if (!strcasecmp(w, "Status")) { r->status = cgi_status = atoi(l); r->status_line = apr_pstrdup(r->pool, l); } else if (!strcasecmp(w, "Location")) { apr_table_set(r->headers_out, w, l); } else if (!strcasecmp(w, "Content-Length")) { apr_table_set(r->headers_out, w, l); } else if (!strcasecmp(w, "Content-Range")) { apr_table_set(r->headers_out, w, l); } else if (!strcasecmp(w, "Transfer-Encoding")) { apr_table_set(r->headers_out, w, l); } /* * If the script gave us a Last-Modified header, we can't just * pass it on blindly because of restrictions on future values. */ else if (!strcasecmp(w, "Last-Modified")) { ap_update_mtime(r, apr_date_parse_http(l)); ap_set_last_modified(r); } else if (!strcasecmp(w, "Set-Cookie")) { apr_table_add(cookie_table, w, l); } else { apr_table_add(merge, w, l); } } return OK; }
static int cgi_handler(request_rec *r) { int nph; apr_size_t dbpos = 0; const char *argv0; const char *command; const char **argv; char *dbuf = NULL; apr_file_t *script_out = NULL, *script_in = NULL, *script_err = NULL; apr_bucket_brigade *bb; apr_bucket *b; int is_included; int seen_eos, child_stopped_reading; apr_pool_t *p; cgi_server_conf *conf; apr_status_t rv; cgi_exec_info_t e_info; conn_rec *c = r->connection; if(strcmp(r->handler, CGI_MAGIC_TYPE) && strcmp(r->handler, "cgi-script")) return DECLINED; is_included = !strcmp(r->protocol, "INCLUDED"); p = r->main ? r->main->pool : r->pool; argv0 = apr_filepath_name_get(r->filename); nph = !(strncmp(argv0, "nph-", 4)); conf = ap_get_module_config(r->server->module_config, &cgi_module); if (!(ap_allow_options(r) & OPT_EXECCGI) && !is_scriptaliased(r)) return log_scripterror(r, conf, HTTP_FORBIDDEN, 0, "Options ExecCGI is off in this directory"); if (nph && is_included) return log_scripterror(r, conf, HTTP_FORBIDDEN, 0, "attempt to include NPH CGI script"); if (r->finfo.filetype == 0) return log_scripterror(r, conf, HTTP_NOT_FOUND, 0, "script not found or unable to stat"); if (r->finfo.filetype == APR_DIR) return log_scripterror(r, conf, HTTP_FORBIDDEN, 0, "attempt to invoke directory as script"); if ((r->used_path_info == AP_REQ_REJECT_PATH_INFO) && r->path_info && *r->path_info) { /* default to accept */ return log_scripterror(r, conf, HTTP_NOT_FOUND, 0, "AcceptPathInfo off disallows user's path"); } /* if (!ap_suexec_enabled) { if (!ap_can_exec(&r->finfo)) return log_scripterror(r, conf, HTTP_FORBIDDEN, 0, "file permissions deny server execution"); } */ ap_add_common_vars(r); ap_add_cgi_vars(r); e_info.process_cgi = 1; e_info.cmd_type = APR_PROGRAM; e_info.detached = 0; e_info.in_pipe = APR_CHILD_BLOCK; e_info.out_pipe = APR_CHILD_BLOCK; e_info.err_pipe = APR_CHILD_BLOCK; e_info.prog_type = RUN_AS_CGI; e_info.bb = NULL; e_info.ctx = NULL; e_info.next = NULL; e_info.addrspace = 0; /* build the command line */ if ((rv = cgi_build_command(&command, &argv, r, p, &e_info)) != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "don't know how to spawn child process: %s", r->filename); return HTTP_INTERNAL_SERVER_ERROR; } /* run the script in its own process */ if ((rv = run_cgi_child(&script_out, &script_in, &script_err, command, argv, r, p, &e_info)) != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "couldn't spawn child process: %s", r->filename); return HTTP_INTERNAL_SERVER_ERROR; } /* Transfer any put/post args, CERN style... * Note that we already ignore SIGPIPE in the core server. */ bb = apr_brigade_create(r->pool, c->bucket_alloc); seen_eos = 0; child_stopped_reading = 0; if (conf->logname) { dbuf = apr_palloc(r->pool, conf->bufbytes + 1); dbpos = 0; } do { apr_bucket *bucket; rv = ap_get_brigade(r->input_filters, bb, AP_MODE_READBYTES, APR_BLOCK_READ, HUGE_STRING_LEN); if (rv != APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_ERR, rv, r, "Error reading request entity data"); return ap_map_http_request_error(rv, HTTP_BAD_REQUEST); } for (bucket = APR_BRIGADE_FIRST(bb); bucket != APR_BRIGADE_SENTINEL(bb); bucket = APR_BUCKET_NEXT(bucket)) { const char *data; apr_size_t len; if (APR_BUCKET_IS_EOS(bucket)) { seen_eos = 1; break; } /* We can't do much with this. */ if (APR_BUCKET_IS_FLUSH(bucket)) { continue; } /* If the child stopped, we still must read to EOS. */ if (child_stopped_reading) { continue; } /* read */ apr_bucket_read(bucket, &data, &len, APR_BLOCK_READ); if (conf->logname && dbpos < conf->bufbytes) { int cursize; if ((dbpos + len) > conf->bufbytes) { cursize = conf->bufbytes - dbpos; } else { cursize = len; } memcpy(dbuf + dbpos, data, cursize); dbpos += cursize; } /* Keep writing data to the child until done or too much time * elapses with no progress or an error occurs. */ rv = apr_file_write_full(script_out, data, len, NULL); if (rv != APR_SUCCESS) { /* silly script stopped reading, soak up remaining message */ child_stopped_reading = 1; } } apr_brigade_cleanup(bb); } while (!seen_eos); if (conf->logname) { dbuf[dbpos] = '\0'; } /* Is this flush really needed? */ apr_file_flush(script_out); apr_file_close(script_out); AP_DEBUG_ASSERT(script_in != NULL); apr_brigade_cleanup(bb); #if APR_FILES_AS_SOCKETS apr_file_pipe_timeout_set(script_in, 0); apr_file_pipe_timeout_set(script_err, 0); b = cgi_bucket_create(r, script_in, script_err, c->bucket_alloc); #else b = apr_bucket_pipe_create(script_in, c->bucket_alloc); #endif APR_BRIGADE_INSERT_TAIL(bb, b); b = apr_bucket_eos_create(c->bucket_alloc); APR_BRIGADE_INSERT_TAIL(bb, b); /* Handle script return... */ if (!nph) { const char *location; char sbuf[MAX_STRING_LEN]; int ret; if ((ret = ap_scan_script_header_err_brigade(r, bb, sbuf))) { ret = log_script(r, conf, ret, dbuf, sbuf, bb, script_err); /* * ret could be HTTP_NOT_MODIFIED in the case that the CGI script * does not set an explicit status and ap_meets_conditions, which * is called by ap_scan_script_header_err_brigade, detects that * the conditions of the requests are met and the response is * not modified. * In this case set r->status and return OK in order to prevent * running through the error processing stack as this would * break with mod_cache, if the conditions had been set by * mod_cache itself to validate a stale entity. * BTW: We circumvent the error processing stack anyway if the * CGI script set an explicit status code (whatever it is) and * the only possible values for ret here are: * * HTTP_NOT_MODIFIED (set by ap_meets_conditions) * HTTP_PRECONDITION_FAILED (set by ap_meets_conditions) * HTTP_INTERNAL_SERVER_ERROR (if something went wrong during the * processing of the response of the CGI script, e.g broken headers * or a crashed CGI process). */ if (ret == HTTP_NOT_MODIFIED) { r->status = ret; return OK; } return ret; } location = apr_table_get(r->headers_out, "Location"); if (location && r->status == 200) { /* For a redirect whether internal or not, discard any * remaining stdout from the script, and log any remaining * stderr output, as normal. */ discard_script_output(bb); apr_brigade_destroy(bb); apr_file_pipe_timeout_set(script_err, r->server->timeout); log_script_err(r, script_err); } if (location && location[0] == '/' && r->status == 200) { /* This redirect needs to be a GET no matter what the original * method was. */ r->method = apr_pstrdup(r->pool, "GET"); r->method_number = M_GET; /* We already read the message body (if any), so don't allow * the redirected request to think it has one. We can ignore * Transfer-Encoding, since we used REQUEST_CHUNKED_ERROR. */ apr_table_unset(r->headers_in, "Content-Length"); ap_internal_redirect_handler(location, r); return OK; } else if (location && r->status == 200) { /* XX Note that if a script wants to produce its own Redirect * body, it now has to explicitly *say* "Status: 302" */ return HTTP_MOVED_TEMPORARILY; } rv = ap_pass_brigade(r->output_filters, bb); } else /* nph */ { struct ap_filter_t *cur; /* get rid of all filters up through protocol... since we * haven't parsed off the headers, there is no way they can * work */ cur = r->proto_output_filters; while (cur && cur->frec->ftype < AP_FTYPE_CONNECTION) { cur = cur->next; } r->output_filters = r->proto_output_filters = cur; rv = ap_pass_brigade(r->output_filters, bb); } /* don't soak up script output if errors occurred writing it * out... otherwise, we prolong the life of the script when the * connection drops or we stopped sending output for some other * reason */ if (rv == APR_SUCCESS && !r->connection->aborted) { apr_file_pipe_timeout_set(script_err, r->server->timeout); log_script_err(r, script_err); } apr_file_close(script_err); return OK; /* NOT r->status, even if it has changed. */ }
static apr_status_t run_cgi_child(apr_file_t **script_out, apr_file_t **script_in, apr_file_t **script_err, const char *command, const char * const argv[], request_rec *r, apr_pool_t *p, cgi_exec_info_t *e_info) { const char * const *env; apr_procattr_t *procattr; apr_proc_t *procnew; apr_status_t rc = APR_SUCCESS; #if defined(RLIMIT_CPU) || defined(RLIMIT_NPROC) || \ defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined (RLIMIT_AS) core_dir_config *conf = ap_get_module_config(r->per_dir_config, &core_module); #endif #ifdef DEBUG_CGI #ifdef OS2 /* Under OS/2 need to use device con. */ FILE *dbg = fopen("con", "w"); #else FILE *dbg = fopen("/dev/tty", "w"); #endif int i; #endif RAISE_SIGSTOP(CGI_CHILD); #ifdef DEBUG_CGI fprintf(dbg, "Attempting to exec %s as CGI child (argv0 = %s)\n", r->filename, argv[0]); #endif env = (const char * const *)ap_create_environment(p, r->subprocess_env); #ifdef DEBUG_CGI fprintf(dbg, "Environment: \n"); for (i = 0; env[i]; ++i) fprintf(dbg, "'%s'\n", env[i]); #endif /* Transmute ourselves into the script. * NB only ISINDEX scripts get decoded arguments. */ if (((rc = apr_procattr_create(&procattr, p)) != APR_SUCCESS) || ((rc = apr_procattr_io_set(procattr, e_info->in_pipe, e_info->out_pipe, e_info->err_pipe)) != APR_SUCCESS) || ((rc = apr_procattr_dir_set(procattr, ap_make_dirstr_parent(r->pool, r->filename))) != APR_SUCCESS) || #ifdef RLIMIT_CPU ((rc = apr_procattr_limit_set(procattr, APR_LIMIT_CPU, conf->limit_cpu)) != APR_SUCCESS) || #endif #if defined(RLIMIT_DATA) || defined(RLIMIT_VMEM) || defined(RLIMIT_AS) ((rc = apr_procattr_limit_set(procattr, APR_LIMIT_MEM, conf->limit_mem)) != APR_SUCCESS) || #endif #ifdef RLIMIT_NPROC ((rc = apr_procattr_limit_set(procattr, APR_LIMIT_NPROC, conf->limit_nproc)) != APR_SUCCESS) || #endif ((rc = apr_procattr_cmdtype_set(procattr, e_info->cmd_type)) != APR_SUCCESS) || ((rc = apr_procattr_detach_set(procattr, e_info->detached)) != APR_SUCCESS) || ((rc = apr_procattr_addrspace_set(procattr, e_info->addrspace)) != APR_SUCCESS) || ((rc = apr_procattr_child_errfn_set(procattr, cgi_child_errfn)) != APR_SUCCESS)) { /* Something bad happened, tell the world. */ ap_log_rerror(APLOG_MARK, APLOG_ERR, rc, r, "couldn't set child process attributes: %s", r->filename); } else { procnew = apr_pcalloc(p, sizeof(*procnew)); rc = ap_os_create_privileged_process(r, procnew, command, argv, env, procattr, p); if (rc != APR_SUCCESS) { /* Bad things happened. Everyone should have cleaned up. */ ap_log_rerror(APLOG_MARK, APLOG_ERR|APLOG_TOCLIENT, rc, r, "couldn't create child process: %d: %s", rc, apr_filepath_name_get(r->filename)); } else { apr_pool_note_subprocess(p, procnew, APR_KILL_AFTER_TIMEOUT); *script_in = procnew->out; if (!*script_in) return APR_EBADF; apr_file_pipe_timeout_set(*script_in, r->server->timeout); if (e_info->prog_type == RUN_AS_CGI) { *script_out = procnew->in; if (!*script_out) return APR_EBADF; apr_file_pipe_timeout_set(*script_out, r->server->timeout); *script_err = procnew->err; if (!*script_err) return APR_EBADF; apr_file_pipe_timeout_set(*script_err, r->server->timeout); } } } #ifdef DEBUG_CGI fclose(dbg); #endif return (rc); }
static int vt_column(sqlite3_vtab_cursor *cur, sqlite3_context *ctx, int col) { vtab_cursor *p_cur = (vtab_cursor*)cur; struct filenode* d = p_cur->current_node; /* Just return the ordinal of the column requested. */ switch(col) { /* col 0: file name */ case 0: { /* Will be present if entry is a file */ if (d->dirent.name != NULL) { sqlite3_result_text( ctx, d->dirent.name, strlen(d->dirent.name), SQLITE_STATIC ); break; } /* Will be present if entry is a directory */ if (d->dirent.fname != NULL) { sqlite3_result_text( ctx, d->dirent.fname, strlen(d->dirent.fname), SQLITE_STATIC ); break; } /* Shouldn't happen: no value. */ sqlite3_result_text(ctx, "", 0, SQLITE_STATIC); break; } /* col 1: file path */ case 1: { if (d->path != NULL) { int len = 0; char path[PATH_MAX]; /* If this entry is a top-level file */ if (d->dir == NULL) { /** Then the full path is the path of the file name. Get * length of path up to the filename. The -1 strips * trailing separator */ len = (int)apr_filepath_name_get(d->path) - (int)d->path - 1; } else { /* Copy the entire path as it is the full directory */ len = strlen(d->path); } strncpy(path, d->path, len); path[len] = '\0'; sqlite3_result_text( ctx, path, strlen(path), SQLITE_STATIC ); } else { /* No value. */ sqlite3_result_text(ctx, "", 0, SQLITE_STATIC); } break; } /* col 2: file type */ case 2: { sqlite3_result_int(ctx, d->dirent.filetype); break; } /* col 3: file size */ case 3: { sqlite3_result_int(ctx, d->dirent.size); break; } /* col 4: uid */ case 4: { sqlite3_result_int(ctx, d->dirent.user); break; } /* col 5: gid */ case 5: { sqlite3_result_int(ctx, d->dirent.group); break; } /* col 6: protection bits */ case 6: { sqlite3_result_int(ctx, d->dirent.protection); break; } /* col 7: modified time */ case 7: { sqlite3_result_int64(ctx, d->dirent.mtime); break; } /* col 8: create time */ case 8: { sqlite3_result_int64(ctx, d->dirent.ctime); break; } /* col 9: access time */ case 9: { sqlite3_result_int64(ctx, d->dirent.atime); break; } /* col 10: device */ case 10: { sqlite3_result_int(ctx, d->dirent.device); break; } /* col 11: number of links */ case 11: { sqlite3_result_int(ctx, d->dirent.nlink); break; } /* col 12: inode */ case 12: { sqlite3_result_int(ctx, d->dirent.inode); break; } /* col 13: dir inode */ case 13: { if (p_cur->current_node->parent != NULL) { sqlite3_result_int(ctx, p_cur->current_node->parent->dirent.inode); } else { sqlite3_result_int(ctx, 0); } break; } default: { sqlite3_result_text(ctx, "", 0, SQLITE_STATIC); } } return SQLITE_OK; }
if (finfo.filetype != APR_REG && #if defined(WIN32) || defined(OS2) || defined(NETWARE) strcasecmp(apr_filepath_name_get(name), "nul") != 0) { #else strcmp(name, "/dev/null") != 0) { #endif /* WIN32 || OS2 */ ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, "Access to file %s denied by server: not a regular file", name); apr_file_close(file); return APR_EBADF; }
APR_DECLARE(apr_status_t) apr_getopt(apr_getopt_t *os, const char *opts, char *optch, const char **optarg) { const char *oli; /* option letter list index */ if (os->reset || !*os->place) { /* update scanning pointer */ os->reset = 0; if (os->ind >= os->argc || *(os->place = os->argv[os->ind]) != '-') { os->place = EMSG; *optch = os->opt; return (APR_EOF); } if (os->place[1] && *++os->place == '-') { /* found "--" */ ++os->ind; os->place = EMSG; *optch = os->opt; return (APR_EOF); } } /* option letter okay? */ if ((os->opt = (int) *os->place++) == (int) ':' || !(oli = strchr(opts, os->opt))) { /* * if the user didn't specify '-' as an option, * assume it means -1. */ if (os->opt == (int) '-') { *optch = os->opt; return (APR_EOF); } if (!*os->place) ++os->ind; if (os->errfn && *opts != ':') { (os->errfn)(os->errarg, "%s: illegal option -- %c\n", apr_filepath_name_get(*os->argv), os->opt); } *optch = os->opt; return (APR_BADCH); } if (*++oli != ':') { /* don't need argument */ *optarg = NULL; if (!*os->place) ++os->ind; } else { /* need an argument */ if (*os->place) /* no white space */ *optarg = os->place; else if (os->argc <= ++os->ind) { /* no arg */ os->place = EMSG; if (*opts == ':') { *optch = os->opt; return (APR_BADARG); } if (os->errfn) { (os->errfn)(os->errarg, "%s: option requires an argument -- %c\n", apr_filepath_name_get(*os->argv), os->opt); } *optch = os->opt; return (APR_BADCH); } else /* white space */ *optarg = os->argv[os->ind]; os->place = EMSG; ++os->ind; } *optch = os->opt; return APR_SUCCESS; }