auth-bozo.c revision 1.1.1.4 1 1.1.1.4 mrg /* $eterna: auth-bozo.c,v 1.13 2009/04/18 07:38:56 mrg Exp $ */
2 1.1 tls
3 1.1 tls /*
4 1.1.1.3 mrg * Copyright (c) 1997-2009 Matthew R. Green
5 1.1 tls * All rights reserved.
6 1.1 tls *
7 1.1 tls * Redistribution and use in source and binary forms, with or without
8 1.1 tls * modification, are permitted provided that the following conditions
9 1.1 tls * are met:
10 1.1 tls * 1. Redistributions of source code must retain the above copyright
11 1.1 tls * notice, this list of conditions and the following disclaimer.
12 1.1 tls * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 tls * notice, this list of conditions and the following disclaimer and
14 1.1 tls * dedication in the documentation and/or other materials provided
15 1.1 tls * with the distribution.
16 1.1 tls *
17 1.1 tls * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 1.1 tls * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 1.1 tls * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 1.1 tls * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 1.1 tls * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22 1.1 tls * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 1.1 tls * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 1.1 tls * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25 1.1 tls * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 1.1 tls * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 1.1 tls * SUCH DAMAGE.
28 1.1 tls *
29 1.1 tls */
30 1.1 tls
31 1.1 tls /* this code implements "http basic authorisation" for bozohttpd */
32 1.1 tls
33 1.1 tls #ifdef DO_HTPASSWD
34 1.1 tls
35 1.1 tls #include <sys/param.h>
36 1.1 tls
37 1.1 tls #include <string.h>
38 1.1.1.4 mrg #include <stdlib.h>
39 1.1 tls #include <unistd.h>
40 1.1 tls
41 1.1 tls #include "bozohttpd.h"
42 1.1 tls
43 1.1 tls #ifndef AUTH_FILE
44 1.1 tls #define AUTH_FILE ".htpasswd"
45 1.1 tls #endif
46 1.1 tls
47 1.1 tls static ssize_t base64_decode(const unsigned char *, size_t,
48 1.1 tls unsigned char *, size_t);
49 1.1 tls
50 1.1 tls /*
51 1.1 tls * Check if HTTP authentication is required
52 1.1 tls */
53 1.1.1.3 mrg int
54 1.1 tls auth_check(http_req *request, const char *file)
55 1.1 tls {
56 1.1 tls struct stat sb;
57 1.1 tls char dir[MAXPATHLEN], authfile[MAXPATHLEN], *basename;
58 1.1 tls char user[BUFSIZ], *pass;
59 1.1 tls FILE *fp;
60 1.1 tls int len;
61 1.1 tls
62 1.1 tls /* get dir=dirname(file) */
63 1.1 tls snprintf(dir, sizeof(dir), "%s", file);
64 1.1 tls if ((basename = strrchr(dir, '/')) == NULL)
65 1.1 tls strcpy(dir, ".");
66 1.1 tls else {
67 1.1 tls *basename++ = '\0';
68 1.1 tls /* ensure basename(file) != AUTH_FILE */
69 1.1.1.3 mrg if (check_special_files(request, basename))
70 1.1.1.3 mrg return 1;
71 1.1 tls }
72 1.1.1.3 mrg request->hr_authrealm = bozostrdup(dir);
73 1.1 tls
74 1.1 tls snprintf(authfile, sizeof(authfile), "%s/%s", dir, AUTH_FILE);
75 1.1 tls if (stat(authfile, &sb) < 0) {
76 1.1 tls debug((DEBUG_NORMAL,
77 1.1 tls "auth_check realm `%s' dir `%s' authfile `%s' missing",
78 1.1 tls dir, file, authfile));
79 1.1.1.4 mrg return 0;
80 1.1 tls }
81 1.1 tls if ((fp = fopen(authfile, "r")) == NULL)
82 1.1.1.3 mrg return http_error(403, request, "no permission to open "
83 1.1.1.3 mrg "authfile");
84 1.1 tls debug((DEBUG_NORMAL,
85 1.1 tls "auth_check realm `%s' dir `%s' authfile `%s' open",
86 1.1 tls dir, file, authfile));
87 1.1 tls if (request->hr_authuser && request->hr_authpass) {
88 1.1 tls while (fgets(user, sizeof(user), fp) != NULL) {
89 1.1 tls len = strlen(user);
90 1.1 tls if (len > 0 && user[len-1] == '\n')
91 1.1 tls user[--len] = '\0';
92 1.1 tls if ((pass = strchr(user, ':')) == NULL)
93 1.1 tls continue;
94 1.1 tls *pass++ = '\0';
95 1.1 tls debug((DEBUG_NORMAL,
96 1.1 tls "auth_check authfile `%s':`%s' client `%s':`%s'",
97 1.1 tls user, pass, request->hr_authuser,
98 1.1 tls request->hr_authpass));
99 1.1 tls if (strcmp(request->hr_authuser, user) != 0)
100 1.1 tls continue;
101 1.1 tls if (strcmp(crypt(request->hr_authpass, pass), pass))
102 1.1 tls break;
103 1.1 tls fclose(fp);
104 1.1.1.4 mrg return 0;
105 1.1 tls }
106 1.1 tls }
107 1.1 tls fclose(fp);
108 1.1.1.3 mrg return http_error(401, request, "bad auth");
109 1.1.1.3 mrg }
110 1.1.1.3 mrg
111 1.1.1.3 mrg void
112 1.1.1.3 mrg auth_cleanup(http_req *request)
113 1.1.1.3 mrg {
114 1.1.1.3 mrg
115 1.1.1.3 mrg if (request == NULL)
116 1.1.1.3 mrg return;
117 1.1.1.3 mrg if (request->hr_authuser)
118 1.1.1.3 mrg free(request->hr_authuser);
119 1.1.1.3 mrg if (request->hr_authpass)
120 1.1.1.3 mrg free(request->hr_authpass);
121 1.1.1.3 mrg if (request->hr_authrealm)
122 1.1.1.3 mrg free(request->hr_authrealm);
123 1.1 tls }
124 1.1 tls
125 1.1 tls int
126 1.1 tls auth_check_headers(http_req *request, char *val, char *str, ssize_t len)
127 1.1 tls {
128 1.1 tls if (strcasecmp(val, "authorization") == 0 &&
129 1.1 tls strncasecmp(str, "Basic ", 6) == 0) {
130 1.1 tls char authbuf[BUFSIZ];
131 1.1.1.2 mrg char *pass = NULL;
132 1.1 tls ssize_t alen;
133 1.1 tls
134 1.1.1.2 mrg alen = base64_decode((unsigned char *)str + 6, len - 6,
135 1.1.1.2 mrg (unsigned char *)authbuf, sizeof(authbuf) - 1);
136 1.1 tls if (alen != -1)
137 1.1 tls authbuf[alen] = '\0';
138 1.1 tls if (alen == -1 ||
139 1.1 tls (pass = strchr(authbuf, ':')) == NULL)
140 1.1.1.3 mrg return http_error(400, request,
141 1.1 tls "bad authorization field");
142 1.1 tls *pass++ = '\0';
143 1.1 tls request->hr_authuser = bozostrdup(authbuf);
144 1.1 tls request->hr_authpass = bozostrdup(pass);
145 1.1 tls debug((DEBUG_FAT,
146 1.1 tls "decoded authorization `%s' as `%s':`%s'",
147 1.1 tls str, request->hr_authuser, request->hr_authpass));
148 1.1 tls /* don't store in request->headers */
149 1.1 tls return 1;
150 1.1 tls }
151 1.1 tls return 0;
152 1.1 tls }
153 1.1 tls
154 1.1.1.3 mrg int
155 1.1 tls auth_check_special_files(http_req *request, const char *name)
156 1.1 tls {
157 1.1 tls if (strcmp(name, AUTH_FILE) == 0)
158 1.1.1.3 mrg return http_error(403, request, "no permission to open authfile");
159 1.1.1.3 mrg return 0;
160 1.1 tls }
161 1.1 tls
162 1.1 tls void
163 1.1 tls auth_check_401(http_req *request, int code)
164 1.1 tls {
165 1.1 tls if (code == 401)
166 1.1 tls bozoprintf("WWW-Authenticate: Basic realm=\"%s\"\r\n",
167 1.1 tls request && request->hr_authrealm ? request->hr_authrealm :
168 1.1 tls "default realm");
169 1.1 tls }
170 1.1 tls
171 1.1 tls #ifndef NO_CGIBIN_SUPPORT
172 1.1 tls void
173 1.1 tls auth_cgi_setenv(http_req *request, char ***curenvpp)
174 1.1 tls {
175 1.1 tls if (request->hr_authuser && *request->hr_authuser) {
176 1.1 tls spsetenv("AUTH_TYPE", "Basic", (*curenvpp)++);
177 1.1.1.3 mrg spsetenv("REMOTE_USER", request->hr_authuser, (*curenvpp)++);
178 1.1 tls }
179 1.1 tls }
180 1.1 tls
181 1.1 tls int
182 1.1 tls auth_cgi_count(http_req *request)
183 1.1 tls {
184 1.1 tls return (request->hr_authuser && *request->hr_authuser) ? 2 : 0;
185 1.1 tls }
186 1.1 tls #endif /* NO_CGIBIN_SUPPORT */
187 1.1 tls
188 1.1 tls /*
189 1.1 tls * Decode len bytes starting at in using base64 encoding into out.
190 1.1 tls * Result is *not* NUL terminated.
191 1.1 tls * Written by Luke Mewburn <lukem (at) NetBSD.org>
192 1.1 tls */
193 1.1 tls const unsigned char decodetable[] = {
194 1.1 tls 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
195 1.1 tls 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
196 1.1 tls 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
197 1.1 tls 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 0, 255, 255,
198 1.1 tls 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
199 1.1 tls 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
200 1.1 tls 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
201 1.1 tls 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255,
202 1.1 tls };
203 1.1 tls
204 1.1 tls static ssize_t
205 1.1 tls base64_decode(const unsigned char *in, size_t ilen, unsigned char *out,
206 1.1 tls size_t olen)
207 1.1 tls {
208 1.1 tls unsigned char *cp;
209 1.1 tls size_t i;
210 1.1 tls
211 1.1 tls cp = out;
212 1.1 tls for (i = 0; i < ilen; i += 4) {
213 1.1 tls if (cp + 3 > out + olen)
214 1.1 tls return (-1);
215 1.1 tls #define IN_CHECK(x) \
216 1.1 tls if ((x) > sizeof(decodetable) || decodetable[(x)] == 255) \
217 1.1 tls return(-1)
218 1.1 tls
219 1.1 tls IN_CHECK(in[i + 0]);
220 1.1 tls *(cp++) = decodetable[in[i + 0]] << 2
221 1.1 tls | decodetable[in[i + 1]] >> 4;
222 1.1 tls IN_CHECK(in[i + 1]);
223 1.1 tls *(cp++) = decodetable[in[i + 1]] << 4
224 1.1 tls | decodetable[in[i + 2]] >> 2;
225 1.1 tls IN_CHECK(in[i + 2]);
226 1.1 tls *(cp++) = decodetable[in[i + 2]] << 6
227 1.1 tls | decodetable[in[i + 3]];
228 1.1 tls #undef IN_CHECK
229 1.1 tls }
230 1.1 tls while (in[i - 1] == '=')
231 1.1 tls cp--,i--;
232 1.1 tls return (cp - out);
233 1.1 tls }
234 1.1 tls #endif /* DO_HTPASSWD */
235