1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/*
* Copyright (c) 2004, Nate Nielsen
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
* * Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or
* other materials provided with the distribution.
* * The names of contributors to this software may not be
* used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
*
* CONTRIBUTORS
* Nate Nielsen <nielsen@memberwebs.com>
*
*/
/* On some linux this is required to get crypt to show itself */
#define _XOPEN_SOURCE
#include "usuals.h"
#include "httpauthd.h"
#include "defaults.h"
#include "hash.h"
#include "bd.h"
#include "md5.h"
#include <stdio.h>
#include <fcntl.h>
#define SIMPLE_MAXLINE 256
/* -------------------------------------------------------------------------------
* Structures
*/
typedef struct simple_context
{
/* Base Handler ------------------------------------------------------ */
bd_context_t bd;
/* Read Only --------------------------------------------------------- */
const char* filename; /* The file name with the user names */
}
simple_context_t;
/* Forward declarations for callbacks */
static int validate_digest(ha_request_t* rq, const char* user, digest_context_t* dg);
static int validate_basic(ha_request_t* rq, const char* user, const char* password);
/* The defaults for the context */
static const simple_context_t simple_defaults =
{
BD_CALLBACKS(validate_digest, validate_basic, NULL),
NULL /* filename */
};
/* -------------------------------------------------------------------------------
* Internal Functions
*/
static int validate_digest(ha_request_t* rq, const char* user, digest_context_t* dg)
{
simple_context_t* ctx = (simple_context_t*)rq->context->ctx_data;
FILE* f;
char* t;
char* t2;
size_t len;
char line[SIMPLE_MAXLINE];
int ret = HA_FALSE;
int foundinvalid = 0;
int foundgood = 0;
ASSERT(ctx && rq && user && user[0]);
ha_messagex(rq, LOG_DEBUG, "searching password file for user's ha1: %s", user);
f = fopen(ctx->filename, "r");
if(!f)
{
ha_message(rq, LOG_ERR, "can't open file for digest auth: %s", ctx->filename);
return HA_FAILED;
}
/*
* Note: There should be no returns or jumps between
* this point and the closing of the file below.
*/
/* Now look through the whole file */
while(!feof(f))
{
fgets(line, SIMPLE_MAXLINE, f);
if(ferror(f))
{
ha_message(rq, LOG_ERR, "error reading basic password file");
ret = HA_FAILED;
break;
}
t = strchr(line, ':');
if(!t)
continue;
/* Split the line */
*t = 0;
t++;
/* Check the user */
if(strcmp(line, user) != 0)
continue;
t2 = strchr(t, ':');
if(!t2)
{
/* An invalid line without a realm */
foundinvalid = 1;
continue;
}
/* Split the line */
*t2 = 0;
t2++;
/* Check the realm */
if(strcmp(t, rq->context->realm) != 0)
continue;
/* Now try and decode the ha1 */
len = MD5_LEN;
t = ha_bufdechex(rq->buf, t2, &len);
if(!t || len != MD5_LEN)
{
/* An invalid ha1 */
foundinvalid = 1;
continue;
}
ha_messagex(rq, LOG_DEBUG, "found ha1 for user: %s", user);
memcpy(dg->ha1, t, MD5_LEN);
foundgood = 1;
/* Try to do the validation */
ret = digest_complete_check(dg, rq->buf);
/* If invalid then continue search */
if(ret == HA_FALSE)
continue;
/* Success or an error ends here */
break;
}
if(foundinvalid && !foundgood)
ha_messagex(rq, LOG_WARNING, "user '%s' found in file, but password not in digest format", user);
fclose(f);
if(CHECK_RBUF(rq))
return HA_CRITERROR;
return ret;
}
static int validate_basic(ha_request_t* rq, const char* user, const char* password)
{
simple_context_t* ctx = (simple_context_t*)rq->context->ctx_data;
FILE* f;
char line[SIMPLE_MAXLINE];
unsigned char ha1[MD5_LEN];
char* t;
char* t2;
size_t len;
int ret = HA_FALSE;
ASSERT(ctx && rq);
ASSERT(user && user[0] && password);
ha_messagex(rq, LOG_DEBUG, "validating user against password file: %s", user);
f = fopen(ctx->filename, "r");
if(!f)
{
ha_message(rq, LOG_ERR, "can't open file for basic auth: %s", ctx->filename);
return HA_FAILED;
}
digest_makeha1(ha1, user, rq->context->realm, password);
/*
* Note: There should be no returns or jumps between
* this point and the closing of the file below.
*/
/* Now look through the whole file */
while(!feof(f))
{
fgets(line, SIMPLE_MAXLINE, f);
if(ferror(f))
{
ha_message(rq, LOG_ERR, "error reading basic password file");
ret = HA_FAILED;
break;
}
/* Take white space off end of line */
trim_end(line);
t = strchr(line, ':');
if(!t)
continue;
/* Split the line */
*t = 0;
t++;
/* Check the user */
if(strcmp(line, user) != 0)
continue;
/* Not sure if crypt is thread safe so we lock */
ha_lock(NULL);
/* Check the password */
t2 = (char*)crypt(password, t);
ha_unlock(NULL);
if(strcmp(t2, t) == 0)
{
ha_messagex(rq, LOG_DEBUG, "found valid crypt password for user: %s", user);
ret = HA_OK;
break;
}
/* Otherwise it might be a digest type ha1. */
t2 = strchr(t, ':');
if(!t2)
continue;
/* Split the line */
*t2 = 0;
t2++;
/* Check the realm */
if(strcmp(t, rq->context->realm) != 0)
continue;
/* Now try antd decode the ha1 */
len = MD5_LEN;
t = ha_bufdechex(rq->buf, t2, &len);
if(!t || len != MD5_LEN)
continue;
if(memcmp(ha1, t, MD5_LEN) == 0)
{
ha_messagex(rq, LOG_DEBUG, "found valid ha1 for user: %s", user);
ret = HA_OK;
break;
}
if(CHECK_RBUF(rq))
break;
}
fclose(f);
if(CHECK_RBUF(rq))
return HA_CRITERROR;
return ret;
}
/* -------------------------------------------------------------------------------
* Handler Functions
*/
int simple_config(ha_context_t* context, const char* name, const char* value)
{
simple_context_t* ctx = (simple_context_t*)(context->ctx_data);
ASSERT(name && name[0] && value && value[0]);
if(strcmp(name, "passwordfile") == 0)
{
ctx->filename = value;
return HA_OK;
}
return HA_FALSE;
}
int simple_init(ha_context_t* context)
{
int r;
if((r = bd_init(context)) != HA_OK)
return r;
if(context)
{
simple_context_t* ctx = (simple_context_t*)(context->ctx_data);
int fd;
ASSERT(ctx);
/* Check to make sure the file exists */
if(!ctx->filename)
{
ha_messagex(NULL, LOG_ERR, "configuration incomplete. "
"Must have a PasswordFile configured.");
return HA_FAILED;
}
fd = open(ctx->filename, O_RDONLY);
if(fd == -1)
{
ha_message(NULL, LOG_ERR, "can't open file for authentication: %s", ctx->filename);
return HA_FAILED;
}
close(fd);
ha_messagex(NULL, LOG_INFO, "initialized simple handler");
}
return HA_OK;
}
/* -------------------------------------------------------------------------------
* Handler Definition
*/
ha_handler_t simple_handler =
{
"SIMPLE", /* The type */
simple_init, /* Initialization function */
bd_destroy, /* Uninitialization routine */
simple_config, /* Config routine */
bd_process, /* Processing routine */
&simple_defaults, /* A default context */
sizeof(simple_context_t) /* Size of the context */
};
|