Blob


1 #include "std.h"
2 #include "dat.h"
4 /*
5 * key attr=val... - add a key
6 * the attr=val pairs are protocol-specific.
7 * for example, both of these are valid:
8 * key p9sk1 gre cs.bell-labs.com mysecret
9 * key p9sk1 gre cs.bell-labs.com 11223344556677 fmt=des7hex
10 * delkey ... - delete a key
11 * if given, the attr=val pairs are used to narrow the search
12 * [maybe should require a password?]
13 *
14 * debug - toggle debugging
15 */
17 static char *msg[] = {
18 "key",
19 "delkey",
20 "debug",
21 };
23 static int
24 classify(char *s)
25 {
26 int i;
28 for(i=0; i<nelem(msg); i++)
29 if(strcmp(msg[i], s) == 0)
30 return i;
31 return -1;
32 }
34 int
35 ctlwrite(char *a)
36 {
37 char *p;
38 int i, nmatch, ret;
39 Attr *attr, *kpa, **l, **lpriv, **lprotos, *pa, *priv, *protos;
40 Key *k;
41 Proto *proto;
43 while(*a == ' ' || *a == '\t' || *a == '\n')
44 a++;
46 if(a[0] == '#' || a[0] == '\0')
47 return 0;
49 /*
50 * it would be nice to emit a warning of some sort here.
51 * we ignore all but the first line of the write. this helps
52 * both with things like "echo delkey >/mnt/factotum/ctl"
53 * and writes that (incorrectly) contain multiple key lines.
54 */
55 if(p = strchr(a, '\n')){
56 if(p[1] != '\0'){
57 werrstr("multiline write not allowed");
58 return -1;
59 }
60 *p = '\0';
61 }
63 if((p = strchr(a, ' ')) == nil)
64 p = "";
65 else
66 *p++ = '\0';
67 switch(classify(a)){
68 default:
69 werrstr("unknown verb %s", a);
70 return -1;
71 case 0: /* key */
72 attr = parseattr(p);
73 /* separate out proto= attributes */
74 lprotos = &protos;
75 for(l=&attr; (*l); ){
76 if(strcmp((*l)->name, "proto") == 0){
77 *lprotos = *l;
78 lprotos = &(*l)->next;
79 *l = (*l)->next;
80 }else
81 l = &(*l)->next;
82 }
83 *lprotos = nil;
84 if(protos == nil){
85 werrstr("key without protos");
86 freeattr(attr);
87 return -1;
88 }
90 /* separate out private attributes */
91 lpriv = &priv;
92 for(l=&attr; (*l); ){
93 if((*l)->name[0] == '!'){
94 *lpriv = *l;
95 lpriv = &(*l)->next;
96 *l = (*l)->next;
97 }else
98 l = &(*l)->next;
99 }
100 *lpriv = nil;
102 /* add keys */
103 ret = 0;
104 for(pa=protos; pa; pa=pa->next){
105 if((proto = protolookup(pa->val)) == nil){
106 werrstr("unknown proto %s", pa->val);
107 ret = -1;
108 continue;
110 if(proto->keyprompt){
111 kpa = parseattr(proto->keyprompt);
112 if(!matchattr(kpa, attr, priv)){
113 freeattr(kpa);
114 werrstr("missing attributes -- want %s", proto->keyprompt);
115 ret = -1;
116 continue;
118 freeattr(kpa);
120 k = emalloc(sizeof(Key));
121 k->attr = mkattr(AttrNameval, "proto", proto->name, copyattr(attr));
122 k->privattr = copyattr(priv);
123 k->ref = 1;
124 k->proto = proto;
125 if(proto->checkkey && (*proto->checkkey)(k) < 0){
126 ret = -1;
127 keyclose(k);
128 continue;
130 keyadd(k);
131 keyclose(k);
133 freeattr(attr);
134 freeattr(priv);
135 freeattr(protos);
136 return ret;
137 case 1: /* delkey */
138 nmatch = 0;
139 attr = parseattr(p);
140 for(pa=attr; pa; pa=pa->next){
141 if(pa->type != AttrQuery && pa->name[0]=='!'){
142 werrstr("only !private? patterns are allowed for private fields");
143 freeattr(attr);
144 return -1;
147 for(i=0; i<ring.nkey; ){
148 if(matchattr(attr, ring.key[i]->attr, ring.key[i]->privattr)){
149 nmatch++;
150 keyclose(ring.key[i]);
151 ring.nkey--;
152 memmove(&ring.key[i], &ring.key[i+1], (ring.nkey-i)*sizeof(ring.key[0]));
153 }else
154 i++;
156 freeattr(attr);
157 if(nmatch == 0){
158 werrstr("found no keys to delete");
159 return -1;
161 return 0;
162 case 2: /* debug */
163 debug ^= 1;
164 return 0;