4a1423c715d0db9e9b6f7d1ff44f34250574b336
2 * uhttpd - Tiny single-threaded httpd - Utility functions
4 * Copyright (C) 2010 Jo-Philipp Wich <xm@subsignal.org>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #define _XOPEN_SOURCE 500 /* crypt() */
20 #define _BSD_SOURCE /* strcasecmp(), strncasecmp() */
23 #include "uhttpd-utils.h"
26 #include "uhttpd-tls.h"
30 static char *uh_index_files
[] = {
38 const char * sa_straddr(void *sa
)
40 static char str
[INET6_ADDRSTRLEN
];
41 struct sockaddr_in
*v4
= (struct sockaddr_in
*)sa
;
42 struct sockaddr_in6
*v6
= (struct sockaddr_in6
*)sa
;
44 if( v4
->sin_family
== AF_INET
)
45 return inet_ntop(AF_INET
, &(v4
->sin_addr
), str
, sizeof(str
));
47 return inet_ntop(AF_INET6
, &(v6
->sin6_addr
), str
, sizeof(str
));
50 const char * sa_strport(void *sa
)
53 snprintf(str
, sizeof(str
), "%i", sa_port(sa
));
59 return ntohs(((struct sockaddr_in6
*)sa
)->sin6_port
);
62 int sa_rfc1918(void *sa
)
64 struct sockaddr_in
*v4
= (struct sockaddr_in
*)sa
;
65 unsigned long a
= htonl(v4
->sin_addr
.s_addr
);
67 if( v4
->sin_family
== AF_INET
)
69 return ((a
>= 0x0A000000) && (a
<= 0x0AFFFFFF)) ||
70 ((a
>= 0xAC100000) && (a
<= 0xAC1FFFFF)) ||
71 ((a
>= 0xC0A80000) && (a
<= 0xC0A8FFFF));
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
)
83 for( i
= 0; i
< hslen
; i
++ )
85 if( haystack
[i
] == needle
[0] )
87 match
= ((ndlen
== 1) || ((i
+ ndlen
) <= hslen
));
89 for( j
= 1; (j
< ndlen
) && ((i
+ j
) < hslen
); j
++ )
91 if( haystack
[i
+j
] != needle
[j
] )
106 /* interruptable select() */
107 int select_intr(int n
, fd_set
*r
, fd_set
*w
, fd_set
*e
, struct timeval
*t
)
112 /* unblock SIGCHLD */
114 sigaddset(&ssn
, SIGCHLD
);
115 sigprocmask(SIG_UNBLOCK
, &ssn
, &sso
);
117 rv
= select(n
, r
, w
, e
, t
);
119 /* restore signal mask */
120 sigprocmask(SIG_SETMASK
, &sso
, NULL
);
126 int uh_tcp_send(struct client
*cl
, const char *buf
, int len
)
129 struct timeval timeout
;
132 FD_SET(cl
->socket
, &writer
);
134 timeout
.tv_sec
= cl
->server
->conf
->network_timeout
;
137 if( select(cl
->socket
+ 1, NULL
, &writer
, NULL
, &timeout
) > 0 )
141 return cl
->server
->conf
->tls_send(cl
, (void *)buf
, len
);
144 return send(cl
->socket
, buf
, len
, 0);
150 int uh_tcp_peek(struct client
*cl
, char *buf
, int len
)
152 int sz
= uh_tcp_recv(cl
, buf
, len
);
154 /* store received data in peek buffer */
158 memcpy(cl
->peekbuf
, buf
, sz
);
164 int uh_tcp_recv(struct client
*cl
, char *buf
, int len
)
169 /* first serve data from peek buffer */
170 if( cl
->peeklen
> 0 )
172 sz
= min(cl
->peeklen
, len
);
173 len
-= sz
; cl
->peeklen
-= sz
;
175 memcpy(buf
, cl
->peekbuf
, sz
);
176 memmove(cl
->peekbuf
, &cl
->peekbuf
[sz
], cl
->peeklen
);
179 /* caller wants more */
184 rsz
= cl
->server
->conf
->tls_recv(cl
, (void *)&buf
[sz
], len
);
187 rsz
= recv(cl
->socket
, (void *)&buf
[sz
], len
, 0);
189 if( (sz
== 0) || (rsz
> 0) )
197 do { if( x < 0 ) return -1; } while(0)
199 int uh_http_sendhf(struct client
*cl
, int code
, const char *summary
, const char *fmt
, ...)
203 char buffer
[UH_LIMIT_MSGHEAD
];
206 len
= snprintf(buffer
, sizeof(buffer
),
207 "HTTP/1.1 %03i %s\r\n"
208 "Connection: close\r\n"
209 "Content-Type: text/plain\r\n"
210 "Transfer-Encoding: chunked\r\n\r\n",
214 ensure(uh_tcp_send(cl
, buffer
, len
));
217 len
= vsnprintf(buffer
, sizeof(buffer
), fmt
, ap
);
220 ensure(uh_http_sendc(cl
, buffer
, len
));
221 ensure(uh_http_sendc(cl
, NULL
, 0));
227 int uh_http_sendc(struct client
*cl
, const char *data
, int len
)
237 clen
= snprintf(chunk
, sizeof(chunk
), "%X\r\n", len
);
238 ensure(uh_tcp_send(cl
, chunk
, clen
));
239 ensure(uh_tcp_send(cl
, data
, len
));
240 ensure(uh_tcp_send(cl
, "\r\n", 2));
244 ensure(uh_tcp_send(cl
, "0\r\n\r\n", 5));
251 struct client
*cl
, struct http_request
*req
, const char *fmt
, ...
254 char buffer
[UH_LIMIT_MSGHEAD
];
258 len
= vsnprintf(buffer
, sizeof(buffer
), fmt
, ap
);
261 if( (req
!= NULL
) && (req
->version
> 1.0) )
262 ensure(uh_http_sendc(cl
, buffer
, len
));
264 ensure(uh_tcp_send(cl
, buffer
, len
));
270 struct client
*cl
, struct http_request
*req
, const char *buf
, int len
275 if( (req
!= NULL
) && (req
->version
> 1.0) )
276 ensure(uh_http_sendc(cl
, buf
, len
));
278 ensure(uh_tcp_send(cl
, buf
, len
));
284 int uh_urldecode(char *buf
, int blen
, const char *src
, int slen
)
290 (((x) <= '9') ? ((x) - '0') : \
291 (((x) <= 'F') ? ((x) - 'A' + 10) : \
294 for( i
= 0; (i
<= slen
) && (i
<= blen
); i
++ )
298 if( ((i
+2) <= slen
) && isxdigit(src
[i
+1]) && isxdigit(src
[i
+2]) )
300 buf
[len
++] = (char)(16 * hex(src
[i
+1]) + hex(src
[i
+2]));
317 int uh_urlencode(char *buf
, int blen
, const char *src
, int slen
)
321 const char hex
[] = "0123456789abcdef";
323 for( i
= 0; (i
<= slen
) && (i
<= blen
); i
++ )
325 if( isalnum(src
[i
]) || (src
[i
] == '-') || (src
[i
] == '_') ||
326 (src
[i
] == '.') || (src
[i
] == '~') )
330 else if( (len
+3) <= blen
)
333 buf
[len
++] = hex
[(src
[i
] >> 4) & 15];
334 buf
[len
++] = hex
[(src
[i
] & 15) & 15];
345 int uh_b64decode(char *buf
, int blen
, const unsigned char *src
, int slen
)
350 unsigned int cin
= 0;
351 unsigned int cout
= 0;
354 for( i
= 0; (i
<= slen
) && (src
[i
] != 0); i
++ )
358 if( (cin
>= '0') && (cin
<= '9') )
359 cin
= cin
- '0' + 52;
360 else if( (cin
>= 'A') && (cin
<= 'Z') )
362 else if( (cin
>= 'a') && (cin
<= 'z') )
363 cin
= cin
- 'a' + 26;
364 else if( cin
== '+' )
366 else if( cin
== '/' )
368 else if( cin
== '=' )
373 cout
= (cout
<< 6) | cin
;
377 if( (len
+ 3) < blen
)
379 buf
[len
++] = (char)(cout
>> 16);
380 buf
[len
++] = (char)(cout
>> 8);
381 buf
[len
++] = (char)(cout
);
394 static char * canonpath(const char *path
, char *path_resolved
)
396 char path_copy
[PATH_MAX
];
397 char *path_cpy
= path_copy
;
398 char *path_res
= path_resolved
;
403 /* relative -> absolute */
406 getcwd(path_copy
, PATH_MAX
);
407 strncat(path_copy
, "/", PATH_MAX
- strlen(path_copy
));
408 strncat(path_copy
, path
, PATH_MAX
- strlen(path_copy
));
412 strncpy(path_copy
, path
, PATH_MAX
);
416 while( (*path_cpy
!= '\0') && (path_cpy
< (path_copy
+ PATH_MAX
- 2)) )
418 if( *path_cpy
== '/' )
420 /* skip repeating / */
421 if( path_cpy
[1] == '/' )
428 else if( path_cpy
[1] == '.' )
431 if( (path_cpy
[2] == '/') || (path_cpy
[2] == '\0') )
437 /* collapse /x/../ */
438 else if( (path_cpy
[2] == '.') &&
439 ((path_cpy
[3] == '/') || (path_cpy
[3] == '\0'))
441 while( (path_res
> path_resolved
) && (*--path_res
!= '/') )
450 *path_res
++ = *path_cpy
++;
453 /* remove trailing slash if not root / */
454 if( (path_res
> (path_resolved
+1)) && (path_res
[-1] == '/') )
456 else if( path_res
== path_resolved
)
462 if( !stat(path_resolved
, &s
) && (s
.st_mode
& S_IROTH
) )
463 return path_resolved
;
468 struct path_info
* uh_path_lookup(struct client
*cl
, const char *url
)
470 static char path_phys
[PATH_MAX
];
471 static char path_info
[PATH_MAX
];
472 static struct path_info p
;
474 char buffer
[UH_LIMIT_MSGHEAD
];
475 char *docroot
= cl
->server
->conf
->docroot
;
476 char *pathptr
= NULL
;
478 int no_sym
= cl
->server
->conf
->no_symlinks
;
482 /* back out early if url is undefined */
486 memset(path_phys
, 0, sizeof(path_phys
));
487 memset(path_info
, 0, sizeof(path_info
));
488 memset(buffer
, 0, sizeof(buffer
));
489 memset(&p
, 0, sizeof(p
));
492 memcpy(buffer
, docroot
,
493 min(strlen(docroot
), sizeof(buffer
) - 1));
495 /* separate query string from url */
496 if( (pathptr
= strchr(url
, '?')) != NULL
)
498 p
.query
= pathptr
[1] ? pathptr
+ 1 : NULL
;
500 /* urldecode component w/o query */
503 &buffer
[strlen(docroot
)],
504 sizeof(buffer
) - strlen(docroot
) - 1,
505 url
, (int)(pathptr
- url
) - 1
509 /* no query string, decode all of url */
513 &buffer
[strlen(docroot
)],
514 sizeof(buffer
) - strlen(docroot
) - 1,
519 /* create canon path */
520 for( i
= strlen(buffer
); i
>= 0; i
-- )
522 if( (buffer
[i
] == 0) || (buffer
[i
] == '/') )
524 memset(path_info
, 0, sizeof(path_info
));
525 memcpy(path_info
, buffer
, min(i
+ 1, sizeof(path_info
) - 1));
527 if( no_sym
? realpath(path_info
, path_phys
)
528 : canonpath(path_info
, path_phys
)
530 memset(path_info
, 0, sizeof(path_info
));
531 memcpy(path_info
, &buffer
[i
],
532 min(strlen(buffer
) - i
, sizeof(path_info
) - 1));
539 /* check whether found path is within docroot */
540 if( strncmp(path_phys
, docroot
, strlen(docroot
)) ||
541 ((path_phys
[strlen(docroot
)] != 0) &&
542 (path_phys
[strlen(docroot
)] != '/'))
547 /* test current path */
548 if( ! stat(path_phys
, &p
.stat
) )
550 /* is a regular file */
551 if( p
.stat
.st_mode
& S_IFREG
)
555 p
.name
= &path_phys
[strlen(docroot
)];
556 p
.info
= path_info
[0] ? path_info
: NULL
;
560 else if( (p
.stat
.st_mode
& S_IFDIR
) && !strlen(path_info
) )
562 /* ensure trailing slash */
563 if( path_phys
[strlen(path_phys
)-1] != '/' )
564 path_phys
[strlen(path_phys
)] = '/';
566 /* try to locate index file */
567 memset(buffer
, 0, sizeof(buffer
));
568 memcpy(buffer
, path_phys
, sizeof(buffer
));
569 pathptr
= &buffer
[strlen(buffer
)];
571 if( cl
->server
->conf
->index_file
)
573 strncat(buffer
, cl
->server
->conf
->index_file
, sizeof(buffer
));
575 if( !stat(buffer
, &s
) && (s
.st_mode
& S_IFREG
) )
577 memcpy(path_phys
, buffer
, sizeof(path_phys
));
578 memcpy(&p
.stat
, &s
, sizeof(p
.stat
));
583 for( i
= 0; i
< array_size(uh_index_files
); i
++ )
585 strncat(buffer
, uh_index_files
[i
], sizeof(buffer
));
587 if( !stat(buffer
, &s
) && (s
.st_mode
& S_IFREG
) )
589 memcpy(path_phys
, buffer
, sizeof(path_phys
));
590 memcpy(&p
.stat
, &s
, sizeof(p
.stat
));
600 p
.name
= &path_phys
[strlen(docroot
)];
604 return p
.phys
? &p
: NULL
;
608 static char uh_realms
[UH_LIMIT_AUTHREALMS
* sizeof(struct auth_realm
)] = { 0 };
609 static int uh_realm_count
= 0;
611 struct auth_realm
* uh_auth_add(char *path
, char *user
, char *pass
)
613 struct auth_realm
*new = NULL
;
617 if( uh_realm_count
< UH_LIMIT_AUTHREALMS
)
619 new = (struct auth_realm
*)
620 &uh_realms
[uh_realm_count
* sizeof(struct auth_realm
)];
622 memset(new, 0, sizeof(struct auth_realm
));
624 memcpy(new->path
, path
,
625 min(strlen(path
), sizeof(new->path
) - 1));
627 memcpy(new->user
, user
,
628 min(strlen(user
), sizeof(new->user
) - 1));
630 /* given password refers to a passwd entry */
631 if( (strlen(pass
) > 3) && !strncmp(pass
, "$p$", 3) )
633 /* try to resolve shadow entry */
634 if( ((spwd
= getspnam(&pass
[3])) != NULL
) && spwd
->sp_pwdp
)
636 memcpy(new->pass
, spwd
->sp_pwdp
,
637 min(strlen(spwd
->sp_pwdp
), sizeof(new->pass
) - 1));
640 /* try to resolve passwd entry */
641 else if( ((pwd
= getpwnam(&pass
[3])) != NULL
) && pwd
->pw_passwd
&&
642 (pwd
->pw_passwd
[0] != '!') && (pwd
->pw_passwd
[0] != 0)
644 memcpy(new->pass
, pwd
->pw_passwd
,
645 min(strlen(pwd
->pw_passwd
), sizeof(new->pass
) - 1));
652 memcpy(new->pass
, pass
,
653 min(strlen(pass
), sizeof(new->pass
) - 1));
667 struct client
*cl
, struct http_request
*req
, struct path_info
*pi
669 int i
, plen
, rlen
, protected;
670 char buffer
[UH_LIMIT_MSGHEAD
];
674 struct auth_realm
*realm
= NULL
;
676 plen
= strlen(pi
->name
);
679 /* check whether at least one realm covers the requested url */
680 for( i
= 0; i
< uh_realm_count
; i
++ )
682 realm
= (struct auth_realm
*)
683 &uh_realms
[i
* sizeof(struct auth_realm
)];
685 rlen
= strlen(realm
->path
);
687 if( (plen
>= rlen
) && !strncasecmp(pi
->name
, realm
->path
, rlen
) )
695 /* requested resource is covered by a realm */
698 /* try to get client auth info */
699 foreach_header(i
, req
->headers
)
701 if( !strcasecmp(req
->headers
[i
], "Authorization") &&
702 (strlen(req
->headers
[i
+1]) > 6) &&
703 !strncasecmp(req
->headers
[i
+1], "Basic ", 6)
705 memset(buffer
, 0, sizeof(buffer
));
706 uh_b64decode(buffer
, sizeof(buffer
) - 1,
707 (unsigned char *) &req
->headers
[i
+1][6],
708 strlen(req
->headers
[i
+1]) - 6);
710 if( (pass
= strchr(buffer
, ':')) != NULL
)
720 /* have client auth */
723 /* find matching realm */
724 for( i
= 0, realm
= NULL
; i
< uh_realm_count
; i
++ )
726 realm
= (struct auth_realm
*)
727 &uh_realms
[i
* sizeof(struct auth_realm
)];
729 rlen
= strlen(realm
->path
);
731 if( (plen
>= rlen
) &&
732 !strncasecmp(pi
->name
, realm
->path
, rlen
) &&
733 !strcmp(user
, realm
->user
)
742 /* found a realm matching the username */
745 /* is a crypt passwd */
746 if( realm
->pass
[0] == '$' )
747 pass
= crypt(pass
, realm
->pass
);
749 /* check user pass */
750 if( !strcmp(pass
, realm
->pass
) )
756 uh_http_sendf(cl
, NULL
,
757 "HTTP/%.1f 401 Authorization Required\r\n"
758 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
759 "Content-Type: text/plain\r\n"
760 "Content-Length: 23\r\n\r\n"
761 "Authorization Required\n",
762 req
->version
, cl
->server
->conf
->realm
772 static char uh_listeners
[UH_LIMIT_LISTENERS
* sizeof(struct listener
)] = { 0 };
773 static char uh_clients
[UH_LIMIT_CLIENTS
* sizeof(struct client
)] = { 0 };
775 static int uh_listener_count
= 0;
776 static int uh_client_count
= 0;
779 struct listener
* uh_listener_add(int sock
, struct config
*conf
)
781 struct listener
*new = NULL
;
784 if( uh_listener_count
< UH_LIMIT_LISTENERS
)
786 new = (struct listener
*)
787 &uh_listeners
[uh_listener_count
* sizeof(struct listener
)];
792 /* get local endpoint addr */
793 sl
= sizeof(struct sockaddr_in6
);
794 memset(&(new->addr
), 0, sl
);
795 getsockname(sock
, (struct sockaddr
*) &(new->addr
), &sl
);
803 struct listener
* uh_listener_lookup(int sock
)
805 struct listener
*cur
= NULL
;
808 for( i
= 0; i
< uh_listener_count
; i
++ )
810 cur
= (struct listener
*) &uh_listeners
[i
* sizeof(struct listener
)];
812 if( cur
->socket
== sock
)
820 struct client
* uh_client_add(int sock
, struct listener
*serv
)
822 struct client
*new = NULL
;
825 if( uh_client_count
< UH_LIMIT_CLIENTS
)
827 new = (struct client
*)
828 &uh_clients
[uh_client_count
* sizeof(struct client
)];
833 /* get remote endpoint addr */
834 sl
= sizeof(struct sockaddr_in6
);
835 memset(&(new->peeraddr
), 0, sl
);
836 getpeername(sock
, (struct sockaddr
*) &(new->peeraddr
), &sl
);
838 /* get local endpoint addr */
839 sl
= sizeof(struct sockaddr_in6
);
840 memset(&(new->servaddr
), 0, sl
);
841 getsockname(sock
, (struct sockaddr
*) &(new->servaddr
), &sl
);
849 struct client
* uh_client_lookup(int sock
)
851 struct client
*cur
= NULL
;
854 for( i
= 0; i
< uh_client_count
; i
++ )
856 cur
= (struct client
*) &uh_clients
[i
* sizeof(struct client
)];
858 if( cur
->socket
== sock
)
865 void uh_client_remove(int sock
)
867 struct client
*del
= uh_client_lookup(sock
);
871 memmove(del
, del
+ 1,
872 sizeof(uh_clients
) - (int)((char *)del
- uh_clients
) - sizeof(struct client
));
This page took 0.080866 seconds and 3 git commands to generate.