chiark / gitweb /
util.[ch]: Factor out hex encoding and decoding utilities.
[secnet.git] / util.c
1 /*
2  * util.c
3  * - output and logging support
4  * - program lifetime support
5  * - IP address and subnet munging routines
6  * - MPI convenience functions
7  */
8 /*
9  * This file is part of secnet.
10  * See README for full list of copyright holders.
11  *
12  * secnet is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  * 
17  * secnet is distributed in the hope that it will be useful, but
18  * WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * General Public License for more details.
21  * 
22  * You should have received a copy of the GNU General Public License
23  * version 3 along with secnet; if not, see
24  * https://www.gnu.org/licenses/gpl.html.
25  */
26
27 #include "secnet.h"
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <assert.h>
34 #include <sys/wait.h>
35 #include <adns.h>
36 #include "util.h"
37 #include "unaligned.h"
38 #include "magic.h"
39 #include "ipaddr.h"
40
41 #define MIN_BUFFER_SIZE 64
42 #define DEFAULT_BUFFER_SIZE 4096
43 #define MAX_BUFFER_SIZE 131072
44
45 static const char *hexdigits="0123456789abcdef";
46
47 uint32_t current_phase=0;
48
49 struct phase_hook {
50     hook_fn *fn;
51     void *state;
52     LIST_ENTRY(phase_hook) entry;
53 };
54
55 static LIST_HEAD(, phase_hook) hooks[NR_PHASES];
56
57 char *safe_strdup(const char *s, const char *message)
58 {
59     char *d;
60     d=strdup(s);
61     if (!d) {
62         fatal_perror("%s",message);
63     }
64     return d;
65 }
66
67 void *safe_malloc(size_t size, const char *message)
68 {
69     void *r;
70     if (!size)
71         return 0;
72     r=malloc(size);
73     if (!r) {
74         fatal_perror("%s",message);
75     }
76     return r;
77 }
78 void *safe_realloc_ary(void *p, size_t size, size_t count,
79                        const char *message) {
80     if (count >= INT_MAX/size) {
81         fatal("array allocation overflow: %s", message);
82     }
83     assert(size && count);
84     p = realloc(p, size*count);
85     if (!p)
86         fatal_perror("%s", message);
87     return p;
88 }
89
90 void *safe_malloc_ary(size_t size, size_t count, const char *message) {
91     if (!size || !count)
92         return 0;
93     return safe_realloc_ary(0,size,count,message);
94 }
95
96 /* Hex-encode a buffer, and return the hex in a freshly allocated string. */
97 string_t hex_encode(const uint8_t *bin, int binsize)
98 {
99     char *buff;
100     int i;
101
102     buff=safe_malloc(binsize*2 + 1,"hex_encode");
103
104     for (i=0; i<binsize; i++) {
105         buff[i*2]=hexdigits[(bin[i] & 0xf0) >> 4];
106         buff[i*2+1]=hexdigits[(bin[i] & 0xf)];
107     }
108     buff[binsize*2]=0;
109     return buff;
110 }
111
112 static uint8_t hexval(uint8_t c)
113 {
114     switch (c) {
115     case '0': return 0;
116     case '1': return 1;
117     case '2': return 2;
118     case '3': return 3;
119     case '4': return 4;
120     case '5': return 5;
121     case '6': return 6;
122     case '7': return 7;
123     case '8': return 8;
124     case '9': return 9;
125     case 'a': return 10;
126     case 'A': return 10;
127     case 'b': return 11;
128     case 'B': return 11;
129     case 'c': return 12;
130     case 'C': return 12;
131     case 'd': return 13;
132     case 'D': return 13;
133     case 'e': return 14;
134     case 'E': return 14;
135     case 'f': return 15;
136     case 'F': return 15;
137     }
138     return -1;
139 }
140
141 bool_t hex_decode(uint8_t *buffer, int32_t buflen, int32_t *outlen,
142                   cstring_t hb, bool_t allow_odd_nibble)
143 {
144     int i = 0, j = 0, l = strlen(hb), hi, lo;
145     bool_t ok = False;
146
147     if (!l || !buflen) { ok = !l; goto done; }
148     if (l&1) {
149         /* The number starts with a half-byte */
150         if (!allow_odd_nibble) goto done;
151         lo = hexval(hb[j++]); if (lo < 0) goto done;
152         buffer[i++] = lo;
153     }
154     for (; hb[j] && i < buflen; i++) {
155         hi = hexval(hb[j++]);
156         lo = hexval(hb[j++]);
157         if (hi < 0 || lo < 0) goto done;
158         buffer[i] = (hi << 4) | lo;
159     }
160     ok = !hb[j];
161 done:
162     *outlen = i;
163     return ok;
164 }
165
166 /* Convert a buffer into its MP_INT representation */
167 void read_mpbin(MP_INT *a, uint8_t *bin, int binsize)
168 {
169     char *buff = hex_encode(bin, binsize);
170     mpz_set_str(a, buff, 16);
171     free(buff);
172 }
173
174 /* Convert a MP_INT into a hex string */
175 char *write_mpstring(MP_INT *a)
176 {
177     char *buff;
178
179     buff=safe_malloc(mpz_sizeinbase(a,16)+2,"write_mpstring");
180     mpz_get_str(buff, 16, a);
181     return buff;
182 }
183
184 /* Convert a MP_INT into a buffer; return length; truncate if necessary */
185 int32_t write_mpbin(MP_INT *a, uint8_t *buffer, int32_t buflen)
186 {
187     char *hb = write_mpstring(a);
188     int32_t len;
189     hex_decode(buffer, buflen, &len, hb, True);
190     free(hb);
191     return len;
192 }
193
194 #define DEFINE_SETFDFLAG(fn,FL,FLAG)                                    \
195 void fn(int fd) {                                                       \
196     int r=fcntl(fd, F_GET##FL);                                         \
197     if (r<0) fatal_perror("fcntl(,F_GET" #FL ") failed");               \
198     r=fcntl(fd, F_SET##FL, r|FLAG);                                     \
199     if (r<0) fatal_perror("fcntl(,F_SET" #FL ",|" #FLAG ") failed");    \
200 }
201
202 DEFINE_SETFDFLAG(setcloexec,FD,FD_CLOEXEC);
203 DEFINE_SETFDFLAG(setnonblock,FL,O_NONBLOCK);
204
205 void pipe_cloexec(int fd[2]) {
206     int r=pipe(fd);
207     if (r) fatal_perror("pipe");
208     setcloexec(fd[0]);
209     setcloexec(fd[1]);
210 }
211
212 static const char *phases[NR_PHASES]={
213     "PHASE_INIT",
214     "PHASE_GETOPTS",
215     "PHASE_READCONFIG",
216     "PHASE_SETUP",
217     "PHASE_DAEMONIZE",
218     "PHASE_GETRESOURCES",
219     "PHASE_DROPPRIV",
220     "PHASE_RUN",
221     "PHASE_SHUTDOWN",
222     "PHASE_CHILDPERSIST"
223 };
224
225 void enter_phase(uint32_t new_phase)
226 {
227     struct phase_hook *i;
228
229     if (!LIST_EMPTY(&hooks[new_phase]))
230         Message(M_DEBUG_PHASE,"Running hooks for %s...\n", phases[new_phase]);
231     current_phase=new_phase;
232
233     LIST_FOREACH(i, &hooks[new_phase], entry)
234         i->fn(i->state, new_phase);
235     Message(M_DEBUG_PHASE,"Now in %s\n",phases[new_phase]);
236 }
237
238 void phase_hooks_init(void)
239 {
240     int i;
241     for (i=0; i<NR_PHASES; i++)
242         LIST_INIT(&hooks[i]);
243 }
244
245 void clear_phase_hooks(uint32_t phase)
246 {
247     struct phase_hook *h, *htmp;
248     LIST_FOREACH_SAFE(h, &hooks[phase], entry, htmp)
249         free(h);
250     LIST_INIT(&hooks[phase]);
251 }
252
253 bool_t add_hook(uint32_t phase, hook_fn *fn, void *state)
254 {
255     struct phase_hook *h;
256
257     NEW(h);
258     h->fn=fn;
259     h->state=state;
260     LIST_INSERT_HEAD(&hooks[phase],h,entry);
261     return True;
262 }
263
264 bool_t remove_hook(uint32_t phase, hook_fn *fn, void *state)
265 {
266     fatal("remove_hook: not implemented");
267
268     return False;
269 }
270
271 void vslilog(struct log_if *lf, int priority, const char *message, va_list ap)
272 {
273     lf->vlogfn(lf->st,priority,message,ap);
274 }
275
276 void slilog(struct log_if *lf, int priority, const char *message, ...)
277 {
278     va_list ap;
279     
280     va_start(ap,message);
281     vslilog(lf,priority,message,ap);
282     va_end(ap);
283 }
284
285 struct buffer {
286     closure_t cl;
287     struct buffer_if ops;
288 };
289
290 void buffer_assert_free(struct buffer_if *buffer, cstring_t file,
291                         int line)
292 {
293     if (!buffer->free) {
294         fprintf(stderr,"secnet: BUF_ASSERT_FREE, %s line %d, owned by %s",
295                 file,line,buffer->owner);
296         assert(!"buffer_assert_free failure");
297     }
298 }
299
300 void buffer_assert_used(struct buffer_if *buffer, cstring_t file,
301                         int line)
302 {
303     if (buffer->free) {
304         fprintf(stderr,"secnet: BUF_ASSERT_USED, %s line %d, last owned by %s",
305                 file,line,buffer->owner);
306         assert(!"buffer_assert_used failure");
307     }
308 }
309
310 void buffer_init(struct buffer_if *buffer, int32_t max_start_pad)
311 {
312     assert(max_start_pad<=buffer->alloclen);
313     buffer->start=buffer->base+max_start_pad;
314     buffer->size=0;
315 }
316
317 void buffer_destroy(struct buffer_if *buf)
318 {
319     BUF_ASSERT_FREE(buf);
320     free(buf->base);
321     buf->start=buf->base=0;
322     buf->size=buf->alloclen=0;
323 }
324
325 void *buf_append(struct buffer_if *buf, int32_t amount) {
326     void *p;
327     assert(amount <= buf_remaining_space(buf));
328     p=buf->start + buf->size;
329     buf->size+=amount;
330     return p;
331 }
332
333 void *buf_prepend(struct buffer_if *buf, int32_t amount) {
334     assert(amount <= buf->start - buf->base);
335     buf->size+=amount;
336     return buf->start-=amount;
337 }
338
339 void *buf_unappend(struct buffer_if *buf, int32_t amount) {
340     if (buf->size < amount) return 0;
341     return buf->start+(buf->size-=amount);
342 }
343
344 void *buf_unprepend(struct buffer_if *buf, int32_t amount) {
345     void *p;
346     if (buf->size < amount) return 0;
347     p=buf->start;
348     buf->start+=amount;
349     buf->size-=amount;
350     return p;
351 }
352
353 /* Append a two-byte length and the string to the buffer. Length is in
354    network byte order. */
355 void buf_append_string(struct buffer_if *buf, cstring_t s)
356 {
357     size_t len;
358
359     len=strlen(s);
360     /* fixme: if string is longer than 65535, result is a corrupted packet */
361     buf_append_uint16(buf,len);
362     BUF_ADD_BYTES(append,buf,s,len);
363 }
364
365 void buffer_new(struct buffer_if *buf, int32_t len)
366 {
367     buf->free=True;
368     buf->owner=NULL;
369     buf->flags=0;
370     buf->loc.file=NULL;
371     buf->loc.line=0;
372     buf->size=0;
373     buf->alloclen=len;
374     buf->start=NULL;
375     buf->base=safe_malloc(len,"buffer_new");
376 }
377
378 void buffer_readonly_view(struct buffer_if *buf, const void *data, int32_t len)
379 {
380     buf->free=False;
381     buf->owner="READONLY";
382     buf->flags=0;
383     buf->loc.file=NULL;
384     buf->loc.line=0;
385     buf->size=buf->alloclen=len;
386     buf->base=buf->start=(uint8_t*)data;
387 }
388
389 void buffer_readonly_clone(struct buffer_if *out, const struct buffer_if *in)
390 {
391     buffer_readonly_view(out,in->start,in->size);
392 }
393
394 void buffer_copy(struct buffer_if *dst, const struct buffer_if *src)
395 {
396     if (dst->alloclen < src->alloclen) {
397         dst->base=realloc(dst->base,src->alloclen);
398         if (!dst->base) fatal_perror("buffer_copy");
399         dst->alloclen = src->alloclen;
400     }
401     dst->start = dst->base + (src->start - src->base);
402     dst->size = src->size;
403     memcpy(dst->start, src->start, dst->size);
404 }
405
406 static list_t *buffer_apply(closure_t *self, struct cloc loc, dict_t *context,
407                             list_t *args)
408 {
409     struct buffer *st;
410     item_t *item;
411     dict_t *dict;
412     bool_t lockdown=False;
413     uint32_t len=DEFAULT_BUFFER_SIZE;
414     
415     NEW(st);
416     st->cl.description="buffer";
417     st->cl.type=CL_BUFFER;
418     st->cl.apply=NULL;
419     st->cl.interface=&st->ops;
420
421     /* First argument, if present, is buffer length */
422     item=list_elem(args,0);
423     if (item) {
424         if (item->type!=t_number) {
425             cfgfatal(st->ops.loc,"buffer","first parameter must be a "
426                      "number (buffer size)\n");
427         }
428         len=item->data.number;
429         if (len<MIN_BUFFER_SIZE) {
430             cfgfatal(st->ops.loc,"buffer","ludicrously small buffer size\n");
431         }
432         if (len>MAX_BUFFER_SIZE) {
433             cfgfatal(st->ops.loc,"buffer","ludicrously large buffer size\n");
434         }
435     }
436     /* Second argument, if present, is a dictionary */
437     item=list_elem(args,1);
438     if (item) {
439         if (item->type!=t_dict) {
440             cfgfatal(st->ops.loc,"buffer","second parameter must be a "
441                      "dictionary\n");
442         }
443         dict=item->data.dict;
444         lockdown=dict_read_bool(dict,"lockdown",False,"buffer",st->ops.loc,
445                                 False);
446     }
447
448     buffer_new(&st->ops,len);
449     if (lockdown) {
450         /* XXX mlock the buffer if possible */
451     }
452     
453     return new_closure(&st->cl);
454 }
455
456 void send_nak(const struct comm_addr *dest, uint32_t our_index,
457               uint32_t their_index, uint32_t msgtype,
458               struct buffer_if *buf, const char *logwhy)
459 {
460     buffer_init(buf,calculate_max_start_pad());
461     buf_append_uint32(buf,their_index);
462     buf_append_uint32(buf,our_index);
463     buf_append_uint32(buf,LABEL_NAK);
464     if (logwhy)
465         Message(M_INFO,"%s: %08"PRIx32"<-%08"PRIx32": %08"PRIx32":"
466                 " %s; sending NAK\n",
467                 comm_addr_to_string(dest),
468                 our_index, their_index, msgtype, logwhy);
469     dest->comm->sendmsg(dest->comm->st, buf, dest, 0);
470 }
471
472 int consttime_memeq(const void *s1in, const void *s2in, size_t n)
473 {
474     const uint8_t *s1=s1in, *s2=s2in;
475     register volatile uint8_t accumulator=0;
476
477     while (n-- > 0) {
478         accumulator |= (*s1++ ^ *s2++);
479     }
480     accumulator |= accumulator >> 4; /* constant-time             */
481     accumulator |= accumulator >> 2; /*  boolean canonicalisation */
482     accumulator |= accumulator >> 1;
483     accumulator &= 1;
484     accumulator ^= 1;
485     return accumulator;
486 }
487
488 void util_module(dict_t *dict)
489 {
490     add_closure(dict,"sysbuffer",buffer_apply);
491 }
492
493 void update_max_start_pad(int32_t *our_module_global, int32_t our_instance)
494 {
495     if (*our_module_global < our_instance)
496         *our_module_global=our_instance;
497 }
498
499 int32_t transform_max_start_pad, comm_max_start_pad;
500
501 int32_t calculate_max_start_pad(void)
502 {
503     return
504         site_max_start_pad +
505         transform_max_start_pad +
506         comm_max_start_pad;
507 }
508
509 void vslilog_part(struct log_if *lf, int priority, const char *message, va_list ap)
510 {
511     char *buff=lf->buff;
512     size_t bp;
513     char *nlp;
514
515     bp=strlen(buff);
516     assert(bp < LOG_MESSAGE_BUFLEN);
517     vsnprintf(buff+bp,LOG_MESSAGE_BUFLEN-bp,message,ap);
518     buff[LOG_MESSAGE_BUFLEN-1] = '\n';
519     buff[LOG_MESSAGE_BUFLEN] = '\0';
520     /* Each line is sent separately */
521     while ((nlp=strchr(buff,'\n'))) {
522         *nlp=0;
523         slilog(lf,priority,"%s",buff);
524         memmove(buff,nlp+1,strlen(nlp+1)+1);
525     }
526 }
527
528 extern void slilog_part(struct log_if *lf, int priority, const char *message, ...)
529 {
530     va_list ap;
531     va_start(ap,message);
532     vslilog_part(lf,priority,message,ap);
533     va_end(ap);
534 }
535
536 void string_item_to_iaddr(const item_t *item, uint16_t port, union iaddr *ia,
537                           const char *desc)
538 {
539 #ifndef CONFIG_IPV6
540
541     ia->sin.sin_family=AF_INET;
542     ia->sin.sin_addr.s_addr=htonl(string_item_to_ipaddr(item,desc));
543     ia->sin.sin_port=htons(port);
544
545 #else /* CONFIG_IPV6 => we have adns_text2addr */
546
547     if (item->type!=t_string)
548         cfgfatal(item->loc,desc,"expecting a string IP (v4 or v6) address\n");
549     socklen_t salen=sizeof(*ia);
550     int r=adns_text2addr(item->data.string, port,
551                          adns_qf_addrlit_ipv4_quadonly,
552                          &ia->sa, &salen);
553     assert(r!=ENOSPC);
554     if (r) cfgfatal(item->loc,desc,"invalid IP (v4 or v6) address: %s\n",
555                     strerror(r));
556
557 #endif /* CONFIG_IPV6 */
558 }
559
560 #define IADDR_NBUFS 8
561
562 const char *iaddr_to_string(const union iaddr *ia)
563 {
564 #ifndef CONFIG_IPV6
565
566     SBUF_DEFINE(IADDR_NBUFS, 100);
567
568     assert(ia->sa.sa_family == AF_INET);
569
570     snprintf(SBUF, sizeof(SBUF), "[%s]:%d",
571              inet_ntoa(ia->sin.sin_addr),
572              ntohs(ia->sin.sin_port));
573
574 #else /* CONFIG_IPV6 => we have adns_addr2text */
575
576     SBUF_DEFINE(IADDR_NBUFS, 1+ADNS_ADDR2TEXT_BUFLEN+20);
577
578     int port;
579
580     char *addrbuf = SBUF;
581     *addrbuf++ = '[';
582     int addrbuflen = ADNS_ADDR2TEXT_BUFLEN;
583
584     int r = adns_addr2text(&ia->sa, 0, addrbuf, &addrbuflen, &port);
585     if (r) {
586         const char fmt[]= "scoped IPv6 addr, error: %.*s";
587         sprintf(addrbuf, fmt,
588                 (int)(ADNS_ADDR2TEXT_BUFLEN - sizeof(fmt)) /* underestimate */,
589                 strerror(r));
590     }
591
592     char *portbuf = addrbuf;
593     int addrl = strlen(addrbuf);
594     portbuf += addrl;
595
596     snprintf(portbuf, sizeof(SBUF)-addrl, "]:%d", port);
597
598 #endif /* CONFIG_IPV6 */
599
600     return SBUF;
601 }
602
603 bool_t iaddr_equal(const union iaddr *ia, const union iaddr *ib,
604                    bool_t ignoreport)
605 {
606     if (ia->sa.sa_family != ib->sa.sa_family)
607         return 0;
608     switch (ia->sa.sa_family) {
609     case AF_INET:
610         return ia->sin.sin_addr.s_addr == ib->sin.sin_addr.s_addr
611            && (ignoreport ||
612                ia->sin.sin_port        == ib->sin.sin_port);
613 #ifdef CONFIG_IPV6
614     case AF_INET6:
615         return !memcmp(&ia->sin6.sin6_addr, &ib->sin6.sin6_addr, 16)
616            &&  ia->sin6.sin6_scope_id  == ib->sin6.sin6_scope_id
617            && (ignoreport ||
618                ia->sin6.sin6_port      == ib->sin6.sin6_port)
619             /* we ignore the flowinfo field */;
620 #endif /* CONFIG_IPV6 */
621     default:
622         abort();
623     }
624 }
625
626 int iaddr_socklen(const union iaddr *ia)
627 {
628     switch (ia->sa.sa_family) {
629     case AF_INET:  return sizeof(ia->sin);
630 #ifdef CONFIG_IPV6
631     case AF_INET6: return sizeof(ia->sin6);
632 #endif /* CONFIG_IPV6 */
633     default:       abort();
634     }
635 }
636
637 const char *pollbadbit(int revents)
638 {
639 #define BADBIT(b) \
640     if ((revents & b)) return #b
641     BADBIT(POLLERR);
642     BADBIT(POLLHUP);
643     /* POLLNVAL is handled by the event loop - see afterpoll_fn comment */
644 #undef BADBIT
645     return 0;
646 }
647
648 enum async_linebuf_result
649 async_linebuf_read(struct pollfd *pfd, struct buffer_if *buf,
650                    const char **emsg_out)
651 {
652     int revents=pfd->revents;
653
654 #define BAD(m) do{ *emsg_out=(m); return async_linebuf_broken; }while(0)
655
656     const char *badbit=pollbadbit(revents);
657     if (badbit) BAD(badbit);
658
659     if (!(revents & POLLIN))
660         return async_linebuf_nothing;
661
662     /*
663      * Data structure: A line which has been returned to the user is
664      * stored in buf at base before start.  But we retain the usual
665      * buffer meaning of size.  So:
666      *
667      *   | returned :    | input read,   |    unused    |
668      *   |  to user : \0 |  awaiting     |     buffer   |
669      *   |          :    |  processing   |      space   |
670      *   |          :    |               |              |
671      *   ^base           ^start          ^start+size    ^base+alloclen
672      */
673
674     BUF_ASSERT_USED(buf);
675
676     /* firstly, eat any previous */
677     if (buf->start != buf->base) {
678         memmove(buf->base,buf->start,buf->size);
679         buf->start=buf->base;
680     }
681
682     uint8_t *searched=buf->base;
683
684     /*
685      * During the workings here we do not use start.  We set start
686      * when we return some actual data.  So we have this:
687      *
688      *   | searched     | read, might   |  unused      |
689      *   |  for \n      |  contain \n   |   buffer     |
690      *   |  none found  |  but not \0   |    space     |
691      *   |              |               |              |
692      *   ^base          ^searched       ^base+size     ^base+alloclen
693      *  [^start]                        ^dataend
694      *
695      */
696     for (;;) {
697         uint8_t *dataend=buf->base+buf->size;
698         char *newline=memchr(searched,'\n',dataend-searched);
699         if (newline) {
700             *newline=0;
701             buf->start=newline+1;
702             buf->size=dataend-buf->start;
703             return async_linebuf_ok;
704         }
705         searched=dataend;
706         ssize_t space=(buf->base+buf->alloclen)-dataend;
707         if (!space) BAD("input line too long");
708         ssize_t r=read(pfd->fd,searched,space);
709         if (r==0) {
710             *searched=0;
711             *emsg_out=buf->size?"no newline at eof":0;
712             buf->start=searched+1;
713             buf->size=0;
714             return async_linebuf_eof;
715         }
716         if (r<0) {
717             if (errno==EINTR)
718                 continue;
719             if (iswouldblock(errno))
720                 return async_linebuf_nothing;
721             BAD(strerror(errno));
722         }
723         assert(r<=space);
724         if (memchr(searched,0,r)) BAD("nul in input data");
725         buf->size+=r;
726     }
727
728 #undef BAD
729 }