aboutsummaryrefslogtreecommitdiff
path: root/src/libauth/auth_challenge.c
blob: b12d0fe5ba94c2585d1feaf084390dc64e02dc39 (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
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <authsrv.h>
#include "authlocal.h"

Chalstate*
auth_challenge(char *fmt, ...)
{
	char *p;
	va_list arg;
	Chalstate *c;

	quotefmtinstall();	/* just in case */
	va_start(arg, fmt);
	p = vsmprint(fmt, arg);
	va_end(arg);
	if(p == nil)
		return nil;

	c = mallocz(sizeof(*c), 1);
	if(c == nil){
		free(p);
		return nil;
	}

	if((c->rpc=auth_allocrpc()) == nil
	|| auth_rpc(c->rpc, "start", p, strlen(p)) != ARok
	|| auth_rpc(c->rpc, "read", nil, 0) != ARok){
	Error:
		auth_freechal(c);
		free(p);
		return nil;
	}

	if(c->rpc->narg > sizeof(c->chal)-1){
		werrstr("buffer too small for challenge");
		goto Error;
	}
	memmove(c->chal, c->rpc->arg, c->rpc->narg);
	c->nchal = c->rpc->narg;
	free(p);
	return c;
}

AuthInfo*
auth_response(Chalstate *c)
{
	int ret;
	AuthInfo *ai;

	ai = nil;
	if(c->rpc == nil){
		werrstr("auth_response: connection not open");
		return nil;
	}
	if(c->resp == nil){
		werrstr("auth_response: nil response");
		return nil;
	}
	if(c->nresp == 0){
		werrstr("auth_response: unspecified response length");
		return nil;
	}

	if(c->user){
		if(auth_rpc(c->rpc, "write", c->user, strlen(c->user)) != ARok){
			/*
			 * if this fails we're out of phase with factotum.
			 * give up.
			 */
			goto Out;
		}
	}

	if(auth_rpc(c->rpc, "write", c->resp, c->nresp) != ARok){
		/*
		 * don't close the connection -- maybe we'll try again.
		 */
		return nil;
	}

	switch(ret = auth_rpc(c->rpc, "read", nil, 0)){
	case ARok:
	default:
		werrstr("factotum protocol botch %d %s", ret, c->rpc->ibuf);
		break;
	case ARdone:
		ai = auth_getinfo(c->rpc);
		break;
	}

Out:
	auth_freerpc(c->rpc);
	c->rpc = nil;
	return ai;
}

void
auth_freechal(Chalstate *c)
{
	if(c == nil)
		return;
	if(c->rpc != nil)
		auth_freerpc(c->rpc);
	memset(c, 0xBB, sizeof(*c));
	free(c);
}