aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/auth/factotum/pass.c
blob: b3d4cb6ad2c87b9b7755d314e63f41bebde4c72a (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
/*
 * This is just a repository for a password.
 * We don't want to encourage this, there's
 * no server side.
 */

#include "dat.h"

typedef struct State State;
struct State 
{
	Key *key;
};

enum
{
	HavePass,
	Maxphase,
};

static char *phasenames[Maxphase] =
{
[HavePass]	"HavePass",
};

static int
passinit(Proto *p, Fsstate *fss)
{
	int ask;
	Key *k;
	State *s;

	k = findkey(fss, Kuser, &ask, 0, fss->attr, "%s", p->keyprompt);
	if(k == nil){
		if(ask)
			return RpcNeedkey;
		return failure(fss, nil);
	}
	setattrs(fss->attr, k->attr);
	s = emalloc(sizeof(*s));
	s->key = k;
	fss->ps = s;
	return RpcOk;
}

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

	s = fss->ps;
	if(s->key)
		closekey(s->key);
	free(s);
}

static int
passread(Fsstate *fss, void *va, uint *n)
{
	int m;
	char buf[500];
	char *pass, *user;
	State *s;

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

	case HavePass:
		user = strfindattr(s->key->attr, "user");
		pass = strfindattr(s->key->privattr, "!password");
		if(user==nil || pass==nil)
			return failure(fss, "passread cannot happen");
		snprint(buf, sizeof buf, "%q %q", user, pass);
		m = strlen(buf);
		if(m > *n)
			return toosmall(fss, m);
		*n = m;
		memmove(va, buf, m);
		return RpcOk;
	}
}

static int
passwrite(Fsstate *fss, void*, uint)
{
	return phaseerror(fss, "write");
}

Proto pass =
{
.name=		"pass",
.init=		passinit,
.write=		passwrite,
.read=		passread,
.close=		passclose,
.addkey=		replacekey,
.keyprompt=	"user? !password?",
};