2 * $Id: fcgi_util.c,v 1.15 2000/09/19 16:26:52 robs Exp $
7 /*******************************************************************************
8 * Compute printable MD5 hash. Pool p is used for scratch as well as for
9 * allocating the hash - use temp storage, and dup it if you need to keep it.
12 fcgi_util_socket_hash_filename(pool
*p
, const char *path
,
13 const char *user
, const char *group
)
15 char *buf
= ap_pstrcat(p
, path
, user
, group
, NULL
);
17 /* Canonicalize the path (remove "//", ".", "..") */
20 return ap_md5(p
, (unsigned char *)buf
);
23 /*******************************************************************************
24 * Return absolute path to file in either "regular" FCGI socket directory or
25 * the dynamic directory. Result is allocated in pool p.
28 fcgi_util_socket_make_path_absolute(pool
* const p
,
29 const char *const file
, const int dynamic
)
31 return (const char *)ap_pstrcat(p
,
33 (dynamic
? fcgi_dynamic_dir
: fcgi_socket_dir
), file
, NULL
);
35 (dynamic
? fcgi_dynamic_dir
: fcgi_socket_dir
), "/", file
, NULL
);
39 /*******************************************************************************
40 * Allocate a new string from pool p with the name of a Unix/Domain socket's
41 * lock file (used by dynamic only).
44 fcgi_util_socket_get_lock_filename(pool
*p
, const char *socket_path
)
46 return ap_pstrcat(p
, socket_path
, ".lock", NULL
);
50 /*******************************************************************************
51 * Build a Domain Socket Address structure, and calculate its size.
52 * The error message is allocated from the pool p. If you don't want the
53 * struct sockaddr_un also allocated from p, pass it preallocated (!=NULL).
56 fcgi_util_socket_make_domain_addr(pool
*p
, struct sockaddr_un
**socket_addr
,
57 int *socket_addr_len
, const char *socket_path
)
59 int socket_pathLen
= strlen(socket_path
);
61 if (socket_pathLen
>= sizeof((*socket_addr
)->sun_path
)) {
62 return ap_pstrcat(p
, "path \"", socket_path
,
63 "\" is too long for a Domain socket", NULL
);
66 if (*socket_addr
== NULL
)
67 *socket_addr
= ap_pcalloc(p
, sizeof(struct sockaddr_un
));
69 memset(*socket_addr
, 0, sizeof(struct sockaddr_un
));
71 (*socket_addr
)->sun_family
= AF_UNIX
;
72 strcpy((*socket_addr
)->sun_path
, socket_path
);
74 *socket_addr_len
= SUN_LEN(*socket_addr
);
79 /*******************************************************************************
80 * Convert a hostname or IP address string to an in_addr struct.
83 convert_string_to_in_addr(const char * const hostname
, struct in_addr
* const addr
)
88 addr
->s_addr
= inet_addr((char *)hostname
);
90 if (addr
->s_addr
== INADDR_NONE
) {
91 if ((hp
= gethostbyname((char *)hostname
)) == NULL
)
94 memcpy((char *) addr
, hp
->h_addr
, hp
->h_length
);
96 while (hp
->h_addr_list
[count
] != 0)
105 /*******************************************************************************
106 * Build an Inet Socket Address structure, and calculate its size.
107 * The error message is allocated from the pool p. If you don't want the
108 * struct sockaddr_in also allocated from p, pass it preallocated (!=NULL).
111 fcgi_util_socket_make_inet_addr(pool
*p
, struct sockaddr_in
**socket_addr
,
112 int *socket_addr_len
, const char *host
, unsigned short port
)
114 if (*socket_addr
== NULL
)
115 *socket_addr
= ap_pcalloc(p
, sizeof(struct sockaddr_in
));
117 memset(*socket_addr
, 0, sizeof(struct sockaddr_in
));
119 (*socket_addr
)->sin_family
= AF_INET
;
120 (*socket_addr
)->sin_port
= htons(port
);
122 /* Get an in_addr represention of the host */
124 if (convert_string_to_in_addr(host
, &(*socket_addr
)->sin_addr
) != 1) {
125 return ap_pstrcat(p
, "failed to resolve \"", host
,
126 "\" to exactly one IP address", NULL
);
129 (*socket_addr
)->sin_addr
.s_addr
= htonl(INADDR_ANY
);
132 *socket_addr_len
= sizeof(struct sockaddr_in
);
136 /*******************************************************************************
137 * Determine if a process with uid/gid can access a file with mode permissions.
140 fcgi_util_check_access(pool
*tp
,
141 const char * const path
, const struct stat
*statBuf
,
142 const int mode
, const uid_t uid
, const gid_t gid
)
144 if (statBuf
== NULL
) {
145 static struct stat staticStatBuf
;
147 if (stat(path
, &staticStatBuf
) < 0)
148 return ap_psprintf(tp
, "stat() failed: %s", strerror(errno
));
149 statBuf
= &staticStatBuf
;
153 /* If the uid owns the file, check the owner bits */
154 if (uid
== statBuf
->st_uid
) {
155 if (mode
& R_OK
&& !(statBuf
->st_mode
& S_IRUSR
))
156 return "read not allowed by owner";
157 if (mode
& W_OK
&& !(statBuf
->st_mode
& S_IWUSR
))
158 return "write not allowed by owner";
159 if (mode
& X_OK
&& !(statBuf
->st_mode
& S_IXUSR
))
160 return "execute not allowed by owner";
164 if (mode
& _S_IREAD
&& !(statBuf
->st_mode
& _S_IREAD
))
165 return "read not allowed";
166 if (mode
& _S_IWRITE
&& !(statBuf
->st_mode
& _S_IWRITE
))
167 return "write not allowed";
169 // I don't think this works on FAT, but since I don't know how to check..
170 // if (mode & _S_IEXEC && !(statBuf->st_mode & _S_IEXEC))
171 // return "execute not allowed";
174 #if !defined(__EMX__) && !defined(WIN32)
175 /* If the gid is same as the file's group, check the group bits */
176 if (gid
== statBuf
->st_gid
) {
177 if (mode
& R_OK
&& !(statBuf
->st_mode
& S_IRGRP
))
178 return "read not allowed by group";
179 if (mode
& W_OK
&& !(statBuf
->st_mode
& S_IWGRP
))
180 return "write not allowed by group";
181 if (mode
& X_OK
&& !(statBuf
->st_mode
& S_IXGRP
))
182 return "execute not allowed by group";
186 /* Get the user membership for the file's group. If the
187 * uid is a member, check the group bits. */
189 const struct group
* const gr
= getgrgid(statBuf
->st_gid
);
190 const struct passwd
* const pw
= getpwuid(uid
);
192 if (gr
!= NULL
&& pw
!= NULL
) {
193 char **user
= gr
->gr_mem
;
194 for ( ; *user
!= NULL
; user
++) {
195 if (strcmp(*user
, pw
->pw_name
) == 0) {
196 if (mode
& R_OK
&& !(statBuf
->st_mode
& S_IRGRP
))
197 return "read not allowed by group";
198 if (mode
& W_OK
&& !(statBuf
->st_mode
& S_IWGRP
))
199 return "write not allowed by group";
200 if (mode
& X_OK
&& !(statBuf
->st_mode
& S_IXGRP
))
201 return "execute not allowed by group";
208 /* That just leaves the other bits.. */
209 if (mode
& R_OK
&& !(statBuf
->st_mode
& S_IROTH
))
210 return "read not allowed";
211 if (mode
& W_OK
&& !(statBuf
->st_mode
& S_IWOTH
))
212 return "write not allowed";
213 if (mode
& X_OK
&& !(statBuf
->st_mode
& S_IXOTH
))
214 return "execute not allowed";
221 /*******************************************************************************
222 * Find a FastCGI server with a matching fs_path, and if fcgi_wrapper is
223 * enabled with matching uid and gid.
226 fcgi_util_fs_get_by_id(const char *ePath
, uid_t uid
, gid_t gid
)
228 char path
[FCGI_MAXPATH
];
231 /* @@@ This should now be done in the loop below */
232 ap_cpystrn(path
, ePath
, FCGI_MAXPATH
);
235 for (s
= fcgi_servers
; s
!= NULL
; s
= s
->next
) {
237 const char *fs_path
= s
->fs_path
;
238 for (i
= 0; fs_path
[i
] && path
[i
]; ++i
) {
239 if (fs_path
[i
] != path
[i
]) {
246 if (path
[i
] == '\0' || path
[i
] == '/') {
247 if (fcgi_wrapper
== NULL
|| (uid
== s
->uid
&& gid
== s
->gid
))
254 /*******************************************************************************
255 * Find a FastCGI server with a matching fs_path, and if fcgi_wrapper is
256 * enabled with matching user and group.
259 fcgi_util_fs_get(const char *ePath
, const char *user
, const char *group
)
261 char path
[FCGI_MAXPATH
];
264 ap_cpystrn(path
, ePath
, FCGI_MAXPATH
);
267 for (s
= fcgi_servers
; s
!= NULL
; s
= s
->next
) {
268 if (strcmp(s
->fs_path
, path
) == 0) {
269 if (fcgi_wrapper
== NULL
)
272 if (strcmp(user
, s
->user
) == 0
273 && (user
[0] == '~' || strcmp(group
, s
->group
) == 0))
283 fcgi_util_fs_is_path_ok(pool
* const p
, const char * const fs_path
,
284 struct stat
*finfo
, const uid_t uid
, const gid_t gid
)
288 /* If a wrapper is in use, let the wrapper determine what it can
289 * and can't execute */
294 finfo
= (struct stat
*)ap_palloc(p
, sizeof(struct stat
));
295 if (stat(fs_path
, finfo
) < 0)
296 return ap_psprintf(p
, "stat() failed: %s", strerror(errno
));
299 /* No Parse Header scripts aren't allowed.
300 * @@@ Well... we really could quite easily */
301 if (strncmp(strrchr(fs_path
, '/'), "/nph-", 5) == 0)
302 return ap_psprintf(p
, "NPH scripts cannot be run as FastCGI");
304 if (finfo
->st_mode
== 0)
305 return ap_psprintf(p
, "script not found or unable to stat()");
307 if (S_ISDIR(finfo
->st_mode
))
308 return ap_psprintf(p
, "script is a directory!");
311 err
= fcgi_util_check_access(p
, fs_path
, finfo
, _S_IEXEC
, fcgi_user_id
, fcgi_group_id
);
313 err
= fcgi_util_check_access(p
, fs_path
, finfo
, X_OK
, fcgi_user_id
, fcgi_group_id
);
316 return ap_psprintf(p
,
317 "access for server (uid %ld, gid %ld) not allowed: %s",
318 (long)fcgi_user_id
, (long)fcgi_group_id
, err
);
326 /*******************************************************************************
327 * Allocate a new FastCGI server record from pool p with default values.
330 fcgi_util_fs_new(pool
*p
)
332 fcgi_server
*s
= (fcgi_server
*) ap_pcalloc(p
, sizeof(fcgi_server
));
334 /* Initialize anything who's init state is not zeroizzzzed */
335 s
->listenQueueDepth
= FCGI_DEFAULT_LISTEN_Q
;
336 s
->appConnectTimeout
= FCGI_DEFAULT_APP_CONN_TIMEOUT
;
337 s
->idle_timeout
= FCGI_DEFAULT_IDLE_TIMEOUT
;
338 s
->initStartDelay
= DEFAULT_INIT_START_DELAY
;
339 s
->restartDelay
= FCGI_DEFAULT_RESTART_DELAY
;
340 s
->restartOnExit
= FALSE
;
341 s
->directive
= APP_CLASS_UNKNOWN
;
342 s
->processPriority
= FCGI_DEFAULT_PRIORITY
;
344 s
->listenFd
= (int)INVALID_HANDLE_VALUE
;
348 s
->envp
= &fcgi_empty_env
;
353 /*******************************************************************************
354 * Add the server to the linked list of FastCGI servers.
357 fcgi_util_fs_add(fcgi_server
*s
)
359 s
->next
= fcgi_servers
;
363 /*******************************************************************************
364 * Configure uid, gid, user, group, username for wrapper.
367 fcgi_util_fs_set_uid_n_gid(pool
*p
, fcgi_server
*s
, uid_t uid
, gid_t gid
)
374 if (fcgi_wrapper
== NULL
)
381 return ap_psprintf(p
,
382 "getpwuid() couldn't determine the username for uid '%ld', "
383 "you probably need to modify the User directive: %s",
384 (long)uid
, strerror(errno
));
386 s
->user
= ap_pstrdup(p
, pw
->pw_name
);
387 s
->username
= s
->user
;
392 return ap_psprintf(p
,
393 "getgrgid() couldn't determine the group name for gid '%ld', "
394 "you probably need to modify the Group directive: %s",
395 (long)gid
, strerror(errno
));
397 s
->group
= ap_pstrdup(p
, gr
->gr_name
);
402 /*******************************************************************************
403 * Allocate an array of ServerProcess records.
406 fcgi_util_fs_create_procs(pool
*p
, int num
)
409 ServerProcess
*proc
= (ServerProcess
*)ap_pcalloc(p
, sizeof(ServerProcess
) * num
);
411 for (i
= 0; i
< num
; i
++) {
413 proc
[i
].handle
= INVALID_HANDLE_VALUE
;
416 proc
[i
].state
= STATE_READY
;
422 *----------------------------------------------------------------------
426 * Provide file locking via fcntl(2) function call. This
427 * code has been borrowed from Stevens "Advanced Unix
428 * Programming", page 370.
431 * File descriptor to be locked, offsets within the file.
434 * 0 on successful locking, -1 otherwise
437 * File pointed to by file descriptor is locked or unlocked.
438 * Provided macros allow for both blocking and non-blocking
441 *----------------------------------------------------------------------
446 fcgi_util_lock_fd(int fd
, int cmd
, int type
, off_t offset
, int whence
, off_t len
)
451 lock
.l_type
= type
; /* F_RDLCK, F_WRLCK, F_UNLCK */
452 lock
.l_start
= offset
; /* byte offset, relative to whence */
453 lock
.l_whence
= whence
; /* SEEK_SET, SEET_CUR, SEEK_END */
454 lock
.l_len
= len
; /* # of bytes, (0 indicates to EOF) */
456 /* Don't be fooled into thinking we've set a lock when we've
457 merely caught a signal. */
459 /* This is OK only if there is a hard_timeout() in effect! */
460 while ((res
= fcntl(fd
, cmd
, &lock
)) == -1 && errno
== EINTR
);
465 int fcgi_util_gettimeofday(struct timeval
*Time
) {
470 clock
= GetTickCount();
474 Time
->tv_sec
= t
; //clock / 1000;
475 Time
->tv_usec
= (clock
- Time
->tv_sec
) * 1000;
477 if (Time
->tv_sec
== (time_t)-1)
482 return gettimeofday(Time
, NULL
);
489 * We allow only one writer at a time and a writer can proceed only
490 * when there are no readers.
492 * @@@ For named pipes we could lock only the process we're going to waste.
495 FcgiRWLock
* fcgi_rdwr_create()
497 FcgiRWLock
*newlock
= NULL
;
499 newlock
= (FcgiRWLock
*) malloc(sizeof(FcgiRWLock
));
502 newlock
->write_lock
= CreateEvent(NULL
, FALSE
, TRUE
, NULL
);
503 newlock
->mutex
= CreateMutex(NULL
, FALSE
, NULL
);
504 newlock
->counter
= 0;
509 void fcgi_rdwr_destroy(FcgiRWLock
*lock
)
511 CloseHandle(lock
->write_lock
);
512 CloseHandle(lock
->mutex
);
516 int fcgi_rdwr_lock(FcgiRWLock
*lock
, int type
)
520 WaitForSingleObject(lock
->write_lock
, INFINITE
);
524 WaitForSingleObject(lock
->mutex
, INFINITE
);
525 if (lock
->counter
== 0)
527 WaitForSingleObject(lock
->write_lock
, INFINITE
);
530 ReleaseMutex(lock
->mutex
);
536 int fcgi_rdwr_unlock(FcgiRWLock
*lock
, int type
)
540 SetEvent(lock
->write_lock
);
544 WaitForSingleObject(lock
->mutex
, INFINITE
);
546 if (lock
->counter
== 0)
548 SetEvent(lock
->write_lock
);
550 ReleaseMutex(lock
->mutex
);
556 int fcgi_rdwr_try_lock(FcgiRWLock
*lock
, int type
)
562 rc
= WaitForSingleObject(lock
->write_lock
, 0);
563 if (rc
== WAIT_TIMEOUT
|| rc
== WAIT_FAILED
)