use a better error code for unsupported expect headers
[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 #include "uhttpd.h"
21
22 static LIST_HEAD(auth_realms);
23
24 void uh_auth_add(const char *path, const char *user, const char *pass)
25 {
26         struct auth_realm *new = NULL;
27         struct passwd *pwd;
28         const char *new_pass = NULL;
29         char *dest_path, *dest_user, *dest_pass;
30
31 #ifdef HAVE_SHADOW
32         struct spwd *spwd;
33 #endif
34
35         /* given password refers to a passwd entry */
36         if ((strlen(pass) > 3) && !strncmp(pass, "$p$", 3)) {
37 #ifdef HAVE_SHADOW
38                 /* try to resolve shadow entry */
39                 spwd = getspnam(&pass[3]);
40                 if (spwd)
41                         new_pass = spwd->sp_pwdp;
42 #endif
43                 if (!new_pass) {
44                         pwd = getpwnam(&pass[3]);
45                         if (pwd && pwd->pw_passwd && pwd->pw_passwd[0] &&
46                             pwd->pw_passwd[0] != '!')
47                                 new_pass = pwd->pw_passwd;
48                 }
49         } else {
50                 new_pass = pass;
51         }
52
53         if (!new_pass || !new_pass[0])
54                 return;
55
56         new = calloc_a(sizeof(*new),
57                 &dest_path, strlen(path) + 1,
58                 &dest_user, strlen(user) + 1,
59                 &dest_pass, strlen(new_pass) + 1);
60
61         if (!new)
62                 return;
63
64         new->path = strcpy(dest_path, path);
65         new->user = strcpy(dest_user, user);
66         new->pass = strcpy(dest_pass, new_pass);
67         list_add(&new->list, &auth_realms);
68 }
69
70 bool uh_auth_check(struct client *cl, struct path_info *pi)
71 {
72         struct http_request *req = &cl->request;
73         struct auth_realm *realm;
74         bool user_match = false;
75         char *user = NULL;
76         char *pass = NULL;
77         int plen;
78
79         if (pi->auth && !strncasecmp(pi->auth, "Basic ", 6)) {
80                 const char *auth = pi->auth + 6;
81
82                 uh_b64decode(uh_buf, sizeof(uh_buf), auth, strlen(auth));
83                 pass = strchr(uh_buf, ':');
84                 if (pass) {
85                         user = uh_buf;
86                         *pass++ = 0;
87                 }
88         }
89
90         req->realm = NULL;
91         plen = strlen(pi->name);
92         list_for_each_entry(realm, &auth_realms, list) {
93                 int rlen = strlen(realm->path);
94
95                 if (plen < rlen)
96                         continue;
97
98                 if (strncasecmp(pi->name, realm->path, rlen) != 0)
99                         continue;
100
101                 req->realm = realm;
102                 if (!user)
103                         break;
104
105                 if (strcmp(user, realm->user) != 0)
106                         continue;
107
108                 user_match = true;
109                 break;
110         }
111
112         if (!req->realm)
113                 return true;
114
115         if (user_match && !strcmp(crypt(pass, realm->pass), realm->pass))
116                 return true;
117
118         uh_http_header(cl, 401, "Authorization Required");
119         ustream_printf(cl->us,
120                                   "WWW-Authenticate: Basic realm=\"%s\"\r\n"
121                                   "Content-Type: text/plain\r\n\r\n",
122                                   conf.realm);
123         uh_chunk_printf(cl, "Authorization Required\n");
124         uh_request_done(cl);
125
126         return false;
127 }