files.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * DHCP server config and lease file manipulation
  4. *
  5. * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
  6. *
  7. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  8. */
  9. #include <netinet/ether.h>
  10. #include "common.h"
  11. #include "dhcpd.h"
  12. #if BB_LITTLE_ENDIAN
  13. static inline uint64_t hton64(uint64_t v)
  14. {
  15. return (((uint64_t)htonl(v)) << 32) | htonl(v >> 32);
  16. }
  17. #else
  18. #define hton64(v) (v)
  19. #endif
  20. #define ntoh64(v) hton64(v)
  21. /* on these functions, make sure your datatype matches */
  22. static int FAST_FUNC read_str(const char *line, void *arg)
  23. {
  24. char **dest = arg;
  25. free(*dest);
  26. *dest = xstrdup(line);
  27. return 1;
  28. }
  29. static int FAST_FUNC read_u32(const char *line, void *arg)
  30. {
  31. *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
  32. return errno == 0;
  33. }
  34. static int FAST_FUNC read_staticlease(const char *const_line, void *arg)
  35. {
  36. char *line;
  37. char *mac_string;
  38. char *ip_string;
  39. struct ether_addr mac_bytes; /* it's "struct { uint8_t mac[6]; }" */
  40. uint32_t nip;
  41. /* Read mac */
  42. line = (char *) const_line;
  43. mac_string = strtok_r(line, " \t", &line);
  44. if (!mac_string || !ether_aton_r(mac_string, &mac_bytes))
  45. return 0;
  46. /* Read ip */
  47. ip_string = strtok_r(NULL, " \t", &line);
  48. if (!ip_string || !udhcp_str2nip(ip_string, &nip))
  49. return 0;
  50. add_static_lease(arg, (uint8_t*) &mac_bytes, nip);
  51. log_static_leases(arg);
  52. return 1;
  53. }
  54. struct config_keyword {
  55. const char *keyword;
  56. int (*handler)(const char *line, void *var) FAST_FUNC;
  57. void *var;
  58. const char *def;
  59. };
  60. static const struct config_keyword keywords[] = {
  61. /* keyword handler variable address default */
  62. {"start" , udhcp_str2nip , &server_config.start_ip , "192.168.0.20"},
  63. {"end" , udhcp_str2nip , &server_config.end_ip , "192.168.0.254"},
  64. {"interface" , read_str , &server_config.interface , "eth0"},
  65. /* Avoid "max_leases value not sane" warning by setting default
  66. * to default_end_ip - default_start_ip + 1: */
  67. {"max_leases" , read_u32 , &server_config.max_leases , "235"},
  68. {"auto_time" , read_u32 , &server_config.auto_time , "7200"},
  69. {"decline_time" , read_u32 , &server_config.decline_time , "3600"},
  70. {"conflict_time", read_u32 , &server_config.conflict_time, "3600"},
  71. {"offer_time" , read_u32 , &server_config.offer_time , "60"},
  72. {"min_lease" , read_u32 , &server_config.min_lease_sec, "60"},
  73. {"lease_file" , read_str , &server_config.lease_file , LEASES_FILE},
  74. {"pidfile" , read_str , &server_config.pidfile , "/var/run/udhcpd.pid"},
  75. {"siaddr" , udhcp_str2nip , &server_config.siaddr_nip , "0.0.0.0"},
  76. /* keywords with no defaults must be last! */
  77. {"option" , udhcp_str2optset, &server_config.options , ""},
  78. {"opt" , udhcp_str2optset, &server_config.options , ""},
  79. {"notify_file" , read_str , &server_config.notify_file , ""},
  80. {"sname" , read_str , &server_config.sname , ""},
  81. {"boot_file" , read_str , &server_config.boot_file , ""},
  82. {"static_lease" , read_staticlease, &server_config.static_leases, ""},
  83. };
  84. enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
  85. void FAST_FUNC read_config(const char *file)
  86. {
  87. parser_t *parser;
  88. const struct config_keyword *k;
  89. unsigned i;
  90. char *token[2];
  91. for (i = 0; i < KWS_WITH_DEFAULTS; i++)
  92. keywords[i].handler(keywords[i].def, keywords[i].var);
  93. parser = config_open(file);
  94. while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
  95. for (k = keywords, i = 0; i < ARRAY_SIZE(keywords); k++, i++) {
  96. if (strcasecmp(token[0], k->keyword) == 0) {
  97. if (!k->handler(token[1], k->var)) {
  98. bb_error_msg("can't parse line %u in %s",
  99. parser->lineno, file);
  100. /* reset back to the default value */
  101. k->handler(k->def, k->var);
  102. }
  103. break;
  104. }
  105. }
  106. }
  107. config_close(parser);
  108. server_config.start_ip = ntohl(server_config.start_ip);
  109. server_config.end_ip = ntohl(server_config.end_ip);
  110. }
  111. void FAST_FUNC write_leases(void)
  112. {
  113. int fd;
  114. unsigned i;
  115. leasetime_t curr;
  116. int64_t written_at;
  117. fd = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
  118. if (fd < 0)
  119. return;
  120. curr = written_at = time(NULL);
  121. written_at = hton64(written_at);
  122. full_write(fd, &written_at, sizeof(written_at));
  123. for (i = 0; i < server_config.max_leases; i++) {
  124. leasetime_t tmp_time;
  125. if (g_leases[i].lease_nip == 0)
  126. continue;
  127. /* Screw with the time in the struct, for easier writing */
  128. tmp_time = g_leases[i].expires;
  129. g_leases[i].expires -= curr;
  130. if ((signed_leasetime_t) g_leases[i].expires < 0)
  131. g_leases[i].expires = 0;
  132. g_leases[i].expires = htonl(g_leases[i].expires);
  133. /* No error check. If the file gets truncated,
  134. * we lose some leases on restart. Oh well. */
  135. full_write(fd, &g_leases[i], sizeof(g_leases[i]));
  136. /* Then restore it when done */
  137. g_leases[i].expires = tmp_time;
  138. }
  139. close(fd);
  140. if (server_config.notify_file) {
  141. char *argv[3];
  142. argv[0] = server_config.notify_file;
  143. argv[1] = server_config.lease_file;
  144. argv[2] = NULL;
  145. spawn_and_wait(argv);
  146. }
  147. }
  148. void FAST_FUNC read_leases(const char *file)
  149. {
  150. struct dyn_lease lease;
  151. int64_t written_at, time_passed;
  152. int fd;
  153. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
  154. unsigned i = 0;
  155. #endif
  156. fd = open_or_warn(file, O_RDONLY);
  157. if (fd < 0)
  158. return;
  159. if (full_read(fd, &written_at, sizeof(written_at)) != sizeof(written_at))
  160. goto ret;
  161. written_at = ntoh64(written_at);
  162. time_passed = time(NULL) - written_at;
  163. /* Strange written_at, or lease file from old version of udhcpd
  164. * which had no "written_at" field? */
  165. if ((uint64_t)time_passed > 12 * 60 * 60)
  166. goto ret;
  167. while (full_read(fd, &lease, sizeof(lease)) == sizeof(lease)) {
  168. //FIXME: what if it matches some static lease?
  169. uint32_t y = ntohl(lease.lease_nip);
  170. if (y >= server_config.start_ip && y <= server_config.end_ip) {
  171. signed_leasetime_t expires = ntohl(lease.expires) - (signed_leasetime_t)time_passed;
  172. if (expires <= 0)
  173. continue;
  174. /* NB: add_lease takes "relative time", IOW,
  175. * lease duration, not lease deadline. */
  176. if (add_lease(lease.lease_mac, lease.lease_nip,
  177. expires,
  178. lease.hostname, sizeof(lease.hostname)
  179. ) == 0
  180. ) {
  181. bb_error_msg("too many leases while loading %s", file);
  182. break;
  183. }
  184. #if defined CONFIG_UDHCP_DEBUG && CONFIG_UDHCP_DEBUG >= 1
  185. i++;
  186. #endif
  187. }
  188. }
  189. log1("Read %d leases", i);
  190. ret:
  191. close(fd);
  192. }