aboutsummaryrefslogtreecommitdiff
path: root/src/libauth/auth_rpc.c
blob: af6f0a639ba88a923ec6260327e803d2edcdb9a1 (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
#include <u.h>
#include <libc.h>
#include <auth.h>
#include <9pclient.h>
#include "authlocal.h"

static struct {
	char *verb;
	int val;
} tab[] = {
	"ok",			ARok,
	"done",		ARdone,
	"error",		ARerror,
	"needkey",	ARneedkey,
	"badkey",		ARbadkey,
	"phase",		ARphase,
	"toosmall",	ARtoosmall,
	"error",		ARerror,
};

static long
rpcread(AuthRpc *rpc, void *buf, int buflen)
{
	if (rpc->afd >= 0)
		return read(rpc->afd, buf, buflen);
	else
		return fsread(rpc->afid, buf, buflen);
}

static long
rpcwrite(AuthRpc *rpc, void *buf, int buflen)
{
	if (rpc->afd >= 0)
		return write(rpc->afd, buf, buflen);
	else
		return fswrite(rpc->afid, buf, buflen);
}

static int
classify(char *buf, uint n, AuthRpc *rpc)
{
	int i, len;

	for(i=0; i<nelem(tab); i++){
		len = strlen(tab[i].verb);
		if(n >= len && memcmp(buf, tab[i].verb, len) == 0 && (n==len || buf[len]==' ')){
			if(n==len){
				rpc->narg = 0;
				rpc->arg = "";
			}else{
				rpc->narg = n - (len+1);
				rpc->arg = (char*)buf+len+1;
			}
			return tab[i].val;
		}
	}
	werrstr("malformed rpc response: %s", buf);
	return ARrpcfailure;
}

AuthRpc*
auth_allocrpc(void)
{
	AuthRpc *rpc;

	rpc = mallocz(sizeof(*rpc), 1);
	if(rpc == nil)
		return nil;
	rpc->afd = open("/mnt/factotum/rpc", ORDWR);
	if(rpc->afd < 0){
		rpc->afid = nsopen("factotum", nil, "rpc", ORDWR);
		if(rpc->afid == nil){
			free(rpc);
			return nil;
		}
	}
	return rpc;
}

void
auth_freerpc(AuthRpc *rpc)
{
	if(rpc == nil)
		return;
	if(rpc->afd >= 0)
		close(rpc->afd);
	if(rpc->afid != nil)
		fsclose(rpc->afid);
	free(rpc);
}

uint
auth_rpc(AuthRpc *rpc, char *verb, void *a, int na)
{
	int l, n, type;
	char *f[4];

	l = strlen(verb);
	if(na+l+1 > AuthRpcMax){
		werrstr("rpc too big");
		return ARtoobig;
	}

	memmove(rpc->obuf, verb, l);
	rpc->obuf[l] = ' ';
	memmove(rpc->obuf+l+1, a, na);
	if((n=rpcwrite(rpc, rpc->obuf, l+1+na)) != l+1+na){
		if(n >= 0)
			werrstr("auth_rpc short write");
		return ARrpcfailure;
	}

	if((n=rpcread(rpc, rpc->ibuf, AuthRpcMax)) < 0)
		return ARrpcfailure;
	rpc->ibuf[n] = '\0';

	/*
	 * Set error string for good default behavior.
	 */
	switch(type = classify(rpc->ibuf, n, rpc)){
	default:
		werrstr("unknown rpc type %d (bug in auth_rpc.c)", type);
		break;
	case ARok:
		break;
	case ARrpcfailure:
		break;
	case ARerror:
		if(rpc->narg == 0)
			werrstr("unspecified rpc error");
		else
			werrstr("%s", rpc->arg);
		break;
	case ARneedkey:
		werrstr("needkey %s", rpc->arg);
		break;
	case ARbadkey:
		if(getfields(rpc->arg, f, nelem(f), 0, "\n") < 2)
			werrstr("badkey %s", rpc->arg);
		else
			werrstr("badkey %s", f[1]);
		break;
	case ARphase:
		werrstr("phase error %s", rpc->arg);
		break;
	}
	return type;
}