Eliminate the need for SetHandler or AddHandler with static or
[mod_fastcgi.git] / fcgi_config.c
blob83b035fd8fee9a2b54a92d6305b4e9d1d6f1856a
1 /*
2 * $Id: fcgi_config.c,v 1.40 2002/10/04 04:33:20 robs Exp $
3 */
5 #define CORE_PRIVATE
6 #include "fcgi.h"
8 #ifdef APACHE2
10 #include <limits.h>
11 #include "mpm_common.h" /* ap_uname2id, ap_gname2id */
13 #ifdef WIN32
14 #include <direct.h>
15 #else
16 #include <unistd.h>
17 #include "unixd.h"
18 #endif
20 #endif
22 #ifdef WIN32
23 /* warning C4100: unreferenced formal parameter */
24 /* warning C4706: assignment within conditional expression */
25 #pragma warning( disable : 4100 4706 )
26 #endif
28 /*******************************************************************************
29 * Get the next configuration directive argument, & return an in_addr and port.
30 * The arg must be in the form "host:port" where host can be an IP or hostname.
31 * The pool arg should be persistant storage.
33 static const char *get_host_n_port(pool *p, const char **arg,
34 const char **host, u_short *port)
36 char *cvptr, *portStr;
37 long tmp;
39 *host = ap_getword_conf(p, arg);
40 if (**host == '\0')
41 return "\"\"";
43 portStr = strchr(*host, ':');
44 if (portStr == NULL)
45 return "missing port specification";
47 /* Split the host and port portions */
48 *portStr++ = '\0';
50 /* Convert port number */
51 tmp = (u_short) strtol(portStr, &cvptr, 10);
52 if (*cvptr != '\0' || tmp < 1 || tmp > USHRT_MAX)
53 return ap_pstrcat(p, "bad port number \"", portStr, "\"", NULL);
55 *port = (unsigned short) tmp;
57 return NULL;
60 /*******************************************************************************
61 * Get the next configuration directive argument, & return an u_short.
62 * The pool arg should be temporary storage.
64 static const char *get_u_short(pool *p, const char **arg,
65 u_short *num, u_short min)
67 char *ptr;
68 long tmp;
69 const char *txt = ap_getword_conf(p, arg);
71 if (*txt == '\0') {
72 return "\"\"";
75 tmp = strtol(txt, &ptr, 10);
77 if (*ptr != '\0') {
78 return ap_pstrcat(p, "\"", txt, "\" must be a positive integer", NULL);
81 if (tmp < min || tmp > USHRT_MAX) {
82 return ap_psprintf(p, "\"%u\" must be >= %u and < %u", *num, min, USHRT_MAX);
85 *num = (u_short) tmp;
87 return NULL;
90 static const char *get_int(pool *p, const char **arg, int *num, int min)
92 char *cp;
93 const char *val = ap_getword_conf(p, arg);
95 if (*val == '\0')
97 return "\"\"";
100 *num = (int) strtol(val, &cp, 10);
102 if (*cp != '\0')
104 return ap_pstrcat(p, "can't parse ", "\"", val, "\"", NULL);
106 else if (*num < min)
108 return ap_psprintf(p, "\"%d\" must be >= %d", *num, min);
111 return NULL;
114 /*******************************************************************************
115 * Get the next configuration directive argument, & return an u_int.
116 * The pool arg should be temporary storage.
118 static const char *get_u_int(pool *p, const char **arg,
119 u_int *num, u_int min)
121 char *ptr;
122 const char *val = ap_getword_conf(p, arg);
124 if (*val == '\0')
125 return "\"\"";
126 *num = (u_int)strtol(val, &ptr, 10);
128 if (*ptr != '\0')
129 return ap_pstrcat(p, "\"", val, "\" must be a positive integer", NULL);
130 else if (*num < min)
131 return ap_psprintf(p, "\"%u\" must be >= %u", *num, min);
132 return NULL;
135 /*******************************************************************************
136 * Get the next configuration directive argument, & return a float.
137 * The pool arg should be temporary storage.
139 static const char *get_float(pool *p, const char **arg,
140 float *num, float min, float max)
142 char *ptr;
143 const char *val = ap_getword_conf(p, arg);
145 if (*val == '\0')
146 return "\"\"";
147 *num = (float) strtod(val, &ptr);
149 if (*ptr != '\0')
150 return ap_pstrcat(p, "\"", val, "\" is not a floating point number", NULL);
151 if (*num < min || *num > max)
152 return ap_psprintf(p, "\"%f\" is not between %f and %f", *num, min, max);
153 return NULL;
156 const char *fcgi_config_set_env_var(pool *p, char **envp, unsigned int *envc, char * var)
158 if (*envc >= MAX_INIT_ENV_VARS) {
159 return "too many variables, must be <= MAX_INIT_ENV_VARS";
162 if (strchr(var, '=') == NULL) {
163 *(envp + *envc) = ap_pstrcat(p, var, "=", getenv(var), NULL);
165 else {
166 *(envp + *envc) = var;
169 (*envc)++;
171 return NULL;
174 /*******************************************************************************
175 * Get the next configuration directive argument, & add it to an env array.
176 * The pool arg should be permanent storage.
178 static const char *get_env_var(pool *p, const char **arg, char **envp, unsigned int *envc)
180 char * const val = ap_getword_conf(p, arg);
182 if (*val == '\0') {
183 return "\"\"";
186 return fcgi_config_set_env_var(p, envp, envc, val);
189 static const char *get_pass_header(pool *p, const char **arg, array_header **array)
191 const char **header;
193 if (!*array) {
194 *array = ap_make_array(p, 10, sizeof(char*));
197 header = (const char **)ap_push_array(*array);
198 *header = ap_getword_conf(p, arg);
200 return header ? NULL : "\"\"";
203 /*******************************************************************************
204 * Return a "standard" message for common configuration errors.
206 static const char *invalid_value(pool *p, const char *cmd, const char *id,
207 const char *opt, const char *err)
209 return ap_psprintf(p, "%s%s%s: invalid value for %s: %s",
210 cmd, id ? " " : "", id ? id : "", opt, err);
213 /*******************************************************************************
214 * Set/Reset the uid/gid that Apache and the PM will run as. This is ap_user_id
215 * and ap_group_id if we're started as root, and euid/egid otherwise. Also try
216 * to check that the config files don't set the User/Group after a FastCGI
217 * directive is used that depends on it.
219 /*@@@ To be complete, we should save a handle to the server each AppClass is
220 * configured in and at init() check that the user/group is still what we
221 * thought it was. Also the other directives should only be allowed in the
222 * parent Apache server.
224 const char *fcgi_config_set_fcgi_uid_n_gid(int set)
226 static int isSet = 0;
228 #ifndef WIN32
230 uid_t uid = geteuid();
231 gid_t gid = getegid();
233 if (set == 0) {
234 isSet = 0;
235 fcgi_user_id = (uid_t)-1;
236 fcgi_group_id = (gid_t)-1;
237 return NULL;
240 if (uid == 0) {
241 uid = ap_user_id;
244 if (gid == 0) {
245 gid = ap_group_id;
248 if (isSet && (uid != fcgi_user_id || gid != fcgi_group_id)) {
249 return "User/Group commands must preceed FastCGI server definitions";
252 isSet = 1;
253 fcgi_user_id = uid;
254 fcgi_group_id = gid;
256 #endif /* !WIN32 */
258 return NULL;
261 apcb_t fcgi_config_reset_globals(void* dummy)
263 fcgi_config_pool = NULL;
264 fcgi_servers = NULL;
265 fcgi_config_set_fcgi_uid_n_gid(0);
266 fcgi_wrapper = NULL;
267 fcgi_socket_dir = NULL;
269 fcgi_dynamic_total_proc_count = 0;
270 fcgi_dynamic_epoch = 0;
271 fcgi_dynamic_last_analyzed = 0;
273 dynamicMaxProcs = FCGI_DEFAULT_MAX_PROCS;
274 dynamicMinProcs = FCGI_DEFAULT_MIN_PROCS;
275 dynamicMaxClassProcs = FCGI_DEFAULT_MAX_CLASS_PROCS;
276 dynamicKillInterval = FCGI_DEFAULT_KILL_INTERVAL;
277 dynamicUpdateInterval = FCGI_DEFAULT_UPDATE_INTERVAL;
278 dynamicGain = FCGI_DEFAULT_GAIN;
279 dynamicThreshold1 = FCGI_DEFAULT_THRESHOLD_1;
280 dynamicThresholdN = FCGI_DEFAULT_THRESHOLD_N;
281 dynamicPleaseStartDelay = FCGI_DEFAULT_START_PROCESS_DELAY;
282 dynamicAppConnectTimeout = FCGI_DEFAULT_APP_CONN_TIMEOUT;
283 dynamicEnvp = &fcgi_empty_env;
284 dynamicProcessSlack = FCGI_DEFAULT_PROCESS_SLACK;
285 dynamicAutoRestart = FCGI_DEFAULT_RESTART_DYNAMIC;
286 dynamicAutoUpdate = FCGI_DEFAULT_AUTOUPDATE;
287 dynamicListenQueueDepth = FCGI_DEFAULT_LISTEN_Q;
288 dynamicInitStartDelay = DEFAULT_INIT_START_DELAY;
289 dynamicRestartDelay = FCGI_DEFAULT_RESTART_DELAY;
290 dynamic_pass_headers = NULL;
291 dynamic_idle_timeout = FCGI_DEFAULT_IDLE_TIMEOUT;
292 dynamicFlush = FCGI_FLUSH;
294 #ifndef WIN32
295 /* Close any old pipe (HUP/USR1) */
296 if (fcgi_pm_pipe[0] != -1) {
297 close(fcgi_pm_pipe[0]);
298 fcgi_pm_pipe[0] = -1;
300 if (fcgi_pm_pipe[1] != -1) {
301 close(fcgi_pm_pipe[1]);
302 fcgi_pm_pipe[1] = -1;
304 #endif
306 return APCB_OK;
309 /*******************************************************************************
310 * Create a directory to hold Unix/Domain sockets.
312 const char *fcgi_config_make_dir(pool *tp, char *path)
314 struct stat finfo;
315 const char *err = NULL;
317 /* Is the directory spec'd correctly */
318 if (*path != '/') {
319 return "path is not absolute (it must start with a \"/\")";
321 else {
322 int i = strlen(path) - 1;
324 /* Strip trailing "/"s */
325 while(i > 0 && path[i] == '/') path[i--] = '\0';
328 /* Does it exist? */
329 if (stat(path, &finfo) != 0) {
330 /* No, but maybe we can create it */
331 #ifdef WIN32
332 if (mkdir(path) != 0)
333 #else
334 if (mkdir(path, S_IRWXU) != 0)
335 #endif
337 return ap_psprintf(tp,
338 "doesn't exist and can't be created: %s",
339 strerror(errno));
342 #ifndef WIN32
343 /* If we're root, we're gonna setuid/setgid so we need to chown */
344 if (geteuid() == 0 && chown(path, ap_user_id, ap_group_id) != 0) {
345 return ap_psprintf(tp,
346 "can't chown() to the server (uid %ld, gid %ld): %s",
347 (long)ap_user_id, (long)ap_group_id, strerror(errno));
349 #endif
351 else {
352 /* Yes, is it a directory? */
353 if (!S_ISDIR(finfo.st_mode))
354 return "isn't a directory!";
356 /* Can we RWX in there? */
357 #ifdef WIN32
358 err = fcgi_util_check_access(tp, NULL, &finfo, _S_IREAD | _S_IWRITE | _S_IEXEC, fcgi_user_id, fcgi_group_id);
359 #else
360 err = fcgi_util_check_access(tp, NULL, &finfo, R_OK | W_OK | X_OK,
361 fcgi_user_id, fcgi_group_id);
362 #endif
363 if (err != NULL) {
364 return ap_psprintf(tp,
365 "access for server (uid %ld, gid %ld) failed: %s",
366 (long)fcgi_user_id, (long)fcgi_group_id, err);
369 return NULL;
372 /*******************************************************************************
373 * Create a "dynamic" subdirectory. If the directory
374 * already exists we don't mess with it unless 'wax' is set.
376 const char *fcgi_config_make_dynamic_dir(pool *p, const int wax)
378 #ifndef WIN32
379 const char *err;
380 pool *tp;
382 fcgi_dynamic_dir = ap_pstrcat(p, fcgi_socket_dir, "/dynamic", NULL);
384 if ((err = fcgi_config_make_dir(p, fcgi_dynamic_dir)))
385 return ap_psprintf(p, "can't create dynamic directory \"%s\": %s", fcgi_dynamic_dir, err);
387 /* Don't step on a running server unless its OK. */
388 if (!wax)
389 return NULL;
391 #ifdef APACHE2
393 apr_dir_t * dir;
394 apr_finfo_t finfo;
396 if (apr_pool_create(&tp, p))
397 return "apr_pool_create() failed";
399 if (apr_dir_open(&dir, fcgi_dynamic_dir, tp))
400 return "apr_dir_open() failed";
402 /* delete the contents */
404 while (apr_dir_read(&finfo, APR_FINFO_NAME, dir) == APR_SUCCESS)
406 if (strcmp(finfo.name, ".") == 0 || strcmp(finfo.name, "..") == 0)
407 continue;
409 apr_file_remove(finfo.name, tp);
413 #else /* !APACHE2 */
415 DIR *dp;
416 struct dirent *dirp = NULL;
418 tp = ap_make_sub_pool(p);
420 dp = ap_popendir(tp, fcgi_dynamic_dir);
421 if (dp == NULL) {
422 ap_destroy_pool(tp);
423 return ap_psprintf(p, "can't open dynamic directory \"%s\": %s",
424 fcgi_dynamic_dir, strerror(errno));
427 /* delete the contents */
429 while ((dirp = readdir(dp)) != NULL)
431 if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
432 continue;
434 unlink(ap_pstrcat(tp, fcgi_dynamic_dir, "/", dirp->d_name, NULL));
438 #endif /* !APACHE2 */
440 ap_destroy_pool(tp);
442 #else
443 fcgi_dynamic_dir = ap_pstrcat(p, fcgi_socket_dir, "dynamic", NULL);
444 #endif
445 return NULL;
449 /*******************************************************************************
450 * Change the directory used for the Unix/Domain sockets from the default.
451 * Create the directory and the "dynamic" subdirectory.
453 const char *fcgi_config_set_socket_dir(cmd_parms *cmd, void *dummy, const char *arg)
455 pool * const tp = cmd->temp_pool;
456 const char * const name = cmd->cmd->name;
457 const char *err;
458 char * arg_nc;
460 if (fcgi_socket_dir) {
461 return ap_psprintf(tp, "%s %s: already defined as \"%s\"",
462 name, arg, fcgi_socket_dir);
465 err = fcgi_config_set_fcgi_uid_n_gid(1);
466 if (err != NULL)
467 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
469 if (fcgi_servers != NULL) {
470 return ap_psprintf(tp,
471 "The %s command must preceed static FastCGI server definitions",
472 name);
475 arg_nc = ap_pstrdup(cmd->pool, arg);
477 #ifndef WIN32
479 #ifdef APACHE2
480 if (apr_filepath_merge(&arg_nc, "", arg, 0, cmd->pool))
481 return ap_psprintf(tp, "%s %s: invalid filepath", name, arg);
482 #else
483 arg_nc = ap_os_canonical_filename(cmd->pool, arg_nc);
484 #endif
486 arg_nc = ap_server_root_relative(cmd->pool, arg_nc);
488 #else /* WIN32 */
490 if (strncmp(arg_nc, "\\\\.\\pipe\\", 9) != 0)
491 return ap_psprintf(tp, "%s %s is invalid format",name, arg_nc);
493 #endif
495 fcgi_socket_dir = arg_nc;
497 #ifndef WIN32
498 err = fcgi_config_make_dir(tp, fcgi_socket_dir);
499 if (err != NULL)
500 return ap_psprintf(tp, "%s %s: %s", name, arg_nc, err);
502 err = fcgi_config_make_dynamic_dir(cmd->pool, 0);
503 if (err != NULL)
504 return ap_psprintf(tp, "%s %s: %s", name, arg_nc, err);
505 #endif
507 return NULL;
510 /*******************************************************************************
511 * Enable, disable, or specify the path to a wrapper used to invoke all
512 * FastCGI applications.
514 const char *fcgi_config_set_wrapper(cmd_parms *cmd, void *dummy, const char *arg)
516 #ifdef WIN32
517 return ap_psprintf(cmd->temp_pool,
518 "the %s directive is not supported on WIN", cmd->cmd->name);
519 #else
521 const char *err = NULL;
522 const char * const name = cmd->cmd->name;
523 pool * const tp = cmd->temp_pool;
524 char * wrapper;
526 if (!ap_suexec_enabled && strcasecmp(arg, "On") == 0) {
527 fprintf(stderr, "Warning: \"%s On\" requires SUEXEC be enabled in Apache", name);
528 return NULL;
531 err = fcgi_config_set_fcgi_uid_n_gid(1);
532 if (err != NULL)
533 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
535 if (fcgi_servers != NULL) {
536 return ap_psprintf(tp,
537 "The %s command must preceed static FastCGI server definitions", name);
540 if (strcasecmp(arg, "On") == 0) {
541 fcgi_wrapper = SUEXEC_BIN;
543 else if (strcasecmp(arg, "Off") == 0) {
544 fcgi_wrapper = NULL;
546 else {
547 #ifdef APACHE2
548 if (apr_filepath_merge((char **) &arg, "", arg, 0, cmd->pool))
549 return ap_psprintf(tp, "%s %s: invalid filepath", name, arg);
550 #else
551 wrapper = ap_os_canonical_filename(cmd->pool, (char *) arg);
552 #endif
554 wrapper = ap_server_root_relative(cmd->pool, wrapper);
556 err = fcgi_util_check_access(tp, wrapper, NULL, X_OK, fcgi_user_id, fcgi_group_id);
558 if (err != NULL) {
559 return ap_psprintf(tp,
560 "%s: \"%s\" access for server (uid %ld, gid %ld) failed: %s",
561 name, wrapper, (long)fcgi_user_id, (long)fcgi_group_id, err);
564 fcgi_wrapper = wrapper;
566 return NULL;
567 #endif /* !WIN32 */
570 /*******************************************************************************
571 * Configure a static FastCGI server.
573 const char *fcgi_config_new_static_server(cmd_parms *cmd, void *dummy, const char *arg)
575 fcgi_server *s;
576 pool *p = cmd->pool, *tp = cmd->temp_pool;
577 const char *name = cmd->cmd->name;
578 char *fs_path = ap_getword_conf(p, &arg);
579 const char *option, *err;
581 /* Allocate temp storage for the array of initial environment variables */
582 char **envp = ap_pcalloc(tp, sizeof(char *) * (MAX_INIT_ENV_VARS + 3));
583 unsigned int envc = 0;
585 #ifdef WIN32
586 HANDLE mutex;
587 #endif
589 if (*fs_path == '\0')
590 return "AppClass requires a pathname!?";
592 if ((err = fcgi_config_set_fcgi_uid_n_gid(1)) != NULL)
593 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
595 #ifdef APACHE2
596 if (apr_filepath_merge(&fs_path, "", fs_path, 0, p))
597 return ap_psprintf(tp, "%s %s: invalid filepath", name, fs_path);
598 #else
599 fs_path = ap_os_canonical_filename(p, fs_path);
600 #endif
601 fs_path = ap_server_root_relative(p, fs_path);
603 ap_getparents(fs_path);
604 ap_no2slash(fs_path);
606 /* See if we've already got one of these configured */
607 s = fcgi_util_fs_get_by_id(fs_path, fcgi_util_get_server_uid(cmd->server),
608 fcgi_util_get_server_gid(cmd->server));
609 if (s != NULL) {
610 if (fcgi_wrapper) {
611 return ap_psprintf(tp,
612 "%s: redefinition of a previously defined FastCGI "
613 "server \"%s\" with uid=%ld and gid=%ld",
614 name, fs_path, (long) fcgi_util_get_server_uid(cmd->server),
615 (long) fcgi_util_get_server_gid(cmd->server));
617 else {
618 return ap_psprintf(tp,
619 "%s: redefinition of a previously defined FastCGI server \"%s\"",
620 name, fs_path);
624 err = fcgi_util_fs_is_path_ok(tp, fs_path, NULL);
625 if (err != NULL) {
626 return ap_psprintf(tp, "%s: \"%s\" %s", name, fs_path, err);
629 s = fcgi_util_fs_new(p);
630 s->fs_path = fs_path;
631 s->directive = APP_CLASS_STANDARD;
632 s->restartOnExit = TRUE;
633 s->numProcesses = 1;
635 #ifdef WIN32
637 // TCP FastCGI applications require SystemRoot be present in the environment
638 // Put it in both for consistency to the application
639 fcgi_config_set_env_var(tp, envp, &envc, "SystemRoot");
641 mutex = CreateMutex(NULL, FALSE, fs_path);
643 if (mutex == NULL)
645 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
646 "FastCGI: CreateMutex() failed");
647 return "failed to create FastCGI application accept mutex";
650 SetHandleInformation(mutex, HANDLE_FLAG_INHERIT, TRUE);
652 s->mutex_env_string = ap_psprintf(p, "_FCGI_MUTEX_=%ld", mutex);
654 #endif
656 /* Parse directive arguments */
657 while (*arg) {
658 option = ap_getword_conf(tp, &arg);
660 if (strcasecmp(option, "-processes") == 0) {
661 if ((err = get_u_int(tp, &arg, &s->numProcesses, 1)))
662 return invalid_value(tp, name, fs_path, option, err);
664 else if (strcasecmp(option, "-restart-delay") == 0) {
665 if ((err = get_u_int(tp, &arg, &s->restartDelay, 0)))
666 return invalid_value(tp, name, fs_path, option, err);
668 else if (strcasecmp(option, "-init-start-delay") == 0) {
669 if ((err = get_int(tp, &arg, &s->initStartDelay, 0)))
670 return invalid_value(tp, name, fs_path, option, err);
672 else if (strcasecmp(option, "-priority") == 0) {
673 if ((err = get_u_int(tp, &arg, &s->processPriority, 0)))
674 return invalid_value(tp, name, fs_path, option, err);
676 else if (strcasecmp(option, "-listen-queue-depth") == 0) {
677 if ((err = get_u_int(tp, &arg, &s->listenQueueDepth, 1)))
678 return invalid_value(tp, name, fs_path, option, err);
680 else if (strcasecmp(option, "-appConnTimeout") == 0) {
681 if ((err = get_u_int(tp, &arg, &s->appConnectTimeout, 0)))
682 return invalid_value(tp, name, fs_path, option, err);
684 else if (strcasecmp(option, "-idle-timeout") == 0) {
685 if ((err = get_u_int(tp, &arg, &s->idle_timeout, 1)))
686 return invalid_value(tp, name, fs_path, option, err);
688 else if (strcasecmp(option, "-port") == 0) {
689 if ((err = get_u_short(tp, &arg, &s->port, 1)))
690 return invalid_value(tp, name, fs_path, option, err);
692 else if (strcasecmp(option, "-socket") == 0) {
693 s->socket_path = ap_getword_conf(tp, &arg);
694 if (*s->socket_path == '\0')
695 return invalid_value(tp, name, fs_path, option, "\"\"");
697 else if (strcasecmp(option, "-initial-env") == 0) {
698 if ((err = get_env_var(p, &arg, envp, &envc)))
699 return invalid_value(tp, name, fs_path, option, err);
701 else if (strcasecmp(option, "-pass-header") == 0) {
702 if ((err = get_pass_header(p, &arg, &s->pass_headers)))
703 return invalid_value(tp, name, fs_path, option, err);
705 else if (strcasecmp(option, "-flush") == 0) {
706 s->flush = 1;
708 else if (strcasecmp(option, "-user") == 0) {
709 #ifdef WIN32
710 return ap_psprintf(tp,
711 "%s %s: the -user option isn't supported on WIN", name, fs_path);
712 #else
713 s->username = ap_getword_conf(tp, &arg);
714 if (*s->user == '\0')
715 return invalid_value(tp, name, fs_path, option, "\"\"");
716 #endif
718 else if (strcasecmp(option, "-group") == 0) {
719 #ifdef WIN32
720 return ap_psprintf(tp,
721 "%s %s: the -group option isn't supported on WIN", name, fs_path);
722 #else
723 s->group = ap_getword_conf(tp, &arg);
724 if (*s->group == '\0')
725 return invalid_value(tp, name, fs_path, option, "\"\"");
726 #endif
728 else {
729 return ap_psprintf(tp, "%s %s: invalid option: %s", name, fs_path, option);
731 } /* while */
733 #ifndef WIN32
734 if (s->user)
736 if (s->group == NULL)
738 return ap_psprintf(tp,
739 "%s %s: -user and -group must be used together", name, fs_path);
742 s->uid = ap_uname2id(s->user);
743 s->gid = ap_gname2id(s->group);
745 else
747 if (s->group)
749 return ap_psprintf(tp,
750 "%s %s: -user and -group must be used together", name, fs_path);
753 s->uid = fcgi_util_get_server_uid(cmd->server);
754 s->gid = fcgi_util_get_server_gid(cmd->server);
757 if ((err = fcgi_util_fs_set_uid_n_gid(p, s, s->uid, s->gid)))
759 return ap_psprintf(tp,
760 "%s %s: invalid user or group: %s", name, fs_path, err);
762 #endif /* !WIN32 */
764 if (s->socket_path != NULL && s->port != 0) {
765 return ap_psprintf(tp,
766 "%s %s: -port and -socket are mutually exclusive options",
767 name, fs_path);
770 /* Move env array to a surviving pool */
771 s->envp = (char **)ap_pcalloc(p, sizeof(char *) * (envc + 4));
772 memcpy(s->envp, envp, sizeof(char *) * envc);
774 /* Initialize process structs */
775 s->procs = fcgi_util_fs_create_procs(p, s->numProcesses);
777 /* Build the appropriate sockaddr structure */
778 if (s->port != 0) {
779 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->socket_addr,
780 &s->socket_addr_len, NULL, s->port);
781 if (err != NULL)
782 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
783 #ifdef WIN32
784 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->dest_addr,
785 &s->socket_addr_len, "localhost", s->port);
786 if (err != NULL)
787 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
788 #endif
789 } else {
790 if (s->socket_path == NULL)
791 s->socket_path = fcgi_util_socket_hash_filename(tp, fs_path, s->user, s->group);
792 s->socket_path = fcgi_util_socket_make_path_absolute(p, s->socket_path, 0);
793 #ifndef WIN32
794 err = fcgi_util_socket_make_domain_addr(p, (struct sockaddr_un **)&s->socket_addr,
795 &s->socket_addr_len, s->socket_path);
796 if (err != NULL)
797 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
798 #endif
801 /* Add it to the list of FastCGI servers */
802 fcgi_util_fs_add(s);
804 return NULL;
807 /*******************************************************************************
808 * Configure a static FastCGI server that is started/managed elsewhere.
810 const char *fcgi_config_new_external_server(cmd_parms *cmd, void *dummy, const char *arg)
812 fcgi_server *s;
813 pool * const p = cmd->pool, *tp = cmd->temp_pool;
814 const char * const name = cmd->cmd->name;
815 char *fs_path = ap_getword_conf(p, &arg);
816 const char *option, *err;
818 if (!*fs_path) {
819 return ap_pstrcat(tp, name, " requires a path and either a -socket or -host option", NULL);
822 #ifdef APACHE2
823 if (apr_filepath_merge(&fs_path, "", fs_path, 0, p))
824 return ap_psprintf(tp, "%s %s: invalid filepath", name, fs_path);
825 #else
826 fs_path = ap_os_canonical_filename(p, fs_path);
827 #endif
829 fs_path = ap_server_root_relative(p, fs_path);
831 ap_getparents(fs_path);
832 ap_no2slash(fs_path);
834 /* See if we've already got one of these bettys configured */
835 s = fcgi_util_fs_get_by_id(fs_path, fcgi_util_get_server_uid(cmd->server),
836 fcgi_util_get_server_gid(cmd->server));
837 if (s != NULL) {
838 if (fcgi_wrapper) {
839 return ap_psprintf(tp,
840 "%s: redefinition of a previously defined class \"%s\" "
841 "with uid=%ld and gid=%ld",
842 name, fs_path, (long) fcgi_util_get_server_uid(cmd->server),
843 (long) fcgi_util_get_server_gid(cmd->server));
845 else
847 return ap_psprintf(tp,
848 "%s: redefinition of previously defined class \"%s\"", name, fs_path);
852 s = fcgi_util_fs_new(p);
853 s->fs_path = fs_path;
854 s->directive = APP_CLASS_EXTERNAL;
856 /* Parse directive arguments */
857 while (*arg != '\0') {
858 option = ap_getword_conf(tp, &arg);
860 if (strcasecmp(option, "-host") == 0) {
861 if ((err = get_host_n_port(p, &arg, &s->host, &s->port)))
862 return invalid_value(tp, name, fs_path, option, err);
864 else if (strcasecmp(option, "-socket") == 0) {
865 s->socket_path = ap_getword_conf(tp, &arg);
866 if (*s->socket_path == '\0')
867 return invalid_value(tp, name, fs_path, option, "\"\"");
869 else if (strcasecmp(option, "-appConnTimeout") == 0) {
870 if ((err = get_u_int(tp, &arg, &s->appConnectTimeout, 0)))
871 return invalid_value(tp, name, fs_path, option, err);
873 else if (strcasecmp(option, "-idle-timeout") == 0) {
874 if ((err = get_u_int(tp, &arg, &s->idle_timeout, 1)))
875 return invalid_value(tp, name, fs_path, option, err);
877 else if (strcasecmp(option, "-pass-header") == 0) {
878 if ((err = get_pass_header(p, &arg, &s->pass_headers)))
879 return invalid_value(tp, name, fs_path, option, err);
881 else if (strcasecmp(option, "-flush") == 0) {
882 s->flush = 1;
884 else if (strcasecmp(option, "-user") == 0) {
885 #ifdef WIN32
886 return ap_psprintf(tp,
887 "%s %s: the -user option isn't supported on WIN", name, fs_path);
888 #else
889 s->username = ap_getword_conf(tp, &arg);
890 if (*s->user == '\0')
891 return invalid_value(tp, name, fs_path, option, "\"\"");
892 #endif
894 else if (strcasecmp(option, "-group") == 0) {
895 #ifdef WIN32
896 return ap_psprintf(tp,
897 "%s %s: the -group option isn't supported on WIN", name, fs_path);
898 #else
899 s->group = ap_getword_conf(tp, &arg);
900 if (*s->group == '\0')
901 return invalid_value(tp, name, fs_path, option, "\"\"");
902 #endif
904 else {
905 return ap_psprintf(tp, "%s %s: invalid option: %s", name, fs_path, option);
907 } /* while */
909 #ifndef WIN32
910 if (s->user)
912 if (s->group == NULL)
914 return ap_psprintf(tp,
915 "%s %s: -user and -group must be used together", name, fs_path);
918 s->uid = ap_uname2id(s->user);
919 s->gid = ap_gname2id(s->group);
921 else
923 if (s->group)
925 return ap_psprintf(tp,
926 "%s %s: -user and -group must be used together", name, fs_path);
929 s->uid = fcgi_util_get_server_uid(cmd->server);
930 s->gid = fcgi_util_get_server_gid(cmd->server);
933 if ((err = fcgi_util_fs_set_uid_n_gid(p, s, s->uid, s->gid)))
935 return ap_psprintf(tp,
936 "%s %s: invalid user or group: %s", name, fs_path, err);
938 #endif /* !WIN32 */
940 /* Require one of -socket or -host, but not both */
941 if (s->socket_path != NULL && s->port != 0) {
942 return ap_psprintf(tp,
943 "%s %s: -host and -socket are mutually exclusive options",
944 name, fs_path);
946 if (s->socket_path == NULL && s->port == 0) {
947 return ap_psprintf(tp,
948 "%s %s: -socket or -host option missing", name, fs_path);
951 /* Build the appropriate sockaddr structure */
952 if (s->port != 0) {
953 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->socket_addr,
954 &s->socket_addr_len, s->host, s->port);
955 if (err != NULL)
956 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
957 } else {
958 s->socket_path = fcgi_util_socket_make_path_absolute(p, s->socket_path, 0);
959 #ifndef WIN32
960 err = fcgi_util_socket_make_domain_addr(p, (struct sockaddr_un **)&s->socket_addr,
961 &s->socket_addr_len, s->socket_path);
962 if (err != NULL)
963 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
964 #endif
967 /* Add it to the list of FastCGI servers */
968 fcgi_util_fs_add(s);
970 return NULL;
974 *----------------------------------------------------------------------
976 * fcgi_config_set_config --
978 * Implements the FastCGI FCGIConfig configuration directive.
979 * This command adds routines to control the execution of the
980 * dynamic FastCGI processes.
983 *----------------------------------------------------------------------
985 const char *fcgi_config_set_config(cmd_parms *cmd, void *dummy, const char *arg)
987 pool * const p = cmd->pool;
988 pool * const tp = cmd->temp_pool;
989 const char *err, *option;
990 const char * const name = cmd->cmd->name;
992 /* Allocate temp storage for an initial environment */
993 unsigned int envc = 0;
994 char **envp = (char **)ap_pcalloc(tp, sizeof(char *) * (MAX_INIT_ENV_VARS + 3));
996 /* Parse the directive arguments */
997 while (*arg) {
998 option = ap_getword_conf(tp, &arg);
1000 if (strcasecmp(option, "-maxProcesses") == 0) {
1001 if ((err = get_u_int(tp, &arg, &dynamicMaxProcs, 1)))
1002 return invalid_value(tp, name, NULL, option, err);
1004 else if (strcasecmp(option, "-minProcesses") == 0) {
1005 if ((err = get_int(tp, &arg, &dynamicMinProcs, 0)))
1006 return invalid_value(tp, name, NULL, option, err);
1008 else if (strcasecmp(option, "-maxClassProcesses") == 0) {
1009 if ((err = get_int(tp, &arg, &dynamicMaxClassProcs, 1)))
1010 return invalid_value(tp, name, NULL, option, err);
1012 else if (strcasecmp(option, "-killInterval") == 0) {
1013 if ((err = get_u_int(tp, &arg, &dynamicKillInterval, 1)))
1014 return invalid_value(tp, name, NULL, option, err);
1016 else if (strcasecmp(option, "-updateInterval") == 0) {
1017 if ((err = get_u_int(tp, &arg, &dynamicUpdateInterval, 1)))
1018 return invalid_value(tp, name, NULL, option, err);
1020 else if (strcasecmp(option, "-gainValue") == 0) {
1021 if ((err = get_float(tp, &arg, &dynamicGain, 0.0, 1.0)))
1022 return invalid_value(tp, name, NULL, option, err);
1024 else if ((strcasecmp(option, "-singleThreshold") == 0)
1025 || (strcasecmp(option, "-singleThreshhold") == 0))
1027 if ((err = get_int(tp, &arg, &dynamicThreshold1, 0)))
1028 return invalid_value(tp, name, NULL, option, err);
1030 else if ((strcasecmp(option, "-multiThreshold") == 0)
1031 || (strcasecmp(option, "-multiThreshhold") == 0))
1033 if ((err = get_int(tp, &arg, &dynamicThresholdN, 0)))
1034 return invalid_value(tp, name, NULL, option, err);
1036 else if (strcasecmp(option, "-startDelay") == 0) {
1037 if ((err = get_u_int(tp, &arg, &dynamicPleaseStartDelay, 1)))
1038 return invalid_value(tp, name, NULL, option, err);
1040 else if (strcasecmp(option, "-initial-env") == 0) {
1041 if ((err = get_env_var(p, &arg, envp, &envc)))
1042 return invalid_value(tp, name, NULL, option, err);
1044 else if (strcasecmp(option, "-pass-header") == 0) {
1045 if ((err = get_pass_header(p, &arg, &dynamic_pass_headers)))
1046 return invalid_value(tp, name, NULL, option, err);
1048 else if (strcasecmp(option, "-appConnTimeout") == 0) {
1049 if ((err = get_u_int(tp, &arg, &dynamicAppConnectTimeout, 0)))
1050 return invalid_value(tp, name, NULL, option, err);
1052 else if (strcasecmp(option, "-idle-timeout") == 0) {
1053 if ((err = get_u_int(tp, &arg, &dynamic_idle_timeout, 1)))
1054 return invalid_value(tp, name, NULL, option, err);
1056 else if (strcasecmp(option, "-listen-queue-depth") == 0) {
1057 if ((err = get_u_int(tp, &arg, &dynamicListenQueueDepth, 1)))
1058 return invalid_value(tp, name, NULL, option, err);
1060 else if (strcasecmp(option, "-restart-delay") == 0) {
1061 if ((err = get_u_int(tp, &arg, &dynamicRestartDelay, 0)))
1062 return invalid_value(tp, name, NULL, option, err);
1064 else if (strcasecmp(option, "-init-start-delay") == 0) {
1065 if ((err = get_u_int(tp, &arg, &dynamicInitStartDelay, 0)))
1066 return invalid_value(tp, name, NULL, option, err);
1068 else if (strcasecmp(option, "-processSlack") == 0) {
1069 if ((err = get_u_int(tp, &arg, &dynamicProcessSlack, 1)))
1070 return invalid_value(tp, name, NULL, option, err);
1072 else if (strcasecmp(option, "-restart") == 0) {
1073 dynamicAutoRestart = 1;
1075 else if (strcasecmp(option, "-autoUpdate") == 0) {
1076 dynamicAutoUpdate = 1;
1078 else if (strcasecmp(option, "-flush") == 0) {
1079 dynamicFlush = TRUE;
1081 else {
1082 return ap_psprintf(tp, "%s: invalid option: %s", name, option);
1084 } /* while */
1086 if (dynamicProcessSlack >= dynamicMaxProcs + 1) {
1087 /* the kill policy would work unexpectedly */
1088 return ap_psprintf(tp,
1089 "%s: processSlack (%u) must be less than maxProcesses (%u) + 1",
1090 name, dynamicProcessSlack, dynamicMaxProcs);
1093 /* Move env array to a surviving pool, leave 2 extra slots for
1094 * WIN32 _FCGI_MUTEX_ and _FCGI_SHUTDOWN_EVENT_ */
1095 dynamicEnvp = (char **)ap_pcalloc(p, sizeof(char *) * (envc + 4));
1096 memcpy(dynamicEnvp, envp, sizeof(char *) * envc);
1098 return NULL;
1101 void *fcgi_config_create_dir_config(pool *p, char *dummy)
1103 fcgi_dir_config *dir_config = ap_pcalloc(p, sizeof(fcgi_dir_config));
1105 dir_config->authenticator_options = FCGI_AUTHORITATIVE;
1106 dir_config->authorizer_options = FCGI_AUTHORITATIVE;
1107 dir_config->access_checker_options = FCGI_AUTHORITATIVE;
1109 return dir_config;
1113 const char *fcgi_config_new_auth_server(cmd_parms * cmd,
1114 void * dircfg, const char *fs_path, const char * compat)
1116 fcgi_dir_config * dir_config = (fcgi_dir_config *) dircfg;
1117 pool * const tp = cmd->temp_pool;
1118 char * auth_server;
1120 #ifdef APACHE2
1121 if (apr_filepath_merge(&auth_server, "", fs_path, 0, cmd->pool))
1122 return ap_psprintf(tp, "%s %s: invalid filepath", cmd->cmd->name, fs_path);
1123 #else
1124 auth_server = (char *) ap_os_canonical_filename(cmd->pool, fs_path);
1125 #endif
1127 auth_server = ap_server_root_relative(cmd->pool, auth_server);
1129 /* Make sure its already configured or at least a candidate for dynamic */
1130 if (fcgi_util_fs_get_by_id(auth_server, fcgi_util_get_server_uid(cmd->server),
1131 fcgi_util_get_server_gid(cmd->server)) == NULL)
1133 const char *err = fcgi_util_fs_is_path_ok(tp, auth_server, NULL);
1134 if (err)
1135 return ap_psprintf(tp, "%s: \"%s\" %s", cmd->cmd->name, auth_server, err);
1138 if (compat && strcasecmp(compat, "-compat"))
1139 return ap_psprintf(cmd->temp_pool, "%s: unknown option: \"%s\"", cmd->cmd->name, compat);
1141 switch((int)cmd->info) {
1142 case FCGI_AUTH_TYPE_AUTHENTICATOR:
1143 dir_config->authenticator = auth_server;
1144 dir_config->authenticator_options |= (compat) ? FCGI_COMPAT : 0;
1145 break;
1146 case FCGI_AUTH_TYPE_AUTHORIZER:
1147 dir_config->authorizer = auth_server;
1148 dir_config->authorizer_options |= (compat) ? FCGI_COMPAT : 0;
1149 break;
1150 case FCGI_AUTH_TYPE_ACCESS_CHECKER:
1151 dir_config->access_checker = auth_server;
1152 dir_config->access_checker_options |= (compat) ? FCGI_COMPAT : 0;
1153 break;
1156 return NULL;
1159 const char *fcgi_config_set_authoritative_slot(cmd_parms * cmd,
1160 void * dir_config, int arg)
1162 int offset = (int)(long)cmd->info;
1164 if (arg)
1165 *((u_char *)dir_config + offset) |= FCGI_AUTHORITATIVE;
1166 else
1167 *((u_char *)dir_config + offset) &= ~FCGI_AUTHORITATIVE;
1169 return NULL;