arp: fix -H/-t handling.
[oweals/busybox.git] / networking / udhcp / files.c
index 20761a585aacc716ddd3727cf42e2003bfd01382..6840f3c259a93fa8de318acf76b9a74bc554dbbc 100644 (file)
-/* 
- * files.c -- DHCP server file manipulation *
+/* vi: set sw=4 ts=4: */
+/*
+ * DHCP server config and lease file manipulation
+ *
  * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
+ *
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
-#include <sys/socket.h>
-#include <arpa/inet.h>
-#include <string.h>
-#include <stdlib.h>
-#include <time.h>
-#include <ctype.h>
+#include <netinet/ether.h>
 
-#include "dhcpd.h"
-#include "files.h"
-#include "options.h"
 #include "common.h"
+#include "dhcpd.h"
 
-/* on these functions, make sure you datatype matches */
-static int read_ip(const char *line, void *arg)
-{
-       struct in_addr *addr = arg;
-       struct hostent *host;
-       int retval = 1;
-
-       if (!inet_aton(line, addr)) {
-               if ((host = gethostbyname(line))) 
-                       addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
-               else retval = 0;
-       }
-       return retval;
-}
-
-
-static int read_str(const char *line, void *arg)
+/* on these functions, make sure your datatype matches */
+static int FAST_FUNC read_str(const char *line, void *arg)
 {
        char **dest = arg;
-       
-       if (*dest) free(*dest);
-       *dest = strdup(line);
-       
+
+       free(*dest);
+       *dest = xstrdup(line);
        return 1;
 }
 
-
-static int read_u32(const char *line, void *arg)
+static int FAST_FUNC read_u32(const char *line, void *arg)
 {
-       u_int32_t *dest = arg;
-       char *endptr;
-       *dest = strtoul(line, &endptr, 0);
-       return endptr[0] == '\0';
+       *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
+       return errno == 0;
 }
 
-
-static int read_yn(const char *line, void *arg)
+static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
 {
-       char *dest = arg;
-       int retval = 1;
-
-       if (!strcasecmp("yes", line))
-               *dest = 1;
-       else if (!strcasecmp("no", line))
-               *dest = 0;
-       else retval = 0;
-       
-       return retval;
+       char *line;
+       char *mac_string;
+       char *ip_string;
+       struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */
+       uint32_t nip;
+
+       /* Read mac */
+       line = (char *) const_line;
+       mac_string = strtok_r(line, " \t", &line);
+       if (!mac_string || !ether_aton_r(mac_string, &mac_bytes))
+               return 0;
+
+       /* Read ip */
+       ip_string = strtok_r(NULL, " \t", &line);
+       if (!ip_string || !udhcp_str2nip(ip_string, &nip))
+               return 0;
+
+       add_static_lease(arg, (uint8_t*) &mac_bytes, nip);
+
+       log_static_leases(arg);
+
+       return 1;
 }
 
-#define READ_CONFIG_BUF_SIZE 512        /* domainname may have 254 chars */
 
-/* read a dhcp option and add it to opt_list */
-static int read_opt(const char *const_line, void *arg)
+struct config_keyword {
+       const char *keyword;
+       int (*handler)(const char *line, void *var) FAST_FUNC;
+       void *var;
+       const char *def;
+};
+
+static const struct config_keyword keywords[] = {
+       /* keyword        handler           variable address               default */
+       {"start"        , udhcp_str2nip   , &server_config.start_ip     , "192.168.0.20"},
+       {"end"          , udhcp_str2nip   , &server_config.end_ip       , "192.168.0.254"},
+       {"interface"    , read_str        , &server_config.interface    , "eth0"},
+       /* Avoid "max_leases value not sane" warning by setting default
+        * to default_end_ip - default_start_ip + 1: */
+       {"max_leases"   , read_u32        , &server_config.max_leases   , "235"},
+       {"auto_time"    , read_u32        , &server_config.auto_time    , "7200"},
+       {"decline_time" , read_u32        , &server_config.decline_time , "3600"},
+       {"conflict_time", read_u32        , &server_config.conflict_time, "3600"},
+       {"offer_time"   , read_u32        , &server_config.offer_time   , "60"},
+       {"min_lease"    , read_u32        , &server_config.min_lease_sec, "60"},
+       {"lease_file"   , read_str        , &server_config.lease_file   , LEASES_FILE},
+       {"pidfile"      , read_str        , &server_config.pidfile      , "/var/run/udhcpd.pid"},
+       {"siaddr"       , udhcp_str2nip   , &server_config.siaddr_nip   , "0.0.0.0"},
+       /* keywords with no defaults must be last! */
+       {"option"       , udhcp_str2optset, &server_config.options      , ""},
+       {"opt"          , udhcp_str2optset, &server_config.options      , ""},
+       {"notify_file"  , read_str        , &server_config.notify_file  , NULL},
+       {"sname"        , read_str        , &server_config.sname        , NULL},
+       {"boot_file"    , read_str        , &server_config.boot_file    , NULL},
+       {"static_lease" , read_staticlease, &server_config.static_leases, ""},
+};
+enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
+
+void FAST_FUNC read_config(const char *file)
 {
-    char line[READ_CONFIG_BUF_SIZE];
-       struct option_set **opt_list = arg;
-       char *opt, *val, *endptr;
-    struct dhcp_option *option;
-    int retval = 0, length;
-    char buffer[256];                       /* max opt length */
-       u_int16_t result_u16;
-       u_int32_t result_u32;
-    void *valptr;
-       
-    if ((opt = strtok(strcpy(line, const_line), " \t="))) {
-               
-       for (option = dhcp_options; option->code; option++)
-               if (!strcasecmp(option->name, opt))
-                       break;
-       
-       if (option->code) do {
-               val = strtok(NULL, ", \t");
-               if(!val)
-                       break;
-                       length = option_lengths[option->flags & TYPE_MASK];
-               valptr = NULL;
-                       switch (option->flags & TYPE_MASK) {
-                       case OPTION_IP:
-                               retval = read_ip(val, buffer);
-                               break;
-                       case OPTION_IP_PAIR:
-                               retval = read_ip(val, buffer);
-                               if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
-                               if (retval) retval = read_ip(val, buffer + 4);
-                               break;
-                       case OPTION_STRING:
-                               length = strlen(val);
-                               if (length > 0) {
-                                       if (length > 254) length = 254;
-                               endptr = buffer + length;
-                               endptr[0] = 0;
-                               valptr = val;
+       parser_t *parser;
+       const struct config_keyword *k;
+       unsigned i;
+       char *token[2];
+
+       for (i = 0; i < KWS_WITH_DEFAULTS; i++)
+               keywords[i].handler(keywords[i].def, keywords[i].var);
+
+       parser = config_open(file);
+       while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
+               for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
+                       if (strcasecmp(token[0], k->keyword) == 0) {
+                               if (!k->handler(token[1], k->var)) {
+                                       bb_error_msg("can't parse line %u in %s",
+                                                       parser->lineno, file);
+                                       /* reset back to the default value */
+                                       k->handler(k->def, k->var);
                                }
                                break;
-                       case OPTION_BOOLEAN:
-                               retval = read_yn(val, buffer);
-                               break;
-                       case OPTION_U8:
-                               buffer[0] = strtoul(val, &endptr, 0);
-                       valptr = buffer;
-                               break;
-                       case OPTION_U16:
-                               result_u16 = htons(strtoul(val, &endptr, 0));
-                       valptr = &result_u16;
-                               break;
-                       case OPTION_S16:
-                               result_u16 = htons(strtol(val, &endptr, 0));
-                       valptr = &result_u16;
-                               break;
-                       case OPTION_U32:
-                               result_u32 = htonl(strtoul(val, &endptr, 0));
-                       valptr = &result_u32;
-                               break;
-                       case OPTION_S32:
-                               result_u32 = htonl(strtol(val, &endptr, 0));    
-                       valptr = &result_u32;
-                               break;
-                       default:
-                       retval = 0;
-                               break;
                        }
-               if(valptr) {
-                       memcpy(buffer, valptr, length);
-                       retval = (endptr[0] == '\0');
                }
-                       if (retval) 
-                               attach_option(opt_list, option, buffer, length);
-               else
-                       break;
-       } while (option->flags & OPTION_LIST);
-    }
-       return retval;
+       }
+       config_close(parser);
+
+       server_config.start_ip = ntohl(server_config.start_ip);
+       server_config.end_ip = ntohl(server_config.end_ip);
 }
 
+void FAST_FUNC write_leases(void)
+{
+       int fd;
+       unsigned i;
+       leasetime_t curr;
+       int64_t written_at;
 
-static const struct config_keyword keywords[] = {
-       /* keyword      handler   variable address              default     */
-       {"start",       read_ip,  &(server_config.start),       "192.168.0.20"},
-       {"end",         read_ip,  &(server_config.end),         "192.168.0.254"},
-       {"interface",   read_str, &(server_config.interface),   "eth0"},
-       {"option",      read_opt, &(server_config.options),     ""},
-       {"opt",         read_opt, &(server_config.options),     ""},
-       {"max_leases",  read_u32, &(server_config.max_leases),  "254"},
-       {"remaining",   read_yn,  &(server_config.remaining),   "yes"},
-       {"auto_time",   read_u32, &(server_config.auto_time),   "7200"},
-       {"decline_time",read_u32, &(server_config.decline_time),"3600"},
-       {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
-       {"offer_time",  read_u32, &(server_config.offer_time),  "60"},
-       {"min_lease",   read_u32, &(server_config.min_lease),   "60"},
-       {"lease_file",  read_str, &(server_config.lease_file),  leases_file},
-       {"pidfile",     read_str, &(server_config.pidfile),     "/var/run/udhcpd.pid"},
-       {"notify_file", read_str, &(server_config.notify_file), ""},
-       {"siaddr",      read_ip,  &(server_config.siaddr),      "0.0.0.0"},
-       {"sname",       read_str, &(server_config.sname),       ""},
-       {"boot_file",   read_str, &(server_config.boot_file),   ""},
-       /*ADDME: static lease */
-       {"",            NULL,     NULL,                         ""}
-};
+       fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
+       if (fd < 0)
+               return;
 
+       curr = written_at = time(NULL);
 
-int read_config(const char *file)
-{
-       FILE *in;
-       char buffer[READ_CONFIG_BUF_SIZE], orig[READ_CONFIG_BUF_SIZE];
-       char *token, *line;
-       int i;
+       written_at = SWAP_BE64(written_at);
+       full_write(fd, &written_at, sizeof(written_at));
 
-       for (i = 0; keywords[i].keyword[0]; i++)
-               if (keywords[i].def[0])
-                       keywords[i].handler(keywords[i].def, keywords[i].var);
+       for (i = 0; i < server_config.max_leases; i++) {
+               leasetime_t tmp_time;
 
-       if (!(in = fopen(file, "r"))) {
-               LOG(LOG_ERR, "unable to open config file: %s", file);
-               return 0;
-       }
-       
-       while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
-               if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
-               strcpy(orig, buffer);
-               if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
-               token = strtok(buffer, " \t");
-               if(!token)
-                       continue;
-               line = strtok(NULL, "");
-               if(!line)
+               if (g_leases[i].lease_nip == 0)
                        continue;
-               while(*line == '=' || isspace(*line))
-               line++;
-               /* eat trailing whitespace */
-               for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
-               line[i] = '\0';
-               if (*line == '\0')
-                       continue;
-               
-               for (i = 0; keywords[i].keyword[0]; i++)
-                       if (!strcasecmp(token, keywords[i].keyword))
-                               if (!keywords[i].handler(line, keywords[i].var)) {
-                                       LOG(LOG_ERR, "unable to parse '%s'", orig);
-                                       /* reset back to the default value */
-                                       keywords[i].handler(keywords[i].def, keywords[i].var);
-                               }
-       }
-       fclose(in);
-       return 1;
-}
 
+               /* Screw with the time in the struct, for easier writing */
+               tmp_time = g_leases[i].expires;
 
-void write_leases(void)
-{
-       FILE *fp;
-       unsigned int i;
-       char buf[255];
-       time_t curr = time(0);
-       unsigned long lease_time;
-       
-       if (!(fp = fopen(server_config.lease_file, "w"))) {
-               LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file);
-               return;
-       }
-       
-       for (i = 0; i < server_config.max_leases; i++) {
-               struct dhcpOfferedAddr lease;
-               if (leases[i].yiaddr != 0) {
-                       if (server_config.remaining) {
-                               if (lease_expired(&(leases[i])))
-                                       lease_time = 0;
-                               else lease_time = leases[i].expires - curr;
-                       } else lease_time = leases[i].expires;
-                       lease.expires = htonl(lease_time);
-                       memcpy(lease.chaddr, leases[i].chaddr, 16);
-                       lease.yiaddr = leases[i].yiaddr;
-                       fwrite(leases, sizeof(lease), 1, fp);
-               }
+               g_leases[i].expires -= curr;
+               if ((signed_leasetime_t) g_leases[i].expires < 0)
+                       g_leases[i].expires = 0;
+               g_leases[i].expires = htonl(g_leases[i].expires);
+
+               /* No error check. If the file gets truncated,
+                * we lose some leases on restart. Oh well. */
+               full_write(fd, &g_leases[i], sizeof(g_leases[i]));
+
+               /* Then restore it when done */
+               g_leases[i].expires = tmp_time;
        }
-       fclose(fp);
-       
+       close(fd);
+
        if (server_config.notify_file) {
-               sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
-               system(buf);
+               char *argv[3];
+               argv[0] = server_config.notify_file;
+               argv[1] = server_config.lease_file;
+               argv[2] = NULL;
+               spawn_and_wait(argv);
        }
 }
 
-
-void read_leases(const char *file)
+void FAST_FUNC read_leases(const char *file)
 {
-       FILE *fp;
-       unsigned int i = 0;
-       struct dhcpOfferedAddr lease;
-       
-       if (!(fp = fopen(file, "r"))) {
-               LOG(LOG_ERR, "Unable to open %s for reading", file);
+       struct dyn_lease lease;
+       int64_t written_at, time_passed;
+       int fd;
+#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
+       unsigned i = 0;
+#endif
+
+       fd = open_or_warn(file, O_RDONLY);
+       if (fd < 0)
                return;
-       }
-       
-       while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
-               /* ADDME: is it a static lease */
-               if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
-                       lease.expires = ntohl(lease.expires);
-                       if (!server_config.remaining) lease.expires -= time(0);
-                       if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
-                               LOG(LOG_WARNING, "Too many leases while loading %s\n", file);
+
+       if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
+               goto ret;
+       written_at = SWAP_BE64(written_at);
+
+       time_passed = time(NULL) - written_at;
+       /* Strange written_at, or lease file from old version of udhcpd
+        * which had no "written_at" field? */
+       if ((uint64_t)time_passed > 12 * 60 * 60)
+               goto ret;
+
+       while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
+//FIXME: what if it matches some static lease?
+               uint32_t y = ntohl(lease.lease_nip);
+               if (y >= server_config.start_ip && y <= server_config.end_ip) {
+                       signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
+                       if (expires <= 0)
+                               continue;
+                       /* NB: add_lease takes "relative time", IOW,
+                        * lease duration, not lease deadline. */
+                       if (add_lease(lease.lease_mac, lease.lease_nip,
+                                       expires,
+                                       lease.hostname, sizeof(lease.hostname)
+                               ) == 0
+                       ) {
+                               bb_error_msg("too many leases while loading %s", file);
                                break;
-                       }                               
+                       }
+#if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
                        i++;
+#endif
                }
        }
-       DEBUG(LOG_INFO, "Read %d leases", i);
-       fclose(fp);
+       log1("Read %d leases", i);
+ ret:
+       close(fd);
 }