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
|
#include "std.h"
#include "dat.h"
int debug;
char *factname = "factotum";
char *service = nil;
char *owner;
char *authaddr;
void gflag(char*);
void
usage(void)
{
fprint(2, "usage: factotum [-Dd] [-a authaddr] [-m mtpt]\n");
fprint(2, " or factotum -g keypattern\n");
fprint(2, " or factotum -g 'badkeyattr\nmsg\nkeypattern'");
exits("usage");
}
void
threadmain(int argc, char *argv[])
{
char *mtpt;
mtpt = "/mnt";
owner = getuser();
quotefmtinstall();
fmtinstall('A', attrfmt);
fmtinstall('H', encodefmt);
fmtinstall('N', attrnamefmt);
if(argc == 3 && strcmp(argv[1], "-g") == 0){
gflag(argv[2]);
exits(nil);
}
ARGBEGIN{
default:
usage();
case 'D':
chatty9p++;
break;
case 'a':
authaddr = EARGF(usage());
break;
case 'g':
usage();
case 'm':
mtpt = EARGF(usage());
break;
case 's':
service = EARGF(usage());
break;
}ARGEND
if(argc != 0)
usage();
threadpostmountsrv(&fs, service, mtpt, MBEFORE);
threadexits(nil);
}
/*
* prompt user for a key. don't care about memory leaks, runs standalone
*/
static Attr*
promptforkey(int fd, char *params)
{
char *v;
Attr *a, *attr;
char *def;
attr = _parseattr(params);
fprint(fd, "!adding key:");
for(a=attr; a; a=a->next)
if(a->type != AttrQuery && a->name[0] != '!')
fprint(fd, " %q=%q", a->name, a->val);
fprint(fd, "\n");
for(a=attr; a; a=a->next){
v = a->name;
if(a->type != AttrQuery || v[0]=='!')
continue;
def = nil;
if(strcmp(v, "user") == 0)
def = getuser();
a->val = readcons(v, def, 0);
if(a->val == nil)
sysfatal("user terminated key input");
a->type = AttrNameval;
}
for(a=attr; a; a=a->next){
v = a->name;
if(a->type != AttrQuery || v[0]!='!')
continue;
def = nil;
if(strcmp(v+1, "user") == 0)
def = getuser();
a->val = readcons(v+1, def, 1);
if(a->val == nil)
sysfatal("user terminated key input");
a->type = AttrNameval;
}
fprint(fd, "!\n");
close(fd);
return attr;
}
/*
* send a key to the mounted factotum
*/
static int
sendkey(Attr *attr)
{
int fd, rv;
char buf[1024];
fd = open("/mnt/factotum/ctl", ORDWR);
if(fd < 0)
sysfatal("opening /mnt/factotum/ctl: %r");
rv = fprint(fd, "key %A\n", attr);
read(fd, buf, sizeof buf);
close(fd);
return rv;
}
static void
askuser(int fd, char *params)
{
Attr *attr;
attr = promptforkey(fd, params);
if(attr == nil)
sysfatal("no key supplied");
if(sendkey(attr) < 0)
sysfatal("sending key to factotum: %r");
}
void
gflag(char *s)
{
char *f[4];
int nf;
int fd;
if((fd = open("/dev/cons", ORDWR)) < 0)
sysfatal("open /dev/cons: %r");
nf = getfields(s, f, nelem(f), 0, "\n");
if(nf == 1){ /* needkey or old badkey */
fprint(fd, "\n");
askuser(fd, s);
exits(nil);
}
if(nf == 3){ /* new badkey */
fprint(fd, "\n");
fprint(fd, "!replace: %s\n", f[0]);
fprint(fd, "!because: %s\n", f[1]);
askuser(fd, f[2]);
exits(nil);
}
usage();
}
|