package/base-files: /etc/passwd: fix uid number for user 'daemon' (conflicting with...
[openwrt.git] / package / uhttpd / src / uhttpd-utils.c
1 /*
2 * uhttpd - Tiny single-threaded httpd - Utility functions
3 *
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 #define _XOPEN_SOURCE 500 /* crypt() */
20 #define _BSD_SOURCE /* strcasecmp(), strncasecmp() */
21
22 #include "uhttpd.h"
23 #include "uhttpd-utils.h"
24
25 #ifdef HAVE_TLS
26 #include "uhttpd-tls.h"
27 #endif
28
29
30 static char *uh_index_files[] = {
31 "index.html",
32 "index.htm",
33 "default.html",
34 "default.htm"
35 };
36
37
38 const char * sa_straddr(void *sa)
39 {
40 static char str[INET6_ADDRSTRLEN];
41 struct sockaddr_in *v4 = (struct sockaddr_in *)sa;
42 struct sockaddr_in6 *v6 = (struct sockaddr_in6 *)sa;
43
44 if( v4->sin_family == AF_INET )
45 return inet_ntop(AF_INET, &(v4->sin_addr), str, sizeof(str));
46 else
47 return inet_ntop(AF_INET6, &(v6->sin6_addr), str, sizeof(str));
48 }
49
50 const char * sa_strport(void *sa)
51 {
52 static char str[6];
53 snprintf(str, sizeof(str), "%i", sa_port(sa));
54 return str;
55 }
56
57 int sa_port(void *sa)
58 {
59 return ntohs(((struct sockaddr_in6 *)sa)->sin6_port);
60 }
61
62 int sa_rfc1918(void *sa)
63 {
64 struct sockaddr_in *v4 = (struct sockaddr_in *)sa;
65 unsigned long a = htonl(v4->sin_addr.s_addr);
66
67 if( v4->sin_family == AF_INET )
68 {
69 return ((a >= 0x0A000000) && (a <= 0x0AFFFFFF)) ||
70 ((a >= 0xAC100000) && (a <= 0xAC1FFFFF)) ||
71 ((a >= 0xC0A80000) && (a <= 0xC0A8FFFF));
72 }
73
74 return 0;
75 }
76
77 /* Simple strstr() like function that takes len arguments for both haystack and needle. */
78 char *strfind(char *haystack, int hslen, const char *needle, int ndlen)
79 {
80 int match = 0;
81 int i, j;
82
83 for( i = 0; i < hslen; i++ )
84 {
85 if( haystack[i] == needle[0] )
86 {
87 match = ((ndlen == 1) || ((i + ndlen) <= hslen));
88
89 for( j = 1; (j < ndlen) && ((i + j) < hslen); j++ )
90 {
91 if( haystack[i+j] != needle[j] )
92 {
93 match = 0;
94 break;
95 }
96 }
97
98 if( match )
99 return &haystack[i];
100 }
101 }
102
103 return NULL;
104 }
105
106 /* interruptable select() */
107 int select_intr(int n, fd_set *r, fd_set *w, fd_set *e, struct timeval *t)
108 {
109 int rv;
110 sigset_t ssn, sso;
111
112 /* unblock SIGCHLD */
113 sigemptyset(&ssn);
114 sigaddset(&ssn, SIGCHLD);
115 sigaddset(&ssn, SIGPIPE);
116 sigprocmask(SIG_UNBLOCK, &ssn, &sso);
117
118 rv = select(n, r, w, e, t);
119
120 /* restore signal mask */
121 sigprocmask(SIG_SETMASK, &sso, NULL);
122
123 return rv;
124 }
125
126
127 int uh_tcp_send_lowlevel(struct client *cl, const char *buf, int len)
128 {
129 fd_set writer;
130 struct timeval timeout;
131
132 FD_ZERO(&writer);
133 FD_SET(cl->socket, &writer);
134
135 timeout.tv_sec = cl->server->conf->network_timeout;
136 timeout.tv_usec = 0;
137
138 if (select(cl->socket + 1, NULL, &writer, NULL, &timeout) > 0)
139 return send(cl->socket, buf, len, 0);
140
141 return -1;
142 }
143
144 int uh_tcp_send(struct client *cl, const char *buf, int len)
145 {
146 #ifdef HAVE_TLS
147 if (cl->tls)
148 return cl->server->conf->tls_send(cl, (void *)buf, len);
149 else
150 #endif
151 return uh_tcp_send_lowlevel(cl, buf, len);
152 }
153
154 int uh_tcp_peek(struct client *cl, char *buf, int len)
155 {
156 /* sanity check, prevent overflowing peek buffer */
157 if (len > sizeof(cl->peekbuf))
158 return -1;
159
160 int sz = uh_tcp_recv(cl, buf, len);
161
162 /* store received data in peek buffer */
163 if( sz > 0 )
164 {
165 cl->peeklen = sz;
166 memcpy(cl->peekbuf, buf, sz);
167 }
168
169 return sz;
170 }
171
172 int uh_tcp_recv_lowlevel(struct client *cl, char *buf, int len)
173 {
174 fd_set reader;
175 struct timeval timeout;
176
177 FD_ZERO(&reader);
178 FD_SET(cl->socket, &reader);
179
180 timeout.tv_sec = cl->server->conf->network_timeout;
181 timeout.tv_usec = 0;
182
183 if (select(cl->socket + 1, &reader, NULL, NULL, &timeout) > 0)
184 return recv(cl->socket, buf, len, 0);
185
186 return -1;
187 }
188
189 int uh_tcp_recv(struct client *cl, char *buf, int len)
190 {
191 int sz = 0;
192 int rsz = 0;
193
194 /* first serve data from peek buffer */
195 if (cl->peeklen > 0)
196 {
197 sz = min(cl->peeklen, len);
198 len -= sz; cl->peeklen -= sz;
199 memcpy(buf, cl->peekbuf, sz);
200 memmove(cl->peekbuf, &cl->peekbuf[sz], cl->peeklen);
201 }
202
203 /* caller wants more */
204 if (len > 0)
205 {
206 #ifdef HAVE_TLS
207 if (cl->tls)
208 rsz = cl->server->conf->tls_recv(cl, (void *)&buf[sz], len);
209 else
210 #endif
211 rsz = uh_tcp_recv_lowlevel(cl, (void *)&buf[sz], len);
212
213 if (rsz < 0)
214 return rsz;
215
216 sz += rsz;
217 }
218
219 return sz;
220 }
221
222
223 int uh_http_sendhf(struct client *cl, int code, const char *summary, const char *fmt, ...)
224 {
225 va_list ap;
226
227 char buffer[UH_LIMIT_MSGHEAD];
228 int len;
229
230 len = snprintf(buffer, sizeof(buffer),
231 "HTTP/1.1 %03i %s\r\n"
232 "Connection: close\r\n"
233 "Content-Type: text/plain\r\n"
234 "Transfer-Encoding: chunked\r\n\r\n",
235 code, summary
236 );
237
238 ensure_ret(uh_tcp_send(cl, buffer, len));
239
240 va_start(ap, fmt);
241 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
242 va_end(ap);
243
244 ensure_ret(uh_http_sendc(cl, buffer, len));
245 ensure_ret(uh_http_sendc(cl, NULL, 0));
246
247 return 0;
248 }
249
250
251 int uh_http_sendc(struct client *cl, const char *data, int len)
252 {
253 char chunk[8];
254 int clen;
255
256 if( len == -1 )
257 len = strlen(data);
258
259 if( len > 0 )
260 {
261 clen = snprintf(chunk, sizeof(chunk), "%X\r\n", len);
262 ensure_ret(uh_tcp_send(cl, chunk, clen));
263 ensure_ret(uh_tcp_send(cl, data, len));
264 ensure_ret(uh_tcp_send(cl, "\r\n", 2));
265 }
266 else
267 {
268 ensure_ret(uh_tcp_send(cl, "0\r\n\r\n", 5));
269 }
270
271 return 0;
272 }
273
274 int uh_http_sendf(
275 struct client *cl, struct http_request *req, const char *fmt, ...
276 ) {
277 va_list ap;
278 char buffer[UH_LIMIT_MSGHEAD];
279 int len;
280
281 va_start(ap, fmt);
282 len = vsnprintf(buffer, sizeof(buffer), fmt, ap);
283 va_end(ap);
284
285 if( (req != NULL) && (req->version > 1.0) )
286 ensure_ret(uh_http_sendc(cl, buffer, len));
287 else if( len > 0 )
288 ensure_ret(uh_tcp_send(cl, buffer, len));
289
290 return 0;
291 }
292
293 int uh_http_send(
294 struct client *cl, struct http_request *req, const char *buf, int len
295 ) {
296 if( len < 0 )
297 len = strlen(buf);
298
299 if( (req != NULL) && (req->version > 1.0) )
300 ensure_ret(uh_http_sendc(cl, buf, len));
301 else if( len > 0 )
302 ensure_ret(uh_tcp_send(cl, buf, len));
303
304 return 0;
305 }
306
307
308 int uh_urldecode(char *buf, int blen, const char *src, int slen)
309 {
310 int i;
311 int len = 0;
312
313 #define hex(x) \
314 (((x) <= '9') ? ((x) - '0') : \
315 (((x) <= 'F') ? ((x) - 'A' + 10) : \
316 ((x) - 'a' + 10)))
317
318 for( i = 0; (i <= slen) && (i <= blen); i++ )
319 {
320 if( src[i] == '%' )
321 {
322 if( ((i+2) <= slen) && isxdigit(src[i+1]) && isxdigit(src[i+2]) )
323 {
324 buf[len++] = (char)(16 * hex(src[i+1]) + hex(src[i+2]));
325 i += 2;
326 }
327 else
328 {
329 buf[len++] = '%';
330 }
331 }
332 else
333 {
334 buf[len++] = src[i];
335 }
336 }
337
338 return len;
339 }
340
341 int uh_urlencode(char *buf, int blen, const char *src, int slen)
342 {
343 int i;
344 int len = 0;
345 const char hex[] = "0123456789abcdef";
346
347 for( i = 0; (i <= slen) && (i <= blen); i++ )
348 {
349 if( isalnum(src[i]) || (src[i] == '-') || (src[i] == '_') ||
350 (src[i] == '.') || (src[i] == '~') )
351 {
352 buf[len++] = src[i];
353 }
354 else if( (len+3) <= blen )
355 {
356 buf[len++] = '%';
357 buf[len++] = hex[(src[i] >> 4) & 15];
358 buf[len++] = hex[(src[i] & 15) & 15];
359 }
360 else
361 {
362 break;
363 }
364 }
365
366 return len;
367 }
368
369 int uh_b64decode(char *buf, int blen, const unsigned char *src, int slen)
370 {
371 int i = 0;
372 int len = 0;
373
374 unsigned int cin = 0;
375 unsigned int cout = 0;
376
377
378 for( i = 0; (i <= slen) && (src[i] != 0); i++ )
379 {
380 cin = src[i];
381
382 if( (cin >= '0') && (cin <= '9') )
383 cin = cin - '0' + 52;
384 else if( (cin >= 'A') && (cin <= 'Z') )
385 cin = cin - 'A';
386 else if( (cin >= 'a') && (cin <= 'z') )
387 cin = cin - 'a' + 26;
388 else if( cin == '+' )
389 cin = 62;
390 else if( cin == '/' )
391 cin = 63;
392 else if( cin == '=' )
393 cin = 0;
394 else
395 continue;
396
397 cout = (cout << 6) | cin;
398
399 if( (i % 4) == 3 )
400 {
401 if( (len + 3) < blen )
402 {
403 buf[len++] = (char)(cout >> 16);
404 buf[len++] = (char)(cout >> 8);
405 buf[len++] = (char)(cout);
406 }
407 else
408 {
409 break;
410 }
411 }
412 }
413
414 buf[len++] = 0;
415 return len;
416 }
417
418 static char * canonpath(const char *path, char *path_resolved)
419 {
420 char path_copy[PATH_MAX];
421 char *path_cpy = path_copy;
422 char *path_res = path_resolved;
423
424 struct stat s;
425
426
427 /* relative -> absolute */
428 if( *path != '/' )
429 {
430 getcwd(path_copy, PATH_MAX);
431 strncat(path_copy, "/", PATH_MAX - strlen(path_copy));
432 strncat(path_copy, path, PATH_MAX - strlen(path_copy));
433 }
434 else
435 {
436 strncpy(path_copy, path, PATH_MAX);
437 }
438
439 /* normalize */
440 while( (*path_cpy != '\0') && (path_cpy < (path_copy + PATH_MAX - 2)) )
441 {
442 if( *path_cpy == '/' )
443 {
444 /* skip repeating / */
445 if( path_cpy[1] == '/' )
446 {
447 path_cpy++;
448 continue;
449 }
450
451 /* /./ or /../ */
452 else if( path_cpy[1] == '.' )
453 {
454 /* skip /./ */
455 if( (path_cpy[2] == '/') || (path_cpy[2] == '\0') )
456 {
457 path_cpy += 2;
458 continue;
459 }
460
461 /* collapse /x/../ */
462 else if( (path_cpy[2] == '.') &&
463 ((path_cpy[3] == '/') || (path_cpy[3] == '\0'))
464 ) {
465 while( (path_res > path_resolved) && (*--path_res != '/') )
466 ;
467
468 path_cpy += 3;
469 continue;
470 }
471 }
472 }
473
474 *path_res++ = *path_cpy++;
475 }
476
477 /* remove trailing slash if not root / */
478 if( (path_res > (path_resolved+1)) && (path_res[-1] == '/') )
479 path_res--;
480 else if( path_res == path_resolved )
481 *path_res++ = '/';
482
483 *path_res = '\0';
484
485 /* test access */
486 if( !stat(path_resolved, &s) && (s.st_mode & S_IROTH) )
487 return path_resolved;
488
489 return NULL;
490 }
491
492 struct path_info * uh_path_lookup(struct client *cl, const char *url)
493 {
494 static char path_phys[PATH_MAX];
495 static char path_info[PATH_MAX];
496 static struct path_info p;
497
498 char buffer[UH_LIMIT_MSGHEAD];
499 char *docroot = cl->server->conf->docroot;
500 char *pathptr = NULL;
501
502 int slash = 0;
503 int no_sym = cl->server->conf->no_symlinks;
504 int i = 0;
505 struct stat s;
506
507 /* back out early if url is undefined */
508 if ( url == NULL )
509 return NULL;
510
511 memset(path_phys, 0, sizeof(path_phys));
512 memset(path_info, 0, sizeof(path_info));
513 memset(buffer, 0, sizeof(buffer));
514 memset(&p, 0, sizeof(p));
515
516 /* copy docroot */
517 memcpy(buffer, docroot,
518 min(strlen(docroot), sizeof(buffer) - 1));
519
520 /* separate query string from url */
521 if( (pathptr = strchr(url, '?')) != NULL )
522 {
523 p.query = pathptr[1] ? pathptr + 1 : NULL;
524
525 /* urldecode component w/o query */
526 if( pathptr > url )
527 uh_urldecode(
528 &buffer[strlen(docroot)],
529 sizeof(buffer) - strlen(docroot) - 1,
530 url, (int)(pathptr - url) - 1
531 );
532 }
533
534 /* no query string, decode all of url */
535 else
536 {
537 uh_urldecode(
538 &buffer[strlen(docroot)],
539 sizeof(buffer) - strlen(docroot) - 1,
540 url, strlen(url)
541 );
542 }
543
544 /* create canon path */
545 for( i = strlen(buffer), slash = (buffer[max(0, i-1)] == '/'); i >= 0; i-- )
546 {
547 if( (buffer[i] == 0) || (buffer[i] == '/') )
548 {
549 memset(path_info, 0, sizeof(path_info));
550 memcpy(path_info, buffer, min(i + 1, sizeof(path_info) - 1));
551
552 if( no_sym ? realpath(path_info, path_phys)
553 : canonpath(path_info, path_phys)
554 ) {
555 memset(path_info, 0, sizeof(path_info));
556 memcpy(path_info, &buffer[i],
557 min(strlen(buffer) - i, sizeof(path_info) - 1));
558
559 break;
560 }
561 }
562 }
563
564 /* check whether found path is within docroot */
565 if( strncmp(path_phys, docroot, strlen(docroot)) ||
566 ((path_phys[strlen(docroot)] != 0) &&
567 (path_phys[strlen(docroot)] != '/'))
568 ) {
569 return NULL;
570 }
571
572 /* test current path */
573 if( ! stat(path_phys, &p.stat) )
574 {
575 /* is a regular file */
576 if( p.stat.st_mode & S_IFREG )
577 {
578 p.root = docroot;
579 p.phys = path_phys;
580 p.name = &path_phys[strlen(docroot)];
581 p.info = path_info[0] ? path_info : NULL;
582 }
583
584 /* is a directory */
585 else if( (p.stat.st_mode & S_IFDIR) && !strlen(path_info) )
586 {
587 /* ensure trailing slash */
588 if( path_phys[strlen(path_phys)-1] != '/' )
589 path_phys[strlen(path_phys)] = '/';
590
591 /* try to locate index file */
592 memset(buffer, 0, sizeof(buffer));
593 memcpy(buffer, path_phys, sizeof(buffer));
594 pathptr = &buffer[strlen(buffer)];
595
596 /* if requested url resolves to a directory and a trailing slash
597 is missing in the request url, redirect the client to the same
598 url with trailing slash appended */
599 if( !slash )
600 {
601 uh_http_sendf(cl, NULL,
602 "HTTP/1.1 302 Found\r\n"
603 "Location: %s%s%s\r\n"
604 "Connection: close\r\n\r\n",
605 &path_phys[strlen(docroot)],
606 p.query ? "?" : "",
607 p.query ? p.query : ""
608 );
609
610 p.redirected = 1;
611 }
612 else if( cl->server->conf->index_file )
613 {
614 strncat(buffer, cl->server->conf->index_file, sizeof(buffer));
615
616 if( !stat(buffer, &s) && (s.st_mode & S_IFREG) )
617 {
618 memcpy(path_phys, buffer, sizeof(path_phys));
619 memcpy(&p.stat, &s, sizeof(p.stat));
620 }
621 }
622 else
623 {
624 for( i = 0; i < array_size(uh_index_files); i++ )
625 {
626 strncat(buffer, uh_index_files[i], sizeof(buffer));
627
628 if( !stat(buffer, &s) && (s.st_mode & S_IFREG) )
629 {
630 memcpy(path_phys, buffer, sizeof(path_phys));
631 memcpy(&p.stat, &s, sizeof(p.stat));
632 break;
633 }
634
635 *pathptr = 0;
636 }
637 }
638
639 p.root = docroot;
640 p.phys = path_phys;
641 p.name = &path_phys[strlen(docroot)];
642 }
643 }
644
645 return p.phys ? &p : NULL;
646 }
647
648
649 static struct auth_realm *uh_realms = NULL;
650
651 struct auth_realm * uh_auth_add(char *path, char *user, char *pass)
652 {
653 struct auth_realm *new = NULL;
654 struct passwd *pwd;
655
656 #ifdef HAVE_SHADOW
657 struct spwd *spwd;
658 #endif
659
660 if((new = (struct auth_realm *)malloc(sizeof(struct auth_realm))) != NULL)
661 {
662 memset(new, 0, sizeof(struct auth_realm));
663
664 memcpy(new->path, path,
665 min(strlen(path), sizeof(new->path) - 1));
666
667 memcpy(new->user, user,
668 min(strlen(user), sizeof(new->user) - 1));
669
670 /* given password refers to a passwd entry */
671 if( (strlen(pass) > 3) && !strncmp(pass, "$p$", 3) )
672 {
673 #ifdef HAVE_SHADOW
674 /* try to resolve shadow entry */
675 if( ((spwd = getspnam(&pass[3])) != NULL) && spwd->sp_pwdp )
676 {
677 memcpy(new->pass, spwd->sp_pwdp,
678 min(strlen(spwd->sp_pwdp), sizeof(new->pass) - 1));
679 }
680
681 else
682 #endif
683
684 /* try to resolve passwd entry */
685 if( ((pwd = getpwnam(&pass[3])) != NULL) && pwd->pw_passwd &&
686 (pwd->pw_passwd[0] != '!') && (pwd->pw_passwd[0] != 0)
687 ) {
688 memcpy(new->pass, pwd->pw_passwd,
689 min(strlen(pwd->pw_passwd), sizeof(new->pass) - 1));
690 }
691 }
692
693 /* ordinary pwd */
694 else
695 {
696 memcpy(new->pass, pass,
697 min(strlen(pass), sizeof(new->pass) - 1));
698 }
699
700 if( new->pass[0] )
701 {
702 new->next = uh_realms;
703 uh_realms = new;
704
705 return new;
706 }
707
708 free(new);
709 }
710
711 return NULL;
712 }
713
714 int uh_auth_check(
715 struct client *cl, struct http_request *req, struct path_info *pi
716 ) {
717 int i, plen, rlen, protected;
718 char buffer[UH_LIMIT_MSGHEAD];
719 char *user = NULL;
720 char *pass = NULL;
721
722 struct auth_realm *realm = NULL;
723
724 plen = strlen(pi->name);
725 protected = 0;
726
727 /* check whether at least one realm covers the requested url */
728 for( realm = uh_realms; realm; realm = realm->next )
729 {
730 rlen = strlen(realm->path);
731
732 if( (plen >= rlen) && !strncasecmp(pi->name, realm->path, rlen) )
733 {
734 req->realm = realm;
735 protected = 1;
736 break;
737 }
738 }
739
740 /* requested resource is covered by a realm */
741 if( protected )
742 {
743 /* try to get client auth info */
744 foreach_header(i, req->headers)
745 {
746 if( !strcasecmp(req->headers[i], "Authorization") &&
747 (strlen(req->headers[i+1]) > 6) &&
748 !strncasecmp(req->headers[i+1], "Basic ", 6)
749 ) {
750 memset(buffer, 0, sizeof(buffer));
751 uh_b64decode(buffer, sizeof(buffer) - 1,
752 (unsigned char *) &req->headers[i+1][6],
753 strlen(req->headers[i+1]) - 6);
754
755 if( (pass = strchr(buffer, ':')) != NULL )
756 {
757 user = buffer;
758 *pass++ = 0;
759 }
760
761 break;
762 }
763 }
764
765 /* have client auth */
766 if( user && pass )
767 {
768 /* find matching realm */
769 for( realm = uh_realms; realm; realm = realm->next )
770 {
771 rlen = strlen(realm->path);
772
773 if( (plen >= rlen) &&
774 !strncasecmp(pi->name, realm->path, rlen) &&
775 !strcmp(user, realm->user)
776 ) {
777 req->realm = realm;
778 break;
779 }
780 }
781
782 /* found a realm matching the username */
783 if( realm )
784 {
785 /* is a crypt passwd */
786 if( realm->pass[0] == '$' )
787 pass = crypt(pass, realm->pass);
788
789 /* check user pass */
790 if( !strcmp(pass, realm->pass) )
791 return 1;
792 }
793 }
794
795 /* 401 */
796 uh_http_sendf(cl, NULL,
797 "HTTP/%.1f 401 Authorization Required\r\n"
798 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
799 "Content-Type: text/plain\r\n"
800 "Content-Length: 23\r\n\r\n"
801 "Authorization Required\n",
802 req->version, cl->server->conf->realm
803 );
804
805 return 0;
806 }
807
808 return 1;
809 }
810
811
812 static struct listener *uh_listeners = NULL;
813 static struct client *uh_clients = NULL;
814
815 struct listener * uh_listener_add(int sock, struct config *conf)
816 {
817 struct listener *new = NULL;
818 socklen_t sl;
819
820 if( (new = (struct listener *)malloc(sizeof(struct listener))) != NULL )
821 {
822 memset(new, 0, sizeof(struct listener));
823
824 new->socket = sock;
825 new->conf = conf;
826
827 /* get local endpoint addr */
828 sl = sizeof(struct sockaddr_in6);
829 memset(&(new->addr), 0, sl);
830 getsockname(sock, (struct sockaddr *) &(new->addr), &sl);
831
832 new->next = uh_listeners;
833 uh_listeners = new;
834
835 return new;
836 }
837
838 return NULL;
839 }
840
841 struct listener * uh_listener_lookup(int sock)
842 {
843 struct listener *cur = NULL;
844
845 for( cur = uh_listeners; cur; cur = cur->next )
846 if( cur->socket == sock )
847 return cur;
848
849 return NULL;
850 }
851
852
853 struct client * uh_client_add(int sock, struct listener *serv)
854 {
855 struct client *new = NULL;
856 socklen_t sl;
857
858 if( (new = (struct client *)malloc(sizeof(struct client))) != NULL )
859 {
860 memset(new, 0, sizeof(struct client));
861
862 new->socket = sock;
863 new->server = serv;
864
865 /* get remote endpoint addr */
866 sl = sizeof(struct sockaddr_in6);
867 memset(&(new->peeraddr), 0, sl);
868 getpeername(sock, (struct sockaddr *) &(new->peeraddr), &sl);
869
870 /* get local endpoint addr */
871 sl = sizeof(struct sockaddr_in6);
872 memset(&(new->servaddr), 0, sl);
873 getsockname(sock, (struct sockaddr *) &(new->servaddr), &sl);
874
875 new->next = uh_clients;
876 uh_clients = new;
877 }
878
879 return new;
880 }
881
882 struct client * uh_client_lookup(int sock)
883 {
884 struct client *cur = NULL;
885
886 for( cur = uh_clients; cur; cur = cur->next )
887 if( cur->socket == sock )
888 return cur;
889
890 return NULL;
891 }
892
893 void uh_client_remove(int sock)
894 {
895 struct client *cur = NULL;
896 struct client *prv = NULL;
897
898 for( cur = uh_clients; cur; prv = cur, cur = cur->next )
899 {
900 if( cur->socket == sock )
901 {
902 if( prv )
903 prv->next = cur->next;
904 else
905 uh_clients = cur->next;
906
907 free(cur);
908 break;
909 }
910 }
911 }
912
913
914 #ifdef HAVE_CGI
915 static struct interpreter *uh_interpreters = NULL;
916
917 struct interpreter * uh_interpreter_add(const char *extn, const char *path)
918 {
919 struct interpreter *new = NULL;
920
921 if( (new = (struct interpreter *)
922 malloc(sizeof(struct interpreter))) != NULL )
923 {
924 memset(new, 0, sizeof(struct interpreter));
925
926 memcpy(new->extn, extn, min(strlen(extn), sizeof(new->extn)-1));
927 memcpy(new->path, path, min(strlen(path), sizeof(new->path)-1));
928
929 new->next = uh_interpreters;
930 uh_interpreters = new;
931
932 return new;
933 }
934
935 return NULL;
936 }
937
938 struct interpreter * uh_interpreter_lookup(const char *path)
939 {
940 struct interpreter *cur = NULL;
941 const char *e;
942
943 for( cur = uh_interpreters; cur; cur = cur->next )
944 {
945 e = &path[max(strlen(path) - strlen(cur->extn), 0)];
946
947 if( !strcmp(e, cur->extn) )
948 return cur;
949 }
950
951 return NULL;
952 }
953 #endif
This page took 0.107408 seconds and 5 git commands to generate.