6 parsepubkey(char *s, RSApub *key, char **sp, int base)
12 n = strtoul(s, &p, 10);
14 if(n < 256 || !isspace(*p)){ /* maybe this is a host name */
16 s = strpbrk(s, " \t");
21 s += strspn(s, " \t");
23 n = strtoul(s, &p, 10);
24 if(n < 256 || !isspace(*p)){
31 if((key->ek = strtomp(p, &p, base, nil)) == nil
32 || (key->n = strtomp(p, &p, base, nil)) == nil
33 || (*p != '\0' && !isspace(*p))
34 || mpsignif(key->n) < 256){ /* 256 is just a sanity check */
45 p += strspn(p, " \t");
47 host = emalloc(strlen(p)+1);
58 readpublickey(Biobuf *b, char **sp)
68 if((s = Brdstr(b, '\n', 1)) == nil){
76 if(parsepubkey(s, key, sp, 10)==0
77 || parsepubkey(s, key, sp, 16)==0)
79 fprint(2, "warning: skipping line '%s'; cannot parse\n", s);
85 match(char *pattern, char *aliases)
90 for(s=pattern; s && *s; s=snext){
91 if((snext=strchr(s, ',')) != nil)
93 for(a=aliases; a && *a; a=anext){
94 if((anext=strchr(a, ',')) != nil){
99 if(ae-a == strlen(s) && memcmp(s, a, ae-a)==0)
107 findkey(char *keyfile, char *host, RSApub *key)
113 if((b = Bopen(keyfile, OREAD)) == nil)
117 if((k = readpublickey(b, &h)) == nil){
121 if(match(h, host) != 0){
126 if(mpcmp(k->n, key->n) != 0 || mpcmp(k->ek, key->ek) != 0){
140 replacekey(char *keyfile, char *host, RSApub *hostkey)
147 nkey = smprint("%s.new", keyfile);
151 if((br = Bopen(keyfile, OREAD)) == nil){
155 if((bw = Bopen(nkey, OWRITE)) == nil){
161 while((k = readpublickey(br, &h)) != nil){
162 if(match(h, host) != 0){
163 Bprint(bw, "%s %d %.10B %.10B\n",
164 h, mpsignif(k->n), k->ek, k->n);
169 Bprint(bw, "%s %d %.10B %.10B\n", host, mpsignif(hostkey->n), hostkey->ek, hostkey->n);
175 fprint(2, "new key file disappeared?\n");
180 p = strrchr(d->name, '.');
181 if(p==nil || strcmp(p, ".new")!=0){
182 fprint(2, "new key file changed names? %s to %s\n", nkey, d->name);
191 if(remove(keyfile) < 0){
192 fprint(2, "error removing %s: %r\n", keyfile);
197 if(dirwstat(nkey, &nd) < 0){
198 fprint(2, "error renaming %s to %s: %r\n", nkey, d->name);
209 appendkey(char *keyfile, char *host, RSApub *key)
213 if((fd = open(keyfile, OWRITE)) < 0){
214 fd = create(keyfile, OWRITE, 0666);
216 fprint(2, "cannot open nor create %s: %r\n", keyfile);
220 if(seek(fd, 0, 2) < 0
221 || fprint(fd, "%s %d %.10B %.10B\n", host, mpsignif(key->n), key->ek, key->n) < 0){