files.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * files.c -- DHCP server file manipulation *
  4. * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
  5. */
  6. #include <netinet/ether.h>
  7. #include "common.h"
  8. #include "dhcpd.h"
  9. #include "options.h"
  10. /* on these functions, make sure your datatype matches */
  11. static int read_ip(const char *line, void *arg)
  12. {
  13. len_and_sockaddr *lsa;
  14. lsa = host_and_af2sockaddr(line, 0, AF_INET);
  15. if (!lsa)
  16. return 0;
  17. *(uint32_t*)arg = lsa->u.sin.sin_addr.s_addr;
  18. free(lsa);
  19. return 1;
  20. }
  21. static int read_mac(const char *line, void *arg)
  22. {
  23. uint8_t *mac_bytes = arg;
  24. struct ether_addr *temp_ether_addr;
  25. temp_ether_addr = ether_aton(line);
  26. if (temp_ether_addr == NULL)
  27. return 0;
  28. memcpy(mac_bytes, temp_ether_addr, 6);
  29. return 1;
  30. }
  31. static int read_str(const char *line, void *arg)
  32. {
  33. char **dest = arg;
  34. free(*dest);
  35. *dest = xstrdup(line);
  36. return 1;
  37. }
  38. static int read_u32(const char *line, void *arg)
  39. {
  40. *(uint32_t*)arg = bb_strtou32(line, NULL, 10);
  41. return errno == 0;
  42. }
  43. static int read_yn(const char *line, void *arg)
  44. {
  45. char *dest = arg;
  46. if (!strcasecmp("yes", line)) {
  47. *dest = 1;
  48. return 1;
  49. }
  50. if (!strcasecmp("no", line)) {
  51. *dest = 0;
  52. return 1;
  53. }
  54. return 0;
  55. }
  56. /* find option 'code' in opt_list */
  57. struct option_set *find_option(struct option_set *opt_list, uint8_t code)
  58. {
  59. while (opt_list && opt_list->data[OPT_CODE] < code)
  60. opt_list = opt_list->next;
  61. if (opt_list && opt_list->data[OPT_CODE] == code)
  62. return opt_list;
  63. return NULL;
  64. }
  65. /* add an option to the opt_list */
  66. static void attach_option(struct option_set **opt_list,
  67. const struct dhcp_option *option, char *buffer, int length)
  68. {
  69. struct option_set *existing, *new, **curr;
  70. existing = find_option(*opt_list, option->code);
  71. if (!existing) {
  72. DEBUG("Attaching option %02x to list", option->code);
  73. #if ENABLE_FEATURE_RFC3397
  74. if ((option->flags & TYPE_MASK) == OPTION_STR1035)
  75. /* reuse buffer and length for RFC1035-formatted string */
  76. buffer = dname_enc(NULL, 0, buffer, &length);
  77. #endif
  78. /* make a new option */
  79. new = xmalloc(sizeof(*new));
  80. new->data = xmalloc(length + 2);
  81. new->data[OPT_CODE] = option->code;
  82. new->data[OPT_LEN] = length;
  83. memcpy(new->data + 2, buffer, length);
  84. curr = opt_list;
  85. while (*curr && (*curr)->data[OPT_CODE] < option->code)
  86. curr = &(*curr)->next;
  87. new->next = *curr;
  88. *curr = new;
  89. #if ENABLE_FEATURE_RFC3397
  90. if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
  91. free(buffer);
  92. #endif
  93. return;
  94. }
  95. /* add it to an existing option */
  96. DEBUG("Attaching option %02x to existing member of list", option->code);
  97. if (option->flags & OPTION_LIST) {
  98. #if ENABLE_FEATURE_RFC3397
  99. if ((option->flags & TYPE_MASK) == OPTION_STR1035)
  100. /* reuse buffer and length for RFC1035-formatted string */
  101. buffer = dname_enc(existing->data + 2,
  102. existing->data[OPT_LEN], buffer, &length);
  103. #endif
  104. if (existing->data[OPT_LEN] + length <= 255) {
  105. existing->data = xrealloc(existing->data,
  106. existing->data[OPT_LEN] + length + 3);
  107. if ((option->flags & TYPE_MASK) == OPTION_STRING) {
  108. /* ' ' can bring us to 256 - bad */
  109. if (existing->data[OPT_LEN] + length >= 255)
  110. return;
  111. /* add space separator between STRING options in a list */
  112. existing->data[existing->data[OPT_LEN] + 2] = ' ';
  113. existing->data[OPT_LEN]++;
  114. }
  115. memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
  116. existing->data[OPT_LEN] += length;
  117. } /* else, ignore the data, we could put this in a second option in the future */
  118. #if ENABLE_FEATURE_RFC3397
  119. if ((option->flags & TYPE_MASK) == OPTION_STR1035 && buffer != NULL)
  120. free(buffer);
  121. #endif
  122. } /* else, ignore the new data */
  123. }
  124. /* read a dhcp option and add it to opt_list */
  125. static int read_opt(const char *const_line, void *arg)
  126. {
  127. struct option_set **opt_list = arg;
  128. char *opt, *val, *endptr;
  129. char *line;
  130. const struct dhcp_option *option;
  131. int retval, length, idx;
  132. char buffer[8] __attribute__((aligned(4)));
  133. uint16_t *result_u16 = (uint16_t *) buffer;
  134. uint32_t *result_u32 = (uint32_t *) buffer;
  135. /* Cheat, the only const line we'll actually get is "" */
  136. line = (char *) const_line;
  137. opt = strtok(line, " \t=");
  138. if (!opt)
  139. return 0;
  140. idx = index_in_strings(dhcp_option_strings, opt); /* NB: was strcasecmp! */
  141. if (idx < 0)
  142. return 0;
  143. option = &dhcp_options[idx];
  144. retval = 0;
  145. do {
  146. val = strtok(NULL, ", \t");
  147. if (!val) break;
  148. length = dhcp_option_lengths[option->flags & TYPE_MASK];
  149. retval = 0;
  150. opt = buffer; /* new meaning for variable opt */
  151. switch (option->flags & TYPE_MASK) {
  152. case OPTION_IP:
  153. retval = read_ip(val, buffer);
  154. break;
  155. case OPTION_IP_PAIR:
  156. retval = read_ip(val, buffer);
  157. val = strtok(NULL, ", \t/-");
  158. if (!val)
  159. retval = 0;
  160. if (retval)
  161. retval = read_ip(val, buffer + 4);
  162. break;
  163. case OPTION_STRING:
  164. #if ENABLE_FEATURE_RFC3397
  165. case OPTION_STR1035:
  166. #endif
  167. length = strlen(val);
  168. if (length > 0) {
  169. if (length > 254) length = 254;
  170. opt = val;
  171. retval = 1;
  172. }
  173. break;
  174. case OPTION_BOOLEAN:
  175. retval = read_yn(val, buffer);
  176. break;
  177. case OPTION_U8:
  178. buffer[0] = strtoul(val, &endptr, 0);
  179. retval = (endptr[0] == '\0');
  180. break;
  181. /* htonX are macros in older libc's, using temp var
  182. * in code below for safety */
  183. /* TODO: use bb_strtoX? */
  184. case OPTION_U16: {
  185. unsigned long tmp = strtoul(val, &endptr, 0);
  186. *result_u16 = htons(tmp);
  187. retval = (endptr[0] == '\0' /*&& tmp < 0x10000*/);
  188. break;
  189. }
  190. case OPTION_S16: {
  191. long tmp = strtol(val, &endptr, 0);
  192. *result_u16 = htons(tmp);
  193. retval = (endptr[0] == '\0');
  194. break;
  195. }
  196. case OPTION_U32: {
  197. unsigned long tmp = strtoul(val, &endptr, 0);
  198. *result_u32 = htonl(tmp);
  199. retval = (endptr[0] == '\0');
  200. break;
  201. }
  202. case OPTION_S32: {
  203. long tmp = strtol(val, &endptr, 0);
  204. *result_u32 = htonl(tmp);
  205. retval = (endptr[0] == '\0');
  206. break;
  207. }
  208. default:
  209. break;
  210. }
  211. if (retval)
  212. attach_option(opt_list, option, opt, length);
  213. } while (retval && option->flags & OPTION_LIST);
  214. return retval;
  215. }
  216. static int read_staticlease(const char *const_line, void *arg)
  217. {
  218. char *line;
  219. char *mac_string;
  220. char *ip_string;
  221. uint8_t *mac_bytes;
  222. uint32_t *ip;
  223. /* Allocate memory for addresses */
  224. mac_bytes = xmalloc(sizeof(unsigned char) * 8);
  225. ip = xmalloc(sizeof(uint32_t));
  226. /* Read mac */
  227. line = (char *) const_line;
  228. mac_string = strtok(line, " \t");
  229. read_mac(mac_string, mac_bytes);
  230. /* Read ip */
  231. ip_string = strtok(NULL, " \t");
  232. read_ip(ip_string, ip);
  233. addStaticLease(arg, mac_bytes, ip);
  234. if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg);
  235. return 1;
  236. }
  237. struct config_keyword {
  238. const char *keyword;
  239. int (*handler)(const char *line, void *var);
  240. void *var;
  241. const char *def;
  242. };
  243. static const struct config_keyword keywords[] = {
  244. /* keyword handler variable address default */
  245. {"start", read_ip, &(server_config.start_ip), "192.168.0.20"},
  246. {"end", read_ip, &(server_config.end_ip), "192.168.0.254"},
  247. {"interface", read_str, &(server_config.interface), "eth0"},
  248. /* Avoid "max_leases value not sane" warning by setting default
  249. * to default_end_ip - default_start_ip + 1: */
  250. {"max_leases", read_u32, &(server_config.max_leases), "235"},
  251. {"remaining", read_yn, &(server_config.remaining), "yes"},
  252. {"auto_time", read_u32, &(server_config.auto_time), "7200"},
  253. {"decline_time", read_u32, &(server_config.decline_time), "3600"},
  254. {"conflict_time",read_u32, &(server_config.conflict_time),"3600"},
  255. {"offer_time", read_u32, &(server_config.offer_time), "60"},
  256. {"min_lease", read_u32, &(server_config.min_lease), "60"},
  257. {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
  258. {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
  259. {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
  260. /* keywords with no defaults must be last! */
  261. {"option", read_opt, &(server_config.options), ""},
  262. {"opt", read_opt, &(server_config.options), ""},
  263. {"notify_file", read_str, &(server_config.notify_file), ""},
  264. {"sname", read_str, &(server_config.sname), ""},
  265. {"boot_file", read_str, &(server_config.boot_file), ""},
  266. {"static_lease", read_staticlease, &(server_config.static_leases), ""},
  267. /* ADDME: static lease */
  268. };
  269. enum { KWS_WITH_DEFAULTS = ARRAY_SIZE(keywords) - 6 };
  270. /*
  271. * Domain names may have 254 chars, and string options can be 254
  272. * chars long. However, 80 bytes will be enough for most, and won't
  273. * hog up memory. If you have a special application, change it
  274. */
  275. #define READ_CONFIG_BUF_SIZE 80
  276. void read_config(const char *file)
  277. {
  278. FILE *in;
  279. char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
  280. int i, lineno;
  281. for (i = 0; i < KWS_WITH_DEFAULTS; i++)
  282. keywords[i].handler(keywords[i].def, keywords[i].var);
  283. in = fopen_or_warn(file, "r");
  284. if (!in)
  285. return;
  286. lineno = 0;
  287. while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
  288. lineno++;
  289. /* *strchrnul(buffer, '\n') = '\0'; - trim() will do it */
  290. *strchrnul(buffer, '#') = '\0';
  291. token = strtok(buffer, " \t");
  292. if (!token) continue;
  293. line = strtok(NULL, "");
  294. if (!line) continue;
  295. trim(line); /* remove leading/trailing whitespace */
  296. for (i = 0; i < ARRAY_SIZE(keywords); i++) {
  297. if (!strcasecmp(token, keywords[i].keyword)) {
  298. if (!keywords[i].handler(line, keywords[i].var)) {
  299. bb_error_msg("can't parse line %d in %s at '%s'",
  300. lineno, file, line);
  301. /* reset back to the default value */
  302. keywords[i].handler(keywords[i].def, keywords[i].var);
  303. }
  304. break;
  305. }
  306. }
  307. }
  308. fclose(in);
  309. server_config.start_ip = ntohl(server_config.start_ip);
  310. server_config.end_ip = ntohl(server_config.end_ip);
  311. }
  312. void write_leases(void)
  313. {
  314. int fp;
  315. unsigned i;
  316. time_t curr = time(0);
  317. unsigned long tmp_time;
  318. fp = open_or_warn(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC);
  319. if (fp < 0) {
  320. return;
  321. }
  322. for (i = 0; i < server_config.max_leases; i++) {
  323. if (leases[i].yiaddr != 0) {
  324. /* screw with the time in the struct, for easier writing */
  325. tmp_time = leases[i].expires;
  326. if (server_config.remaining) {
  327. if (lease_expired(&(leases[i])))
  328. leases[i].expires = 0;
  329. else leases[i].expires -= curr;
  330. } /* else stick with the time we got */
  331. leases[i].expires = htonl(leases[i].expires);
  332. // FIXME: error check??
  333. full_write(fp, &leases[i], sizeof(leases[i]));
  334. /* then restore it when done */
  335. leases[i].expires = tmp_time;
  336. }
  337. }
  338. close(fp);
  339. if (server_config.notify_file) {
  340. char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
  341. system(cmd);
  342. free(cmd);
  343. }
  344. }
  345. void read_leases(const char *file)
  346. {
  347. int fp;
  348. unsigned int i = 0;
  349. struct dhcpOfferedAddr lease;
  350. fp = open_or_warn(file, O_RDONLY);
  351. if (fp < 0) {
  352. return;
  353. }
  354. while (i < server_config.max_leases
  355. && full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
  356. ) {
  357. /* ADDME: is it a static lease */
  358. uint32_t y = ntohl(lease.yiaddr);
  359. if (y >= server_config.start_ip && y <= server_config.end_ip) {
  360. lease.expires = ntohl(lease.expires);
  361. if (!server_config.remaining)
  362. lease.expires -= time(0);
  363. if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
  364. bb_error_msg("too many leases while loading %s", file);
  365. break;
  366. }
  367. i++;
  368. }
  369. }
  370. DEBUG("Read %d leases", i);
  371. close(fp);
  372. }