]> git.lizzy.rs Git - plan9front.git/blob - sys/src/libndb/ndbipinfo.c
ssh: loop keyboard-interactive on failure
[plan9front.git] / sys / src / libndb / ndbipinfo.c
1 #include <u.h>
2 #include <libc.h>
3 #include <bio.h>
4 #include <ndb.h>
5 #include <ip.h>
6
7 enum
8 {
9         Ffound= 1<<0,
10         Fignore=1<<1,
11         Faddr=  1<<2,
12 };
13
14 static Ndbtuple*        filter(Ndb *db, Ndbtuple *t, Ndbtuple *f);
15 static Ndbtuple*        mkfilter(int argc, char **argv);
16 static int              filtercomplete(Ndbtuple *f);
17 static int              prefixlen(uchar *ip);
18 static Ndbtuple*        subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix);
19
20 /* make a filter to be used in filter */
21 static Ndbtuple*
22 mkfilter(int argc, char **argv)
23 {
24         Ndbtuple *t, *first, *last;
25         char *p;
26
27         last = first = nil;
28         while(argc-- > 0){
29                 t = ndbnew(0, 0);
30                 if(first)
31                         last->entry = t;
32                 else
33                         first = t;
34                 last = t;
35                 p = *argv++;
36                 if(*p == '@'){                  /* @attr=val ? */
37                         t->ptr |= Faddr;        /* return resolved address(es) */
38                         p++;
39                 }
40                 strncpy(t->attr, p, sizeof(t->attr)-1);
41         }
42         ndbsetmalloctag(first, getcallerpc(&argc));
43         return first;
44 }
45
46 /* return true if every pair of filter has been used */
47 static int
48 filtercomplete(Ndbtuple *f)
49 {
50         for(; f; f = f->entry)
51                 if((f->ptr & Fignore) == 0)
52                         return 0;
53         return 1;
54 }
55
56 /* set the attribute of all entries in a tuple */
57 static Ndbtuple*
58 setattr(Ndbtuple *t, char *attr)
59 {
60         Ndbtuple *nt;
61
62         for(nt = t; nt; nt = nt->entry)
63                 strcpy(nt->attr, attr);
64         return t;
65 }
66
67 /*
68  *  return only the attr/value pairs in t maching the filter, f.
69  *  others are freed.  line structure is preserved.
70  */
71 static Ndbtuple*
72 filter(Ndb *db, Ndbtuple *t, Ndbtuple *f)
73 {
74         Ndbtuple *nt, *nf, *next;
75
76         /* filter out what we don't want */
77         for(nt = t; nt; nt = next){
78                 next = nt->entry;
79
80                 /* look through filter */
81                 for(nf = f; nf != nil; nf = nf->entry){
82                         if(!(nf->ptr&Fignore) && strcmp(nt->attr, nf->attr) == 0)
83                                 break;
84                 }
85                 if(nf == nil){
86                         /* remove nt from t */
87                         t = ndbdiscard(t, nt);
88                 } else {
89                         if(nf->ptr & Faddr)
90                                 t = ndbsubstitute(t, nt, setattr(ndbgetipaddr(db, nt->val), nt->attr));
91                         nf->ptr |= Ffound;
92                 }
93         }
94
95         /* remember filter etnries that matched */
96         for(nf = f; nf != nil; nf = nf->entry)
97                 if(nf->ptr & Ffound)
98                         nf->ptr = (nf->ptr & ~Ffound) | Fignore;
99
100         ndbsetmalloctag(t, getcallerpc(&db));
101         return t;
102 }
103
104 static int
105 prefixlen(uchar *ip)
106 {
107         int y, i;
108
109         for(y = IPaddrlen-1; y >= 0; y--)
110                 for(i = 8; i > 0; i--)
111                         if(ip[y] & (1<<(8-i)))
112                                 return y*8 + i;
113         return 0;
114 }
115
116 /*
117  *  look through a containing subset
118  */
119 static Ndbtuple*
120 subnet(Ndb *db, uchar *net, Ndbtuple *f, int prefix)
121 {
122         Ndbs s;
123         Ndbtuple *t, *nt, *xt;
124         char netstr[128];
125         uchar mask[IPaddrlen];
126         int masklen;
127
128         t = nil;
129         sprint(netstr, "%I", net);
130         nt = ndbsearch(db, &s, "ip", netstr);
131         while(nt != nil){
132                 xt = ndbfindattr(nt, nt, "ipnet");
133                 if(xt){
134                         xt = ndbfindattr(nt, nt, "ipmask");
135                         if(xt)
136                                 parseipmask(mask, xt->val);
137                         else
138                                 ipmove(mask, defmask(net));
139                         masklen = prefixlen(mask);
140                         if(masklen <= prefix){
141                                 t = ndbconcatenate(t, filter(db, nt, f));
142                                 nt = nil;
143                         }
144                 }
145                 ndbfree(nt);
146                 nt = ndbsnext(&s, "ip", netstr);
147         }
148         ndbsetmalloctag(t, getcallerpc(&db));
149         return t;
150 }
151
152 /*
153  *  fill in all the requested attributes for a system.
154  *  if the system's entry doesn't have all required,
155  *  walk through successively more inclusive networks
156  *  for inherited attributes.
157  */
158 Ndbtuple*
159 ndbipinfo(Ndb *db, char *attr, char *val, char **alist, int n)
160 {
161         Ndbtuple *t, *nt, *f;
162         Ndbs s;
163         char *ipstr;
164         uchar net[IPaddrlen], ip[IPaddrlen];
165         int prefix, smallestprefix, force;
166         vlong r;
167
168         /* just in case */
169         fmtinstall('I', eipfmt);
170         fmtinstall('M', eipfmt);
171
172         /* get needed attributes */
173         f = mkfilter(n, alist);
174
175         /*
176          *  first look for a matching entry with an ip address
177          */
178         t = nil;
179         ipstr = ndbgetvalue(db, &s, attr, val, "ip", &nt);
180         if(ipstr == nil){
181                 /* none found, make one up */
182                 if(strcmp(attr, "ip") != 0) {
183                         ndbfree(f);
184                         return nil;     
185                 }
186                 t = ndbnew("ip", val);
187                 t->line = t;
188                 t->entry = nil;
189                 r = parseip(net, val);
190                 if(r == -1)
191                         ndbfree(t);
192         } else {
193                 /* found one */
194                 while(nt != nil){
195                         nt = ndbreorder(nt, s.t);
196                         t = ndbconcatenate(t, nt);
197                         nt = ndbsnext(&s, attr, val);
198                 }
199                 r = parseip(net, ipstr);
200                 free(ipstr);
201         }
202         if(r < 0){
203                 ndbfree(f);
204                 return nil;
205         }
206         ipmove(ip, net);
207         t = filter(db, t, f);
208
209         /*
210          *  now go through subnets to fill in any missing attributes
211          */
212         if(isv4(net)){
213                 prefix = 127;
214                 smallestprefix = 100;
215                 force = 0;
216         } else {
217                 /* in v6, the last 8 bytes have no structure (we hope) */
218                 prefix = 64;
219                 smallestprefix = 2;
220                 memset(net+8, 0, 8);
221                 force = 1;
222         }
223
224         /*
225          *  to find a containing network, keep turning off
226          *  the lower bit and look for a network with
227          *  that address and a shorter mask.  tedius but
228          *  complete, we may need to find a trick to speed this up.
229          */
230         for(; prefix >= smallestprefix; prefix--){
231                 if(filtercomplete(f))
232                         break;
233                 if(!force && (net[prefix/8] & (1<<(7-(prefix%8)))) == 0)
234                         continue;
235                 force = 0;
236                 net[prefix/8] &= ~(1<<(7-(prefix%8)));
237                 t = ndbconcatenate(t, subnet(db, net, f, prefix));
238         }
239
240         /*
241          *  if there's an unfulfilled ipmask, make one up
242          */
243         nt = ndbfindattr(f, f, "ipmask");
244         if(nt && !(nt->ptr & Fignore)){
245                 char x[64];
246
247                 snprint(x, sizeof(x), "%M", defmask(ip));
248                 t = ndbconcatenate(t, ndbnew("ipmask", x));
249         }
250
251         ndbfree(f);
252         ndbsetmalloctag(t, getcallerpc(&db));
253         return t;
254 }