Put in links to suexec and cgiwrap.
[mod_fastcgi.git] / fcgi_config.c
blob6becdbb1b7fe11bd500a4f093dfa7fd41a94878d
1 /*
2 * $Id: fcgi_config.c,v 1.28 2001/05/03 20:51:24 robs Exp $
3 */
5 #include "fcgi.h"
7 #ifdef WIN32
8 #pragma warning( disable : 4100 4706 )
9 #endif
11 /*******************************************************************************
12 * Get the next configuration directive argument, & return an in_addr and port.
13 * The arg must be in the form "host:port" where host can be an IP or hostname.
14 * The pool arg should be persistant storage.
16 static const char *get_host_n_port(pool *p, const char **arg,
17 const char **host, u_short *port)
19 char *cvptr, *portStr;
20 long tmp;
22 *host = ap_getword_conf(p, arg);
23 if (**host == '\0')
24 return "\"\"";
26 portStr = strchr(*host, ':');
27 if (portStr == NULL)
28 return "missing port specification";
30 /* Split the host and port portions */
31 *portStr++ = '\0';
33 /* Convert port number */
34 tmp = (u_short) strtol(portStr, &cvptr, 10);
35 if (*cvptr != '\0' || tmp < 1 || tmp > USHRT_MAX)
36 return ap_pstrcat(p, "bad port number \"", portStr, "\"", NULL);
38 *port = (unsigned short) tmp;
40 return NULL;
43 /*******************************************************************************
44 * Get the next configuration directive argument, & return an u_short.
45 * The pool arg should be temporary storage.
47 static const char *get_u_short(pool *p, const char **arg,
48 u_short *num, u_short min)
50 char *ptr;
51 long tmp;
52 const char *txt = ap_getword_conf(p, arg);
54 if (*txt == '\0') {
55 return "\"\"";
58 tmp = strtol(txt, &ptr, 10);
60 if (*ptr != '\0') {
61 return ap_pstrcat(p, "\"", txt, "\" must be a positive integer", NULL);
64 if (tmp < min || tmp > USHRT_MAX) {
65 return ap_psprintf(p, "\"%u\" must be >= %u and < %u", *num, min, USHRT_MAX);
68 *num = (u_short) tmp;
70 return NULL;
73 static const char *get_int(pool *p, const char **arg, int *num, int min)
75 char *cp;
76 const char *val = ap_getword_conf(p, arg);
78 if (*val == '\0')
80 return "\"\"";
83 *num = (int) strtol(val, &cp, 10);
85 if (*cp != '\0')
87 return ap_pstrcat(p, "can't parse ", "\"", val, "\"", NULL);
89 else if (*num < min)
91 return ap_psprintf(p, "\"%d\" must be >= %d", *num, min);
94 return NULL;
97 /*******************************************************************************
98 * Get the next configuration directive argument, & return an u_int.
99 * The pool arg should be temporary storage.
101 static const char *get_u_int(pool *p, const char **arg,
102 u_int *num, u_int min)
104 char *ptr;
105 const char *val = ap_getword_conf(p, arg);
107 if (*val == '\0')
108 return "\"\"";
109 *num = (u_int)strtol(val, &ptr, 10);
111 if (*ptr != '\0')
112 return ap_pstrcat(p, "\"", val, "\" must be a positive integer", NULL);
113 else if (*num < min)
114 return ap_psprintf(p, "\"%u\" must be >= %u", *num, min);
115 return NULL;
118 /*******************************************************************************
119 * Get the next configuration directive argument, & return a float.
120 * The pool arg should be temporary storage.
122 static const char *get_float(pool *p, const char **arg,
123 float *num, float min, float max)
125 char *ptr;
126 const char *val = ap_getword_conf(p, arg);
128 if (*val == '\0')
129 return "\"\"";
130 *num = (float) strtod(val, &ptr);
132 if (*ptr != '\0')
133 return ap_pstrcat(p, "\"", val, "\" is not a floating point number", NULL);
134 if (*num < min || *num > max)
135 return ap_psprintf(p, "\"%f\" is not between %f and %f", *num, min, max);
136 return NULL;
139 const char *fcgi_config_set_env_var(pool *p, char **envp, unsigned int *envc, char * var)
141 if (*envc >= MAX_INIT_ENV_VARS) {
142 return "too many variables, must be <= MAX_INIT_ENV_VARS";
145 if (strchr(var, '=') == NULL) {
146 *(envp + *envc) = ap_pstrcat(p, var, "=", getenv(var), NULL);
148 else {
149 *(envp + *envc) = var;
152 (*envc)++;
154 return NULL;
157 /*******************************************************************************
158 * Get the next configuration directive argument, & add it to an env array.
159 * The pool arg should be permanent storage.
161 static const char *get_env_var(pool *p, const char **arg, char **envp, unsigned int *envc)
163 char * const val = ap_getword_conf(p, arg);
165 if (*val == '\0') {
166 return "\"\"";
169 return fcgi_config_set_env_var(p, envp, envc, val);
172 static const char *get_pass_header(pool *p, const char **arg, array_header **array)
174 const char **header;
176 if (!*array) {
177 *array = ap_make_array(p, 10, sizeof(char*));
180 header = (const char **)ap_push_array(*array);
181 *header = ap_getword_conf(p, arg);
183 return header ? NULL : "\"\"";
186 /*******************************************************************************
187 * Return a "standard" message for common configuration errors.
189 static const char *invalid_value(pool *p, const char *cmd, const char *id,
190 const char *opt, const char *err)
192 return ap_psprintf(p, "%s%s%s: invalid value for %s: %s",
193 cmd, id ? " " : "", id ? id : "", opt, err);
196 /*******************************************************************************
197 * Set/Reset the uid/gid that Apache and the PM will run as. This is ap_user_id
198 * and ap_group_id if we're started as root, and euid/egid otherwise. Also try
199 * to check that the config files don't set the User/Group after a FastCGI
200 * directive is used that depends on it.
202 /*@@@ To be complete, we should save a handle to the server each AppClass is
203 * configured in and at init() check that the user/group is still what we
204 * thought it was. Also the other directives should only be allowed in the
205 * parent Apache server.
207 const char *fcgi_config_set_fcgi_uid_n_gid(int set)
209 static int isSet = 0;
210 #ifndef WIN32
211 uid_t uid = geteuid();
212 gid_t gid = getegid();
214 if (set == 0) {
215 isSet = 0;
216 fcgi_user_id = (uid_t)-1;
217 fcgi_group_id = (gid_t)-1;
218 return NULL;
221 uid = uid ? uid : ap_user_id;
222 gid = gid ? gid : ap_group_id;
224 if (isSet && (uid != fcgi_user_id || gid != fcgi_group_id)) {
225 return "User/Group commands must preceed FastCGI server definitions";
228 isSet = 1;
229 fcgi_user_id = uid;
230 fcgi_group_id = gid;
231 #endif
232 return NULL;
235 void fcgi_config_reset_globals(void* dummy)
237 fcgi_config_pool = NULL;
238 fcgi_servers = NULL;
239 fcgi_config_set_fcgi_uid_n_gid(0);
240 fcgi_wrapper = NULL;
241 fcgi_socket_dir = DEFAULT_SOCK_DIR;
243 fcgi_dynamic_total_proc_count = 0;
244 fcgi_dynamic_epoch = 0;
245 fcgi_dynamic_last_analyzed = 0;
247 dynamicMaxProcs = FCGI_DEFAULT_MAX_PROCS;
248 dynamicMinProcs = FCGI_DEFAULT_MIN_PROCS;
249 dynamicMaxClassProcs = FCGI_DEFAULT_MAX_CLASS_PROCS;
250 dynamicKillInterval = FCGI_DEFAULT_KILL_INTERVAL;
251 dynamicUpdateInterval = FCGI_DEFAULT_UPDATE_INTERVAL;
252 dynamicGain = FCGI_DEFAULT_GAIN;
253 dynamicThreshold1 = FCGI_DEFAULT_THRESHOLD_1;
254 dynamicThresholdN = FCGI_DEFAULT_THRESHOLD_N;
255 dynamicPleaseStartDelay = FCGI_DEFAULT_START_PROCESS_DELAY;
256 dynamicAppConnectTimeout = FCGI_DEFAULT_APP_CONN_TIMEOUT;
257 dynamicEnvp = &fcgi_empty_env;
258 dynamicProcessSlack = FCGI_DEFAULT_PROCESS_SLACK;
259 dynamicAutoRestart = FCGI_DEFAULT_RESTART_DYNAMIC;
260 dynamicAutoUpdate = FCGI_DEFAULT_AUTOUPDATE;
261 dynamicListenQueueDepth = FCGI_DEFAULT_LISTEN_Q;
262 dynamicInitStartDelay = DEFAULT_INIT_START_DELAY;
263 dynamicRestartDelay = FCGI_DEFAULT_RESTART_DELAY;
264 dynamic_pass_headers = NULL;
265 dynamic_idle_timeout = FCGI_DEFAULT_IDLE_TIMEOUT;
267 #ifndef WIN32
268 /* Close any old pipe (HUP/USR1) */
269 if (fcgi_pm_pipe[0] != -1) {
270 close(fcgi_pm_pipe[0]);
271 fcgi_pm_pipe[0] = -1;
273 if (fcgi_pm_pipe[1] != -1) {
274 close(fcgi_pm_pipe[1]);
275 fcgi_pm_pipe[1] = -1;
277 #endif
280 /*******************************************************************************
281 * Create a directory to hold Unix/Domain sockets.
283 const char *fcgi_config_make_dir(pool *tp, char *path)
285 struct stat finfo;
286 const char *err = NULL;
288 /* Is the directory spec'd correctly */
289 if (*path != '/') {
290 return "path is not absolute (it must start with a \"/\")";
292 else {
293 int i = strlen(path) - 1;
295 /* Strip trailing "/"s */
296 while(i > 0 && path[i] == '/') path[i--] = '\0';
299 /* Does it exist? */
300 if (stat(path, &finfo) != 0) {
301 /* No, but maybe we can create it */
302 #ifdef WIN32
303 if (mkdir(path) != 0)
304 #else
305 if (mkdir(path, S_IRWXU) != 0)
306 #endif
308 return ap_psprintf(tp,
309 "doesn't exist and can't be created: %s",
310 strerror(errno));
313 #ifndef WIN32
314 /* If we're root, we're gonna setuid/setgid so we need to chown */
315 if (geteuid() == 0 && chown(path, ap_user_id, ap_group_id) != 0) {
316 return ap_psprintf(tp,
317 "can't chown() to the server (uid %ld, gid %ld): %s",
318 (long)ap_user_id, (long)ap_group_id, strerror(errno));
320 #endif
322 else {
323 /* Yes, is it a directory? */
324 if (!S_ISDIR(finfo.st_mode))
325 return "isn't a directory!";
327 /* Can we RWX in there? */
328 #ifdef WIN32
329 err = fcgi_util_check_access(tp, NULL, &finfo, _S_IREAD | _S_IWRITE | _S_IEXEC, fcgi_user_id, fcgi_group_id);
330 #else
331 err = fcgi_util_check_access(tp, NULL, &finfo, R_OK | W_OK | X_OK,
332 fcgi_user_id, fcgi_group_id);
333 #endif
334 if (err != NULL) {
335 return ap_psprintf(tp,
336 "access for server (uid %ld, gid %ld) failed: %s",
337 (long)fcgi_user_id, (long)fcgi_group_id, err);
340 return NULL;
343 /*******************************************************************************
344 * Create a "dynamic" subdirectory. If the directory
345 * already exists we don't mess with it unless 'wax' is set.
347 const char *fcgi_config_make_dynamic_dir(pool *p, const int wax)
349 #ifndef WIN32
350 DIR *dp = NULL;
351 struct dirent *dirp = NULL;
352 const char *err;
353 pool *tp;
355 fcgi_dynamic_dir = ap_pstrcat(p, fcgi_socket_dir, "/dynamic", NULL);
357 if ((err = fcgi_config_make_dir(p, fcgi_dynamic_dir)))
358 return ap_psprintf(p, "can't create dynamic directory \"%s\": %s", fcgi_dynamic_dir, err);
360 /* Don't step on a running server unless its OK. */
361 if (!wax)
362 return NULL;
364 /* Create a subpool for the directory operations */
365 tp = ap_make_sub_pool(p);
367 dp = ap_popendir(tp, fcgi_dynamic_dir);
368 if (dp == NULL) {
369 ap_destroy_pool(tp);
370 return ap_psprintf(p, "can't open dynamic directory \"%s\": %s",
371 fcgi_dynamic_dir, strerror(errno));
374 /* Delete everything in the directory, its all FCGI specific */
375 while ((dirp = readdir(dp)) != NULL) {
376 if (strcmp(dirp->d_name, ".") == 0
377 || strcmp(dirp->d_name, "..") == 0) {
378 continue;
381 unlink(ap_pstrcat(tp, fcgi_dynamic_dir, "/", dirp->d_name, NULL));
384 ap_destroy_pool(tp);
386 #else
387 fcgi_dynamic_dir = ap_pstrcat(p, fcgi_socket_dir, "dynamic", NULL);
388 #endif
389 return NULL;
393 /*******************************************************************************
394 * Change the directory used for the Unix/Domain sockets from the default.
395 * Create the directory and the "dynamic" subdirectory.
397 const char *fcgi_config_set_socket_dir(cmd_parms *cmd, void *dummy, char *arg)
399 pool * const tp = cmd->temp_pool;
400 const char * const name = cmd->cmd->name;
401 const char *err;
403 if (strcmp(fcgi_socket_dir, DEFAULT_SOCK_DIR) != 0) {
404 return ap_psprintf(tp, "%s %s: already defined as \"%s\"",
405 name, arg, fcgi_socket_dir);
408 err = fcgi_config_set_fcgi_uid_n_gid(1);
409 if (err != NULL)
410 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
412 if (fcgi_servers != NULL) {
413 return ap_psprintf(tp,
414 "The %s command must preceed static FastCGI server definitions",
415 name);
418 #ifndef WIN32
419 arg = ap_os_canonical_filename(cmd->pool, arg);
420 arg = ap_server_root_relative(cmd->pool, arg);
421 #else
422 if (strncmp(arg, "\\\\.\\pipe\\", 9) != 0)
423 return ap_psprintf(tp, "%s %s is invalid format",name, arg);
424 #endif
426 fcgi_socket_dir = arg;
428 #ifndef WIN32
429 err = fcgi_config_make_dir(tp, fcgi_socket_dir);
430 if (err != NULL)
431 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
433 err = fcgi_config_make_dynamic_dir(cmd->pool, 0);
434 if (err != NULL)
435 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
436 #endif
438 return NULL;
441 /*******************************************************************************
442 * Enable, disable, or specify the path to a wrapper used to invoke all
443 * FastCGI applications.
445 const char *fcgi_config_set_wrapper(cmd_parms *cmd, void *dummy, const char *arg)
447 const char *err = NULL;
448 const char * const name = cmd->cmd->name;
449 pool * const tp = cmd->temp_pool;
450 char * wrapper;
452 if (!ap_suexec_enabled && strcasecmp(arg, "On") == 0) {
453 fprintf(stderr, "Warning: \"%s On\" requires SUEXEC be enabled in Apache", name);
454 return NULL;
457 err = fcgi_config_set_fcgi_uid_n_gid(1);
458 if (err != NULL)
459 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
461 if (fcgi_servers != NULL) {
462 return ap_psprintf(tp,
463 "The %s command must preceed static FastCGI server definitions", name);
466 if (strcasecmp(arg, "On") == 0) {
467 fcgi_wrapper = SUEXEC_BIN;
469 else if (strcasecmp(arg, "Off") == 0) {
470 fcgi_wrapper = NULL;
472 else {
473 wrapper = (char *) ap_os_canonical_filename(cmd->pool, arg);
474 wrapper = ap_server_root_relative(cmd->pool, wrapper);
476 #ifdef WIN32
477 err = fcgi_util_check_access(tp, wrapper, NULL, _S_IEXEC, fcgi_user_id, fcgi_group_id);
478 #else
479 err = fcgi_util_check_access(tp, wrapper, NULL, X_OK, fcgi_user_id, fcgi_group_id);
480 #endif
482 if (err != NULL) {
483 return ap_psprintf(tp,
484 "%s: \"%s\" access for server (uid %ld, gid %ld) failed: %s",
485 name, wrapper, (long)fcgi_user_id, (long)fcgi_group_id, err);
488 fcgi_wrapper = wrapper;
490 return NULL;
493 /*******************************************************************************
494 * Configure a static FastCGI server.
496 const char *fcgi_config_new_static_server(cmd_parms *cmd, void *dummy, const char *arg)
498 fcgi_server *s;
499 pool *p = cmd->pool, *tp = cmd->temp_pool;
500 const char *name = cmd->cmd->name;
501 char *fs_path = ap_getword_conf(p, &arg);
502 const char *option, *err;
504 /* Allocate temp storage for the array of initial environment variables */
505 char **envp = ap_pcalloc(tp, sizeof(char *) * (MAX_INIT_ENV_VARS + 3));
506 unsigned int envc = 0;
508 #ifdef WIN32
509 HANDLE mutex = ap_create_mutex(NULL);
511 SetHandleInformation(mutex, HANDLE_FLAG_INHERIT, TRUE);
512 #endif
514 if (*fs_path == '\0')
515 return "AppClass requires a pathname!?";
517 if ((err = fcgi_config_set_fcgi_uid_n_gid(1)) != NULL)
518 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
520 fs_path = ap_os_canonical_filename(p, fs_path);
521 fs_path = ap_server_root_relative(p, fs_path);
523 ap_getparents(fs_path);
524 ap_no2slash(fs_path);
526 /* See if we've already got one of these configured */
527 s = fcgi_util_fs_get_by_id(fs_path, cmd->server->server_uid,
528 cmd->server->server_gid);
529 if (s != NULL) {
530 if (fcgi_wrapper) {
531 return ap_psprintf(tp,
532 "%s: redefinition of a previously defined FastCGI server \"%s\" with uid=%ld and gid=%ld",
533 name, fs_path, (long)cmd->server->server_uid,
534 (long)cmd->server->server_gid);
536 else {
537 return ap_psprintf(tp,
538 "%s: redefinition of a previously defined FastCGI server \"%s\"",
539 name, fs_path);
543 err = fcgi_util_fs_is_path_ok(tp, fs_path, NULL);
544 if (err != NULL) {
545 return ap_psprintf(tp, "%s: \"%s\" %s", name, fs_path, err);
548 s = fcgi_util_fs_new(p);
549 s->fs_path = fs_path;
550 s->directive = APP_CLASS_STANDARD;
551 s->restartOnExit = TRUE;
552 s->numProcesses = 1;
554 #ifdef WIN32
555 // TCP FastCGI applications require SystemRoot be present in the environment
556 // Put it in both for consistency to the application
557 fcgi_config_set_env_var(tp, envp, &envc, "SystemRoot");
559 s->mutex_env_string = ap_psprintf(p, "_FCGI_MUTEX_=%ld", mutex);;
560 #else
561 if (fcgi_wrapper) {
562 struct passwd *pw;
563 struct group *gr;
565 s->uid = cmd->server->server_uid;
566 pw = getpwuid(s->uid);
567 if (pw == NULL) {
568 return ap_psprintf(tp, "mod_fastcgi: "
569 "getpwuid() couldn't determine the username for uid '%ld', "
570 "you probably need to modify the User directive: %s",
571 (long)s->uid, strerror(errno));
573 s->user = ap_pstrdup(p, pw->pw_name);
574 s->username = s->user;
576 s->gid = cmd->server->server_gid;
577 gr = getgrgid(s->gid);
578 if (gr == NULL) {
579 return ap_psprintf(tp, "mod_fastcgi: "
580 "getgrgid() couldn't determine the group name for gid '%ld', "
581 "you probably need to modify the Group directive: %s\n",
582 (long)s->gid, strerror(errno));
584 s->group = ap_pstrdup(p, gr->gr_name);
586 #endif
588 /* Parse directive arguments */
589 while (*arg) {
590 option = ap_getword_conf(tp, &arg);
592 if (strcasecmp(option, "-processes") == 0) {
593 if ((err = get_u_int(tp, &arg, &s->numProcesses, 1)))
594 return invalid_value(tp, name, fs_path, option, err);
596 else if (strcasecmp(option, "-restart-delay") == 0) {
597 if ((err = get_u_int(tp, &arg, &s->restartDelay, 0)))
598 return invalid_value(tp, name, fs_path, option, err);
600 else if (strcasecmp(option, "-init-start-delay") == 0) {
601 if ((err = get_int(tp, &arg, &s->initStartDelay, 0)))
602 return invalid_value(tp, name, fs_path, option, err);
604 else if (strcasecmp(option, "-priority") == 0) {
605 if ((err = get_u_int(tp, &arg, &s->processPriority, 0)))
606 return invalid_value(tp, name, fs_path, option, err);
608 else if (strcasecmp(option, "-listen-queue-depth") == 0) {
609 if ((err = get_u_int(tp, &arg, &s->listenQueueDepth, 1)))
610 return invalid_value(tp, name, fs_path, option, err);
612 else if (strcasecmp(option, "-appConnTimeout") == 0) {
613 if ((err = get_u_int(tp, &arg, &s->appConnectTimeout, 0)))
614 return invalid_value(tp, name, fs_path, option, err);
616 else if (strcasecmp(option, "-idle-timeout") == 0) {
617 if ((err = get_u_int(tp, &arg, &s->idle_timeout, 1)))
618 return invalid_value(tp, name, fs_path, option, err);
620 else if (strcasecmp(option, "-port") == 0) {
621 if ((err = get_u_short(tp, &arg, &s->port, 1)))
622 return invalid_value(tp, name, fs_path, option, err);
624 else if (strcasecmp(option, "-socket") == 0) {
625 s->socket_path = ap_getword_conf(tp, &arg);
626 if (*s->socket_path == '\0')
627 return invalid_value(tp, name, fs_path, option, "\"\"");
629 else if (strcasecmp(option, "-initial-env") == 0) {
630 if ((err = get_env_var(p, &arg, envp, &envc)))
631 return invalid_value(tp, name, fs_path, option, err);
633 else if (strcasecmp(option, "-pass-header") == 0) {
634 if ((err = get_pass_header(p, &arg, &s->pass_headers)))
635 return invalid_value(tp, name, fs_path, option, err);
637 else if (strcasecmp(option, "-flush") == 0) {
638 s->flush = 1;
640 else {
641 return ap_psprintf(tp, "%s %s: invalid option: %s", name, fs_path, option);
643 } /* while */
645 if (s->socket_path != NULL && s->port != 0) {
646 return ap_psprintf(tp,
647 "%s %s: -port and -socket are mutually exclusive options",
648 name, fs_path);
651 /* Move env array to a surviving pool */
652 ++envc;
653 s->envp = (char **)ap_palloc(p, sizeof(char *) * ++envc);
654 memcpy(s->envp, envp, sizeof(char *) * envc);
656 /* Initialize process structs */
657 s->procs = fcgi_util_fs_create_procs(p, s->numProcesses);
659 /* Build the appropriate sockaddr structure */
660 if (s->port != 0) {
661 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->socket_addr,
662 &s->socket_addr_len, NULL, s->port);
663 if (err != NULL)
664 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
665 #ifdef WIN32
666 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->dest_addr,
667 &s->socket_addr_len, "localhost", s->port);
668 if (err != NULL)
669 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
670 #endif
671 } else {
672 if (s->socket_path == NULL)
673 s->socket_path = fcgi_util_socket_hash_filename(tp, fs_path, s->user, s->group);
674 s->socket_path = fcgi_util_socket_make_path_absolute(p, s->socket_path, 0);
675 #ifndef WIN32
676 err = fcgi_util_socket_make_domain_addr(p, (struct sockaddr_un **)&s->socket_addr,
677 &s->socket_addr_len, s->socket_path);
678 if (err != NULL)
679 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
680 #endif
683 /* Add it to the list of FastCGI servers */
684 fcgi_util_fs_add(s);
686 return NULL;
689 /*******************************************************************************
690 * Configure a static FastCGI server that is started/managed elsewhere.
692 const char *fcgi_config_new_external_server(cmd_parms *cmd, void *dummy, const char *arg)
694 fcgi_server *s;
695 pool * const p = cmd->pool, *tp = cmd->temp_pool;
696 const char * const name = cmd->cmd->name;
697 char *fs_path = ap_getword_conf(p, &arg);
698 const char *option, *err;
700 if (!*fs_path) {
701 return ap_pstrcat(tp, name, " requires a path and either a -socket or -host option", NULL);
704 fs_path = ap_os_canonical_filename(p, fs_path);
705 fs_path = ap_server_root_relative(p, fs_path);
707 ap_getparents(fs_path);
708 ap_no2slash(fs_path);
710 /* See if we've already got one of these bettys configured */
711 s = fcgi_util_fs_get_by_id(fs_path, cmd->server->server_uid,
712 cmd->server->server_gid);
713 if (s != NULL) {
714 if (fcgi_wrapper) {
715 return ap_psprintf(tp,
716 "%s: redefinition of a previously defined class \"%s\" with uid=%ld and gid=%ld",
717 name, fs_path, (long)cmd->server->server_uid,
718 (long)cmd->server->server_gid);
720 else {
721 return ap_psprintf(tp,
722 "%s: redefinition of previously defined class \"%s\"", name, fs_path);
726 s = fcgi_util_fs_new(p);
727 s->fs_path = fs_path;
728 s->directive = APP_CLASS_EXTERNAL;
730 err = fcgi_util_fs_set_uid_n_gid(p, s, cmd->server->server_uid, cmd->server->server_gid);
731 if (err != NULL)
732 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
734 /* Parse directive arguments */
735 while (*arg != '\0') {
736 option = ap_getword_conf(tp, &arg);
738 if (strcasecmp(option, "-host") == 0) {
739 if ((err = get_host_n_port(p, &arg, &s->host, &s->port)))
740 return invalid_value(tp, name, fs_path, option, err);
742 else if (strcasecmp(option, "-socket") == 0) {
743 s->socket_path = ap_getword_conf(tp, &arg);
744 if (*s->socket_path == '\0')
745 return invalid_value(tp, name, fs_path, option, "\"\"");
747 else if (strcasecmp(option, "-appConnTimeout") == 0) {
748 if ((err = get_u_int(tp, &arg, &s->appConnectTimeout, 0)))
749 return invalid_value(tp, name, fs_path, option, err);
751 else if (strcasecmp(option, "-idle-timeout") == 0) {
752 if ((err = get_u_int(tp, &arg, &s->idle_timeout, 1)))
753 return invalid_value(tp, name, fs_path, option, err);
755 else if (strcasecmp(option, "-pass-header") == 0) {
756 if ((err = get_pass_header(p, &arg, &s->pass_headers)))
757 return invalid_value(tp, name, fs_path, option, err);
759 else if (strcasecmp(option, "-flush") == 0) {
760 s->flush = 1;
762 else {
763 return ap_psprintf(tp, "%s %s: invalid option: %s", name, fs_path, option);
765 } /* while */
767 /* Require one of -socket or -host, but not both */
768 if (s->socket_path != NULL && s->port != 0) {
769 return ap_psprintf(tp,
770 "%s %s: -host and -socket are mutually exclusive options",
771 name, fs_path);
773 if (s->socket_path == NULL && s->port == 0) {
774 return ap_psprintf(tp,
775 "%s %s: -socket or -host option missing", name, fs_path);
778 /* Build the appropriate sockaddr structure */
779 if (s->port != 0) {
780 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->socket_addr,
781 &s->socket_addr_len, s->host, s->port);
782 if (err != NULL)
783 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
784 } else {
785 s->socket_path = fcgi_util_socket_make_path_absolute(p, s->socket_path, 0);
786 #ifndef WIN32
787 err = fcgi_util_socket_make_domain_addr(p, (struct sockaddr_un **)&s->socket_addr,
788 &s->socket_addr_len, s->socket_path);
789 if (err != NULL)
790 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
791 #endif
794 /* Add it to the list of FastCGI servers */
795 fcgi_util_fs_add(s);
797 return NULL;
801 *----------------------------------------------------------------------
803 * fcgi_config_set_config --
805 * Implements the FastCGI FCGIConfig configuration directive.
806 * This command adds routines to control the execution of the
807 * dynamic FastCGI processes.
810 *----------------------------------------------------------------------
812 const char *fcgi_config_set_config(cmd_parms *cmd, void *dummy, const char *arg)
814 pool * const p = cmd->pool;
815 pool * const tp = cmd->temp_pool;
816 const char *err, *option;
817 const char * const name = cmd->cmd->name;
819 /* Allocate temp storage for an initial environment */
820 unsigned int envc = 0;
821 char **envp = (char **)ap_pcalloc(tp, sizeof(char *) * (MAX_INIT_ENV_VARS + 3));
823 /* Parse the directive arguments */
824 while (*arg) {
825 option = ap_getword_conf(tp, &arg);
827 if (strcasecmp(option, "-maxProcesses") == 0) {
828 if ((err = get_u_int(tp, &arg, &dynamicMaxProcs, 1)))
829 return invalid_value(tp, name, NULL, option, err);
831 else if (strcasecmp(option, "-minProcesses") == 0) {
832 if ((err = get_int(tp, &arg, &dynamicMinProcs, 0)))
833 return invalid_value(tp, name, NULL, option, err);
835 else if (strcasecmp(option, "-maxClassProcesses") == 0) {
836 if ((err = get_int(tp, &arg, &dynamicMaxClassProcs, 1)))
837 return invalid_value(tp, name, NULL, option, err);
839 else if (strcasecmp(option, "-killInterval") == 0) {
840 if ((err = get_u_int(tp, &arg, &dynamicKillInterval, 1)))
841 return invalid_value(tp, name, NULL, option, err);
843 else if (strcasecmp(option, "-updateInterval") == 0) {
844 if ((err = get_u_int(tp, &arg, &dynamicUpdateInterval, 1)))
845 return invalid_value(tp, name, NULL, option, err);
847 else if (strcasecmp(option, "-gainValue") == 0) {
848 if ((err = get_float(tp, &arg, &dynamicGain, 0.0, 1.0)))
849 return invalid_value(tp, name, NULL, option, err);
851 else if ((strcasecmp(option, "-singleThreshold") == 0)
852 || (strcasecmp(option, "-singleThreshhold") == 0))
854 if ((err = get_int(tp, &arg, &dynamicThreshold1, 0)))
855 return invalid_value(tp, name, NULL, option, err);
857 else if ((strcasecmp(option, "-multiThreshold") == 0)
858 || (strcasecmp(option, "-multiThreshhold") == 0))
860 if ((err = get_int(tp, &arg, &dynamicThresholdN, 0)))
861 return invalid_value(tp, name, NULL, option, err);
863 else if (strcasecmp(option, "-startDelay") == 0) {
864 if ((err = get_u_int(tp, &arg, &dynamicPleaseStartDelay, 1)))
865 return invalid_value(tp, name, NULL, option, err);
867 else if (strcasecmp(option, "-initial-env") == 0) {
868 if ((err = get_env_var(p, &arg, envp, &envc)))
869 return invalid_value(tp, name, NULL, option, err);
871 else if (strcasecmp(option, "-pass-header") == 0) {
872 if ((err = get_pass_header(p, &arg, &dynamic_pass_headers)))
873 return invalid_value(tp, name, NULL, option, err);
875 else if (strcasecmp(option, "-appConnTimeout") == 0) {
876 if ((err = get_u_int(tp, &arg, &dynamicAppConnectTimeout, 0)))
877 return invalid_value(tp, name, NULL, option, err);
879 else if (strcasecmp(option, "-idle-timeout") == 0) {
880 if ((err = get_u_int(tp, &arg, &dynamic_idle_timeout, 1)))
881 return invalid_value(tp, name, NULL, option, err);
883 else if (strcasecmp(option, "-listen-queue-depth") == 0) {
884 if ((err = get_u_int(tp, &arg, &dynamicListenQueueDepth, 1)))
885 return invalid_value(tp, name, NULL, option, err);
887 else if (strcasecmp(option, "-restart-delay") == 0) {
888 if ((err = get_u_int(tp, &arg, &dynamicRestartDelay, 0)))
889 return invalid_value(tp, name, NULL, option, err);
891 else if (strcasecmp(option, "-init-start-delay") == 0) {
892 if ((err = get_u_int(tp, &arg, &dynamicInitStartDelay, 0)))
893 return invalid_value(tp, name, NULL, option, err);
895 else if (strcasecmp(option, "-processSlack") == 0) {
896 if ((err = get_u_int(tp, &arg, &dynamicProcessSlack, 1)))
897 return invalid_value(tp, name, NULL, option, err);
899 else if (strcasecmp(option, "-restart") == 0) {
900 dynamicAutoRestart = 1;
902 else if (strcasecmp(option, "-autoUpdate") == 0) {
903 dynamicAutoUpdate = 1;
905 else {
906 return ap_psprintf(tp, "%s: invalid option: %s", name, option);
908 } /* while */
910 /* Move env array to a surviving pool, leave an extra slot for WIN32 _FCGI_MUTEX_ */
911 ++envc;
912 dynamicEnvp = (char **)ap_palloc(p, sizeof(char *) * ++envc);
913 memcpy(dynamicEnvp, envp, sizeof(char *) * envc);
915 return NULL;
918 void *fcgi_config_create_dir_config(pool *p, char *dummy)
920 fcgi_dir_config *dir_config = ap_pcalloc(p, sizeof(fcgi_dir_config));
922 dir_config->authenticator_options = FCGI_AUTHORITATIVE;
923 dir_config->authorizer_options = FCGI_AUTHORITATIVE;
924 dir_config->access_checker_options = FCGI_AUTHORITATIVE;
926 return dir_config;
930 const char *fcgi_config_new_auth_server(cmd_parms * const cmd,
931 fcgi_dir_config *dir_config, const char *fs_path, const char * const compat)
933 pool * const tp = cmd->temp_pool;
934 const uid_t uid = cmd->server->server_uid;
935 const gid_t gid = cmd->server->server_gid;
936 char * auth_server;
938 auth_server = (char *) ap_os_canonical_filename(cmd->pool, fs_path);
939 auth_server = ap_server_root_relative(cmd->pool, auth_server);
941 /* Make sure its already configured or at least a candidate for dynamic */
942 if (fcgi_util_fs_get_by_id(auth_server, uid, gid) == NULL) {
943 const char *err = fcgi_util_fs_is_path_ok(tp, auth_server, NULL);
944 if (err)
945 return ap_psprintf(tp, "%s: \"%s\" %s", cmd->cmd->name, auth_server, err);
948 if (compat && strcasecmp(compat, "-compat"))
949 return ap_psprintf(cmd->temp_pool, "%s: unknown option: \"%s\"", cmd->cmd->name, compat);
951 switch((int)cmd->info) {
952 case FCGI_AUTH_TYPE_AUTHENTICATOR:
953 dir_config->authenticator = auth_server;
954 dir_config->authenticator_options |= (compat) ? FCGI_COMPAT : 0;
955 break;
956 case FCGI_AUTH_TYPE_AUTHORIZER:
957 dir_config->authorizer = auth_server;
958 dir_config->authorizer_options |= (compat) ? FCGI_COMPAT : 0;
959 break;
960 case FCGI_AUTH_TYPE_ACCESS_CHECKER:
961 dir_config->access_checker = auth_server;
962 dir_config->access_checker_options |= (compat) ? FCGI_COMPAT : 0;
963 break;
966 return NULL;
969 const char *fcgi_config_set_authoritative_slot(const cmd_parms * const cmd,
970 fcgi_dir_config * const dir_config, int arg)
972 int offset = (int)(long)cmd->info;
974 if (arg)
975 *((u_char *)dir_config + offset) |= FCGI_AUTHORITATIVE;
976 else
977 *((u_char *)dir_config + offset) &= ~FCGI_AUTHORITATIVE;
979 return NULL;