2 * $Id: fcgi_util.c,v 1.25 2002/07/26 03:10:54 robs Exp $
8 #pragma warning( disable : 4100 )
17 fcgi_util_get_server_uid(const server_rec
* const s
)
20 /* AP2TODO get the server's uid */
28 fcgi_util_get_server_gid(const server_rec
* const s
)
31 /* AP2TODO get the server's gid */
38 /*******************************************************************************
39 * Compute printable MD5 hash. Pool p is used for scratch as well as for
40 * allocating the hash - use temp storage, and dup it if you need to keep it.
43 fcgi_util_socket_hash_filename(pool
*p
, const char *path
,
44 const char *user
, const char *group
)
46 char *buf
= ap_pstrcat(p
, path
, user
, group
, NULL
);
48 /* Canonicalize the path (remove "//", ".", "..") */
51 return ap_md5(p
, (unsigned char *)buf
);
55 /*******************************************************************************
56 * Concat src1 and src2 using the approprate path seperator for the platform.
58 static char * make_full_path(pool
*a
, const char *src1
, const char *src2
)
68 p
= ap_pstrcat(a
, "\\", src2
, NULL
);
70 else if (src1
[x
- 1] != '\\' && src1
[x
- 1] != '/') {
71 p
= ap_pstrcat(a
, src1
, "\\", src2
, NULL
);
74 p
= ap_pstrcat(a
, src1
, src2
, NULL
);
88 return ap_make_full_path(a
, src1
, src2
);
92 /*******************************************************************************
93 * Return absolute path to file in either "regular" FCGI socket directory or
94 * the dynamic directory. Result is allocated in pool p.
97 fcgi_util_socket_make_path_absolute(pool
* const p
,
98 const char *const file
, const int dynamic
)
101 if (ap_os_is_path_absolute(p
, (char *) file
))
103 if (ap_os_is_path_absolute(file
))
110 const char * parent_dir
= dynamic
? fcgi_dynamic_dir
: fcgi_socket_dir
;
111 return (const char *) make_full_path(p
, parent_dir
, file
);
116 /*******************************************************************************
117 * Build a Domain Socket Address structure, and calculate its size.
118 * The error message is allocated from the pool p. If you don't want the
119 * struct sockaddr_un also allocated from p, pass it preallocated (!=NULL).
122 fcgi_util_socket_make_domain_addr(pool
*p
, struct sockaddr_un
**socket_addr
,
123 int *socket_addr_len
, const char *socket_path
)
125 int socket_pathLen
= strlen(socket_path
);
127 if (socket_pathLen
>= sizeof((*socket_addr
)->sun_path
)) {
128 return ap_pstrcat(p
, "path \"", socket_path
,
129 "\" is too long for a Domain socket", NULL
);
132 if (*socket_addr
== NULL
)
133 *socket_addr
= ap_pcalloc(p
, sizeof(struct sockaddr_un
));
135 memset(*socket_addr
, 0, sizeof(struct sockaddr_un
));
137 (*socket_addr
)->sun_family
= AF_UNIX
;
138 strcpy((*socket_addr
)->sun_path
, socket_path
);
140 *socket_addr_len
= SUN_LEN(*socket_addr
);
145 /*******************************************************************************
146 * Convert a hostname or IP address string to an in_addr struct.
149 convert_string_to_in_addr(const char * const hostname
, struct in_addr
* const addr
)
154 addr
->s_addr
= inet_addr((char *)hostname
);
156 #if !defined(INADDR_NONE) && defined(APACHE2)
157 #define INADDR_NONE APR_INADDR_NONE
160 if (addr
->s_addr
== INADDR_NONE
) {
161 if ((hp
= gethostbyname((char *)hostname
)) == NULL
)
164 memcpy((char *) addr
, hp
->h_addr
, hp
->h_length
);
166 while (hp
->h_addr_list
[count
] != 0)
175 /*******************************************************************************
176 * Build an Inet Socket Address structure, and calculate its size.
177 * The error message is allocated from the pool p. If you don't want the
178 * struct sockaddr_in also allocated from p, pass it preallocated (!=NULL).
181 fcgi_util_socket_make_inet_addr(pool
*p
, struct sockaddr_in
**socket_addr
,
182 int *socket_addr_len
, const char *host
, unsigned short port
)
184 if (*socket_addr
== NULL
)
185 *socket_addr
= ap_pcalloc(p
, sizeof(struct sockaddr_in
));
187 memset(*socket_addr
, 0, sizeof(struct sockaddr_in
));
189 (*socket_addr
)->sin_family
= AF_INET
;
190 (*socket_addr
)->sin_port
= htons(port
);
192 /* Get an in_addr represention of the host */
194 if (convert_string_to_in_addr(host
, &(*socket_addr
)->sin_addr
) != 1) {
195 return ap_pstrcat(p
, "failed to resolve \"", host
,
196 "\" to exactly one IP address", NULL
);
199 (*socket_addr
)->sin_addr
.s_addr
= htonl(INADDR_ANY
);
202 *socket_addr_len
= sizeof(struct sockaddr_in
);
206 /*******************************************************************************
207 * Determine if a process with uid/gid can access a file with mode permissions.
210 fcgi_util_check_access(pool
*tp
,
211 const char * const path
, const struct stat
*statBuf
,
212 const int mode
, const uid_t uid
, const gid_t gid
)
214 struct stat myStatBuf
;
216 if (statBuf
== NULL
) {
217 if (stat(path
, &myStatBuf
) < 0)
218 return ap_psprintf(tp
, "stat(%s) failed: %s", path
, strerror(errno
));
219 statBuf
= &myStatBuf
;
223 /* If the uid owns the file, check the owner bits */
224 if (uid
== statBuf
->st_uid
) {
225 if (mode
& R_OK
&& !(statBuf
->st_mode
& S_IRUSR
))
226 return "read not allowed by owner";
227 if (mode
& W_OK
&& !(statBuf
->st_mode
& S_IWUSR
))
228 return "write not allowed by owner";
229 if (mode
& X_OK
&& !(statBuf
->st_mode
& S_IXUSR
))
230 return "execute not allowed by owner";
234 if (mode
& _S_IREAD
&& !(statBuf
->st_mode
& _S_IREAD
))
235 return "read not allowed";
236 if (mode
& _S_IWRITE
&& !(statBuf
->st_mode
& _S_IWRITE
))
237 return "write not allowed";
239 // I don't think this works on FAT, but since I don't know how to check..
240 // if (mode & _S_IEXEC && !(statBuf->st_mode & _S_IEXEC))
241 // return "execute not allowed";
244 #if !defined(__EMX__) && !defined(WIN32)
245 /* If the gid is same as the file's group, check the group bits */
246 if (gid
== statBuf
->st_gid
) {
247 if (mode
& R_OK
&& !(statBuf
->st_mode
& S_IRGRP
))
248 return "read not allowed by group";
249 if (mode
& W_OK
&& !(statBuf
->st_mode
& S_IWGRP
))
250 return "write not allowed by group";
251 if (mode
& X_OK
&& !(statBuf
->st_mode
& S_IXGRP
))
252 return "execute not allowed by group";
256 /* Get the user membership for the file's group. If the
257 * uid is a member, check the group bits. */
259 const struct group
* const gr
= getgrgid(statBuf
->st_gid
);
260 const struct passwd
* const pw
= getpwuid(uid
);
262 if (gr
!= NULL
&& pw
!= NULL
) {
263 char **user
= gr
->gr_mem
;
264 for ( ; *user
!= NULL
; user
++) {
265 if (strcmp(*user
, pw
->pw_name
) == 0) {
266 if (mode
& R_OK
&& !(statBuf
->st_mode
& S_IRGRP
))
267 return "read not allowed by group";
268 if (mode
& W_OK
&& !(statBuf
->st_mode
& S_IWGRP
))
269 return "write not allowed by group";
270 if (mode
& X_OK
&& !(statBuf
->st_mode
& S_IXGRP
))
271 return "execute not allowed by group";
278 /* That just leaves the other bits.. */
279 if (mode
& R_OK
&& !(statBuf
->st_mode
& S_IROTH
))
280 return "read not allowed";
281 if (mode
& W_OK
&& !(statBuf
->st_mode
& S_IWOTH
))
282 return "write not allowed";
283 if (mode
& X_OK
&& !(statBuf
->st_mode
& S_IXOTH
))
284 return "execute not allowed";
291 /*******************************************************************************
292 * Find a FastCGI server with a matching fs_path, and if fcgi_wrapper is
293 * enabled with matching uid and gid.
296 fcgi_util_fs_get_by_id(const char *ePath
, uid_t uid
, gid_t gid
)
298 char path
[FCGI_MAXPATH
];
301 /* @@@ This should now be done in the loop below */
302 ap_cpystrn(path
, ePath
, FCGI_MAXPATH
);
305 for (s
= fcgi_servers
; s
!= NULL
; s
= s
->next
) {
307 const char *fs_path
= s
->fs_path
;
308 for (i
= 0; fs_path
[i
] && path
[i
]; ++i
) {
309 if (fs_path
[i
] != path
[i
]) {
316 if (path
[i
] == '\0' || path
[i
] == '/') {
317 if (fcgi_wrapper
== NULL
|| (uid
== s
->uid
&& gid
== s
->gid
))
324 /*******************************************************************************
325 * Find a FastCGI server with a matching fs_path, and if fcgi_wrapper is
326 * enabled with matching user and group.
329 fcgi_util_fs_get(const char *ePath
, const char *user
, const char *group
)
331 char path
[FCGI_MAXPATH
];
334 ap_cpystrn(path
, ePath
, FCGI_MAXPATH
);
337 for (s
= fcgi_servers
; s
!= NULL
; s
= s
->next
) {
338 if (strcmp(s
->fs_path
, path
) == 0) {
339 if (fcgi_wrapper
== NULL
)
342 if (strcmp(user
, s
->user
) == 0
343 && (user
[0] == '~' || strcmp(group
, s
->group
) == 0))
353 fcgi_util_fs_is_path_ok(pool
* const p
, const char * const fs_path
, struct stat
*finfo
)
358 finfo
= (struct stat
*)ap_palloc(p
, sizeof(struct stat
));
359 if (stat(fs_path
, finfo
) < 0)
360 return ap_psprintf(p
, "stat(%s) failed: %s", fs_path
, strerror(errno
));
363 /* No Parse Header scripts aren't allowed.
364 * @@@ Well... we really could quite easily */
365 if (strncmp(strrchr(fs_path
, '/'), "/nph-", 5) == 0)
366 return ap_psprintf(p
, "NPH scripts cannot be run as FastCGI");
368 if (finfo
->st_mode
== 0)
369 return ap_psprintf(p
, "script not found or unable to stat()");
371 if (S_ISDIR(finfo
->st_mode
))
372 return ap_psprintf(p
, "script is a directory!");
374 /* Let the wrapper determine what it can and can't execute */
378 err
= fcgi_util_check_access(p
, fs_path
, finfo
, _S_IEXEC
, fcgi_user_id
, fcgi_group_id
);
380 err
= fcgi_util_check_access(p
, fs_path
, finfo
, X_OK
, fcgi_user_id
, fcgi_group_id
);
383 return ap_psprintf(p
,
384 "access for server (uid %ld, gid %ld) not allowed: %s",
385 (long)fcgi_user_id
, (long)fcgi_group_id
, err
);
394 /*******************************************************************************
395 * Allocate a new FastCGI server record from pool p with default values.
398 fcgi_util_fs_new(pool
*p
)
400 fcgi_server
*s
= (fcgi_server
*) ap_pcalloc(p
, sizeof(fcgi_server
));
402 /* Initialize anything who's init state is not zeroizzzzed */
403 s
->listenQueueDepth
= FCGI_DEFAULT_LISTEN_Q
;
404 s
->appConnectTimeout
= FCGI_DEFAULT_APP_CONN_TIMEOUT
;
405 s
->idle_timeout
= FCGI_DEFAULT_IDLE_TIMEOUT
;
406 s
->initStartDelay
= DEFAULT_INIT_START_DELAY
;
407 s
->restartDelay
= FCGI_DEFAULT_RESTART_DELAY
;
408 s
->restartOnExit
= FALSE
;
409 s
->directive
= APP_CLASS_UNKNOWN
;
410 s
->processPriority
= FCGI_DEFAULT_PRIORITY
;
411 s
->envp
= &fcgi_empty_env
;
414 s
->listenFd
= (int) INVALID_HANDLE_VALUE
;
422 /*******************************************************************************
423 * Add the server to the linked list of FastCGI servers.
426 fcgi_util_fs_add(fcgi_server
*s
)
428 s
->next
= fcgi_servers
;
432 /*******************************************************************************
433 * Configure uid, gid, user, group, username for wrapper.
436 fcgi_util_fs_set_uid_n_gid(pool
*p
, fcgi_server
*s
, uid_t uid
, gid_t gid
)
443 if (fcgi_wrapper
== NULL
)
450 return ap_psprintf(p
,
451 "getpwuid() couldn't determine the username for uid '%ld', "
452 "you probably need to modify the User directive: %s",
453 (long)uid
, strerror(errno
));
455 s
->user
= ap_pstrdup(p
, pw
->pw_name
);
456 s
->username
= s
->user
;
461 return ap_psprintf(p
,
462 "getgrgid() couldn't determine the group name for gid '%ld', "
463 "you probably need to modify the Group directive: %s",
464 (long)gid
, strerror(errno
));
466 s
->group
= ap_pstrdup(p
, gr
->gr_name
);
471 /*******************************************************************************
472 * Allocate an array of ServerProcess records.
475 fcgi_util_fs_create_procs(pool
*p
, int num
)
478 ServerProcess
*proc
= (ServerProcess
*)ap_pcalloc(p
, sizeof(ServerProcess
) * num
);
480 for (i
= 0; i
< num
; i
++) {
482 proc
[i
].handle
= INVALID_HANDLE_VALUE
;
483 proc
[i
].terminationEvent
= INVALID_HANDLE_VALUE
;
486 proc
[i
].state
= FCGI_READY_STATE
;
491 int fcgi_util_ticks(struct timeval
* tv
)
494 // millisecs is sufficent granularity
495 DWORD millis
= GetTickCount();
497 tv
->tv_sec
= millis
/ 1000;
498 tv
->tv_usec
= (millis
% 1000) * 1000;
502 return gettimeofday(tv
, NULL
);