add check for shadow support, turn -Os back on
[project/uhttpd.git] / auth.c
1 /*
2 * uhttpd - Tiny single-threaded httpd
3 *
4 * Copyright (C) 2010-2012 Jo-Philipp Wich <xm@subsignal.org>
5 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #define _GNU_SOURCE
21 #define _XOPEN_SOURCE 700
22 #include <strings.h>
23 #ifdef HAVE_SHADOW
24 #include <shadow.h>
25 #endif
26 #include "uhttpd.h"
27
28 static LIST_HEAD(auth_realms);
29
30 void uh_auth_add(const char *path, const char *user, const char *pass)
31 {
32 struct auth_realm *new = NULL;
33 struct passwd *pwd;
34 const char *new_pass = NULL;
35 char *dest_path, *dest_user, *dest_pass;
36
37 #ifdef HAVE_SHADOW
38 struct spwd *spwd;
39 #endif
40
41 /* given password refers to a passwd entry */
42 if ((strlen(pass) > 3) && !strncmp(pass, "$p$", 3)) {
43 #ifdef HAVE_SHADOW
44 /* try to resolve shadow entry */
45 spwd = getspnam(&pass[3]);
46 if (spwd)
47 new_pass = spwd->sp_pwdp;
48 #endif
49 if (!new_pass) {
50 pwd = getpwnam(&pass[3]);
51 if (pwd && pwd->pw_passwd && pwd->pw_passwd[0] &&
52 pwd->pw_passwd[0] != '!')
53 new_pass = pwd->pw_passwd;
54 }
55 } else {
56 new_pass = pass;
57 }
58
59 if (!new_pass || !new_pass[0])
60 return;
61
62 new = calloc_a(sizeof(*new),
63 &dest_path, strlen(path) + 1,
64 &dest_user, strlen(user) + 1,
65 &dest_pass, strlen(new_pass) + 1);
66
67 if (!new)
68 return;
69
70 new->path = strcpy(dest_path, path);
71 new->user = strcpy(dest_user, user);
72 new->pass = strcpy(dest_pass, new_pass);
73 list_add(&new->list, &auth_realms);
74 }
75
76 bool uh_auth_check(struct client *cl, struct path_info *pi)
77 {
78 struct http_request *req = &cl->request;
79 struct auth_realm *realm;
80 bool user_match = false;
81 char *user = NULL;
82 char *pass = NULL;
83 int plen;
84
85 if (pi->auth && !strncasecmp(pi->auth, "Basic ", 6)) {
86 const char *auth = pi->auth + 6;
87
88 uh_b64decode(uh_buf, sizeof(uh_buf), auth, strlen(auth));
89 pass = strchr(uh_buf, ':');
90 if (pass) {
91 user = uh_buf;
92 *pass++ = 0;
93 }
94 }
95
96 req->realm = NULL;
97 plen = strlen(pi->name);
98 list_for_each_entry(realm, &auth_realms, list) {
99 int rlen = strlen(realm->path);
100
101 if (plen < rlen)
102 continue;
103
104 if (strncasecmp(pi->name, realm->path, rlen) != 0)
105 continue;
106
107 req->realm = realm;
108 if (!user)
109 break;
110
111 if (strcmp(user, realm->user) != 0)
112 continue;
113
114 user_match = true;
115 break;
116 }
117
118 if (!req->realm)
119 return true;
120
121 if (user_match && !strcmp(crypt(pass, realm->pass), realm->pass))
122 return true;
123
124 uh_http_header(cl, 401, "Authorization Required");
125 ustream_printf(cl->us,
126 "WWW-Authenticate: Basic realm=\"%s\"\r\n"
127 "Content-Type: text/plain\r\n\r\n",
128 conf.realm);
129 uh_chunk_printf(cl, "Authorization Required\n");
130 uh_request_done(cl);
131
132 return false;
133 }