summaryrefslogtreecommitdiff
path: root/sys/src/cmd/auth/factotum/apop.c
blob: 7709678c6f876147c2c7b244b246130016e2fe21 (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
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
/*
 * APOP, CRAM - MD5 challenge/response authentication
 *
 * The client does not authenticate the server, hence no CAI
 *
 * Client protocol:
 *	write challenge: randomstring@domain
 *	read response: 2*MD5dlen hex digits
 *
 * Server protocol:
 *	read challenge: randomstring@domain
 *	write user: user
 *	write response: 2*MD5dlen hex digits
 */

#include "dat.h"

struct State
{
	int asfd;
	int astype;
	Key *key;
	Ticket	t;
	Ticketreq	tr;
	char chal[128];
	char	resp[64];
	char *user;
};

enum
{
	CNeedChal,
	CHaveResp,

	SHaveChal,
	SNeedUser,
	SNeedResp,

	Maxphase,
};

static char *phasenames[Maxphase] = {
[CNeedChal]	"CNeedChal",
[CHaveResp]	"CHaveResp",

[SHaveChal]	"SHaveChal",
[SNeedUser]	"SNeedUser",
[SNeedResp]	"SNeedResp",
};

static int dochal(State*);
static int doreply(State*, char*, char*);

static int
apopinit(Proto *p, Fsstate *fss)
{
	int iscli, ret;
	State *s;

	if((iscli = isclient(_strfindattr(fss->attr, "role"))) < 0)
		return failure(fss, nil);

	s = emalloc(sizeof *s);
	fss->phasename = phasenames;
	fss->maxphase = Maxphase;
	s->asfd = -1;
	if(p == &apop)
		s->astype = AuthApop;
	else if(p == &cram)
		s->astype = AuthCram;
	else
		abort();

	if(iscli)
		fss->phase = CNeedChal;
	else{
		if((ret = findp9authkey(&s->key, fss)) != RpcOk){
			free(s);
			return ret;
		}
		if(dochal(s) < 0){
			free(s);
			return failure(fss, nil);
		}
		fss->phase = SHaveChal;
	}
	fss->ps = s;
	return RpcOk;
}

static int
apopwrite(Fsstate *fss, void *va, uint n)
{
	char *a, *v;
	int i, ret;
	uchar digest[MD5dlen];
	DigestState *ds;
	Key *k;
	State *s;
	Keyinfo ki;

	s = fss->ps;
	a = va;
	switch(fss->phase){
	default:
		return phaseerror(fss, "write");

	case CNeedChal:
		ret = findkey(&k, mkkeyinfo(&ki, fss, nil), "%s", fss->proto->keyprompt);
		if(ret != RpcOk)
			return ret;
		v = _strfindattr(k->privattr, "!password");
		if(v == nil)
			return failure(fss, "key has no password");
		setattrs(fss->attr, k->attr);
		switch(s->astype){
		default:
			abort();
		case AuthCram:
			hmac_md5((uchar*)a, n, (uchar*)v, strlen(v),
				digest, nil);
			sprint(s->resp, "%.*H", MD5dlen, digest);
			break;
		case AuthApop:
			ds = md5((uchar*)a, n, nil, nil);
			md5((uchar*)v, strlen(v), digest, ds);
			for(i=0; i<MD5dlen; i++)
				sprint(&s->resp[2*i], "%2.2x", digest[i]);
			break;
		}
		closekey(k);
		fss->phase = CHaveResp;
		return RpcOk;
	
	case SNeedUser:
		if((v = _strfindattr(fss->attr, "user")) && strcmp(v, a) != 0)
			return failure(fss, "bad user");
		fss->attr = setattr(fss->attr, "user=%q", a);
		s->user = estrdup(a);
		fss->phase = SNeedResp;
		return RpcOk;

	case SNeedResp:
		if(n != 2*MD5dlen)
			return failure(fss, "response not MD5 digest");
		if(doreply(s, s->user, a) < 0){
			fss->phase = SNeedUser;
			return failure(fss, nil);
		}
		fss->haveai = 1;
		fss->ai.cuid = s->t.cuid;
		fss->ai.suid = s->t.suid;
		fss->ai.nsecret = 0;
		fss->ai.secret = nil;
		fss->phase = Established;
		return RpcOk;
	}
}

static int
apopread(Fsstate *fss, void *va, uint *n)
{
	State *s;

	s = fss->ps;
	switch(fss->phase){
	default:
		return phaseerror(fss, "read");

	case CHaveResp:
		if(*n > strlen(s->resp))
			*n = strlen(s->resp);
		memmove(va, s->resp, *n);
		fss->phase = Established;
		fss->haveai = 0;
		return RpcOk;

	case SHaveChal:
		if(*n > strlen(s->chal))
			*n = strlen(s->chal);
		memmove(va, s->chal, *n);
		fss->phase = SNeedUser;
		return RpcOk;
	}
}

static void
apopclose(Fsstate *fss)
{
	State *s;

	s = fss->ps;
	if(s->asfd >= 0){
		close(s->asfd);
		s->asfd = -1;
	}
	if(s->key != nil){
		closekey(s->key);
		s->key = nil;
	}
	if(s->user != nil){
		free(s->user);
		s->user = nil;
	}
	free(s);
}

static int
dochal(State *s)
{
	char *dom, *user, trbuf[TICKREQLEN];

	s->asfd = -1;

	/* send request to authentication server and get challenge */
	/* send request to authentication server and get challenge */
	if((dom = _strfindattr(s->key->attr, "dom")) == nil
	|| (user = _strfindattr(s->key->attr, "user")) == nil){
		werrstr("apop/dochal cannot happen");
		goto err;
	}

	s->asfd = _authdial(nil, dom);

	/* could generate our own challenge on error here */
	if(s->asfd < 0)
		goto err;

	memset(&s->tr, 0, sizeof(s->tr));
	s->tr.type = s->astype;
	safecpy(s->tr.authdom, dom, sizeof s->tr.authdom);
	safecpy(s->tr.hostid, user, sizeof(s->tr.hostid));
	convTR2M(&s->tr, trbuf);

	if(write(s->asfd, trbuf, TICKREQLEN) != TICKREQLEN)
		goto err;
	if(_asrdresp(s->asfd, s->chal, sizeof s->chal) <= 5)
		goto err;
	return 0;

err:
	if(s->asfd >= 0)
		close(s->asfd);
	s->asfd = -1;
	return -1;
}

static int
doreply(State *s, char *user, char *response)
{
	char ticket[TICKETLEN+AUTHENTLEN];
	char trbuf[TICKREQLEN];
	int n;
	Authenticator a;

	memrandom(s->tr.chal, CHALLEN);
	safecpy(s->tr.uid, user, sizeof(s->tr.uid));
	convTR2M(&s->tr, trbuf);
	if((n=write(s->asfd, trbuf, TICKREQLEN)) != TICKREQLEN){
		if(n >= 0)
			werrstr("short write to auth server");
		goto err;
	}
	/* send response to auth server */
	if(strlen(response) != MD5dlen*2){
		werrstr("response not MD5 digest");
		goto err;
	}
	if((n=write(s->asfd, response, MD5dlen*2)) != MD5dlen*2){
		if(n >= 0)
			werrstr("short write to auth server");
		goto err;
	}
	if(_asrdresp(s->asfd, ticket, TICKETLEN+AUTHENTLEN) < 0){
		/* leave connection open so we can try again */
		return -1;
	}
	close(s->asfd);
	s->asfd = -1;

	convM2T(ticket, &s->t, (char*)s->key->priv);
	if(s->t.num != AuthTs
	|| memcmp(s->t.chal, s->tr.chal, sizeof(s->t.chal)) != 0){
		if(s->key->successes == 0)
			disablekey(s->key);
		werrstr(Easproto);
		goto err;
	}
	s->key->successes++;
	convM2A(ticket+TICKETLEN, &a, s->t.key);
	if(a.num != AuthAc
	|| memcmp(a.chal, s->tr.chal, sizeof(a.chal)) != 0
	|| a.id != 0){
		werrstr(Easproto);
		goto err;
	}

	return 0;
err:
	if(s->asfd >= 0)
		close(s->asfd);
	s->asfd = -1;
	return -1;
}

Proto apop = {
.name=	"apop",
.init=		apopinit,
.write=	apopwrite,
.read=	apopread,
.close=	apopclose,
.addkey=	replacekey,
.keyprompt=	"!password?"
};

Proto cram = {
.name=	"cram",
.init=		apopinit,
.write=	apopwrite,
.read=	apopread,
.close=	apopclose,
.addkey=	replacekey,
.keyprompt=	"!password?"
};