aboutsummaryrefslogtreecommitdiff
path: root/lib/basicauth.c
blob: 4d313e6ae7c26454956a7775ffa0c77e54c729f6 (plain)
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
#include <config.h>
#include <errno.h>
#include <pthread.h>
#include <crypt.h>
#include <string.h>
#include "sha1.h"
#include "md5.h"
#include "basicauth.h"

static int b64val[128] = {
    	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
	-1, 0, 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, -1, -1, -1, -1, -1,
	-1, 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, -1, -1, -1, -1, -1
};

static int
base64_decode(const unsigned char *input, size_t input_len,
              unsigned char *output, size_t output_len)
{
	unsigned char *out = output;
#define AC(c) do { if (output_len-- == 0) return -1; *out++ = (c); } while (0)
	
    	if (!out)
        	return -1;

    	do {
        	if (input[0] > 127 || b64val[input[0]] == -1 || 
                    input[1] > 127 || b64val[input[1]] == -1 || 
                    input[2] > 127 || 
                    ((input[2] != '=') && (b64val[input[2]] == -1)) || 
                    input[3] > 127 || 
                    ((input[3] != '=') && (b64val[input[3]] == -1))) {
            		errno = EINVAL;
            		return -1;
        	}
        	AC((b64val[input[0]] << 2) | (b64val[input[1]] >> 4));
        	if (input[2] != '=') {
            		AC(((b64val[input[1]] << 4) & 0xf0) | 
                           (b64val[input[2]] >> 2));
            		if (input[3] != '=')
                		AC(((b64val[input[2]] << 6) & 0xc0) | 
                                    b64val[input[3]]);
        	}
        	input += 4;
        	input_len -= 4;
    	} while (input_len > 0);
    	return out - output;
}

static pthread_mutex_t crypt_mutex = PTHREAD_MUTEX_INITIALIZER;

static int
crypt_match(const char *pass, const char *hash)
{
	int res = 1;
	char *cp;
	
	pthread_mutex_lock(&crypt_mutex);
	cp = crypt(pass, hash);
	if (cp)
		res = strcmp(cp, hash);
	pthread_mutex_unlock(&crypt_mutex);
	return res;
}

static int
plain_match(const char *pass, const char *hash)
{
	return strcmp(pass, hash);
}

static int
apr_match(const char *pass, const char *hash)
{
	char buf[120];
	char *cp = apr_md5_encode(pass, hash, buf, sizeof(buf));
	return cp ? strcmp(cp, hash) : 1;
}

#define SHA1_DIGEST_SIZE 20

static int
sha1_match(const char *pass, const char *hash)
{
	char hashbuf[SHA1_DIGEST_SIZE], resbuf[SHA1_DIGEST_SIZE];
	int n;
	
	hash += 5; /* Skip past {SHA} */
	n = base64_decode((const unsigned char *)hash, strlen(hash),
			  (unsigned char *)hashbuf, sizeof(hashbuf));
	if (n < 0) {
		/* FIXME: error("cannot decode %s", hash); */
		return 1;
	}
	if (n != SHA1_DIGEST_SIZE) {
		/* FIXME: error("bad hash length: %s %d", hash, n); */
		return 1;
	}
	sha1_buffer(pass, strlen(pass), resbuf);
	
	return memcmp(resbuf, hashbuf, SHA1_DIGEST_SIZE);
}

/* Matcher table */
struct matcher {
	char *cm_pfx;
	size_t cm_len;
	int (*cm_match)(const char *, const char *);
};

static struct matcher match_tab[] = {
#define S(s) #s, sizeof(#s)-1
	{ S($apr1$), apr_match },
	{ S({SHA}), sha1_match },
	{ "", 0, crypt_match },
	{ "", 0, plain_match },
	{ NULL }
};

static int
match(const char *pass, const char *hash)
{
	struct matcher *p;
	size_t plen = strlen(hash);

	for (p = match_tab; p->cm_match; p++) {
		if (p->cm_len < plen && 
		    memcmp(p->cm_pfx, hash, p->cm_len) == 0 &&
		    p->cm_match(pass, hash) == 0)
			return 0;
	}
	return 1;
}

#define BASICPREF "Basic "
#define BASICLEN (sizeof(BASICPREF)-1)

BASICAUTH_RESULT
basicauth(char const *file, char const *input)
{
	char buf[1024];	
	char lbuf[1024];	
	char *pass;
	int n;
	FILE *fp;
	BASICAUTH_RESULT rc;

	if (!input || strncmp(input, BASICPREF, BASICLEN))
		return BASICAUTH_BAD_INPUT;
	input += BASICLEN;
	n = base64_decode((const unsigned char *)input, strlen(input),
			  (unsigned char *)buf, sizeof(buf));
	if (n < 0) {
		return BASICAUTH_BAD_INPUT;
	} else if (n == sizeof(buf)) {
		return BASICAUTH_BAD_INPUT;
	}
	buf[n] = 0;

	pass = strchr(buf, ':');
	if (!pass)
		return BASICAUTH_BAD_INPUT;
	*pass++ = 0;

	fp = fopen(file, "r");
	if (!fp)
		return BASICAUTH_CANT_OPEN;

	rc = BASICAUTH_DENY;
	while (fgets(lbuf, sizeof(lbuf), fp)) {
		char *p, *q;
		for (p = lbuf; *p && (*p == ' ' || *p == '\t'); p++);
		if (*p == '#')
			continue;
		q = p + strlen(p);
		if (q == p)
			continue;
		if (q[-1] == '\n')
			*--q = 0;
		if (!*p)
			continue;
		q = strchr(p, ':');
		if (!q) 
			continue;
		*q++ = 0;
		if (strcmp(p, buf))
			continue;
		rc = match(pass, q) ? BASICAUTH_DENY : BASICAUTH_ALLOW;
		break;
	}
	fclose(fp);
	return rc;
}
	

Return to:

Send suggestions and report system problems to the System administrator.