files.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. /*
  11. * Domain names may have 254 chars, and string options can be 254
  12. * chars long. However, 80 bytes will be enough for most, and won't
  13. * hog up memory. If you have a special application, change it
  14. */
  15. #define READ_CONFIG_BUF_SIZE 80
  16. /* on these functions, make sure you datatype matches */
  17. static int read_ip(const char *line, void *arg)
  18. {
  19. struct in_addr *addr = arg;
  20. struct hostent *host;
  21. int retval = 1;
  22. if (!inet_aton(line, addr)) {
  23. host = gethostbyname(line);
  24. if (host)
  25. addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
  26. else retval = 0;
  27. }
  28. return retval;
  29. }
  30. static int read_mac(const char *line, void *arg)
  31. {
  32. uint8_t *mac_bytes = arg;
  33. struct ether_addr *temp_ether_addr;
  34. int retval = 1;
  35. temp_ether_addr = ether_aton(line);
  36. if (temp_ether_addr == NULL)
  37. retval = 0;
  38. else
  39. memcpy(mac_bytes, temp_ether_addr, 6);
  40. return retval;
  41. }
  42. static int read_str(const char *line, void *arg)
  43. {
  44. char **dest = arg;
  45. free(*dest);
  46. *dest = xstrdup(line);
  47. return 1;
  48. }
  49. static int read_u32(const char *line, void *arg)
  50. {
  51. *((uint32_t*)arg) = bb_strtou32(line, NULL, 10);
  52. return errno == 0;
  53. }
  54. static int read_yn(const char *line, void *arg)
  55. {
  56. char *dest = arg;
  57. int retval = 1;
  58. if (!strcasecmp("yes", line))
  59. *dest = 1;
  60. else if (!strcasecmp("no", line))
  61. *dest = 0;
  62. else retval = 0;
  63. return retval;
  64. }
  65. /* find option 'code' in opt_list */
  66. struct option_set *find_option(struct option_set *opt_list, char code)
  67. {
  68. while (opt_list && opt_list->data[OPT_CODE] < code)
  69. opt_list = opt_list->next;
  70. if (opt_list && opt_list->data[OPT_CODE] == code) return opt_list;
  71. else return NULL;
  72. }
  73. /* add an option to the opt_list */
  74. static void attach_option(struct option_set **opt_list,
  75. const struct dhcp_option *option, char *buffer, int length)
  76. {
  77. struct option_set *existing, *new, **curr;
  78. /* add it to an existing option */
  79. existing = find_option(*opt_list, option->code);
  80. if (existing) {
  81. DEBUG("Attaching option %s to existing member of list", option->name);
  82. if (option->flags & OPTION_LIST) {
  83. if (existing->data[OPT_LEN] + length <= 255) {
  84. existing->data = realloc(existing->data,
  85. existing->data[OPT_LEN] + length + 2);
  86. memcpy(existing->data + existing->data[OPT_LEN] + 2, buffer, length);
  87. existing->data[OPT_LEN] += length;
  88. } /* else, ignore the data, we could put this in a second option in the future */
  89. } /* else, ignore the new data */
  90. } else {
  91. DEBUG("Attaching option %s to list", option->name);
  92. /* make a new option */
  93. new = xmalloc(sizeof(struct option_set));
  94. new->data = xmalloc(length + 2);
  95. new->data[OPT_CODE] = option->code;
  96. new->data[OPT_LEN] = length;
  97. memcpy(new->data + 2, buffer, length);
  98. curr = opt_list;
  99. while (*curr && (*curr)->data[OPT_CODE] < option->code)
  100. curr = &(*curr)->next;
  101. new->next = *curr;
  102. *curr = new;
  103. }
  104. }
  105. /* read a dhcp option and add it to opt_list */
  106. static int read_opt(const char *const_line, void *arg)
  107. {
  108. struct option_set **opt_list = arg;
  109. char *opt, *val, *endptr;
  110. const struct dhcp_option *option;
  111. int retval = 0, length;
  112. char buffer[8];
  113. char *line;
  114. uint16_t *result_u16 = (uint16_t *) buffer;
  115. uint32_t *result_u32 = (uint32_t *) buffer;
  116. /* Cheat, the only const line we'll actually get is "" */
  117. line = (char *) const_line;
  118. opt = strtok(line, " \t=");
  119. if (!opt) return 0;
  120. option = dhcp_options;
  121. while (1) {
  122. if (!option->code)
  123. return 0;
  124. if (!strcasecmp(option->name, opt))
  125. break;
  126. option++;
  127. }
  128. do {
  129. val = strtok(NULL, ", \t");
  130. if (!val) break;
  131. length = option_lengths[option->flags & TYPE_MASK];
  132. retval = 0;
  133. opt = buffer; /* new meaning for variable opt */
  134. switch (option->flags & TYPE_MASK) {
  135. case OPTION_IP:
  136. retval = read_ip(val, buffer);
  137. break;
  138. case OPTION_IP_PAIR:
  139. retval = read_ip(val, buffer);
  140. if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
  141. if (retval) retval = read_ip(val, buffer + 4);
  142. break;
  143. case OPTION_STRING:
  144. length = strlen(val);
  145. if (length > 0) {
  146. if (length > 254) length = 254;
  147. opt = val;
  148. retval = 1;
  149. }
  150. break;
  151. case OPTION_BOOLEAN:
  152. retval = read_yn(val, buffer);
  153. break;
  154. case OPTION_U8:
  155. buffer[0] = strtoul(val, &endptr, 0);
  156. retval = (endptr[0] == '\0');
  157. break;
  158. case OPTION_U16:
  159. *result_u16 = htons(strtoul(val, &endptr, 0));
  160. retval = (endptr[0] == '\0');
  161. break;
  162. case OPTION_S16:
  163. *result_u16 = htons(strtol(val, &endptr, 0));
  164. retval = (endptr[0] == '\0');
  165. break;
  166. case OPTION_U32:
  167. *result_u32 = htonl(strtoul(val, &endptr, 0));
  168. retval = (endptr[0] == '\0');
  169. break;
  170. case OPTION_S32:
  171. *result_u32 = htonl(strtol(val, &endptr, 0));
  172. retval = (endptr[0] == '\0');
  173. break;
  174. default:
  175. break;
  176. }
  177. if (retval)
  178. attach_option(opt_list, option, opt, length);
  179. } while (retval && option->flags & OPTION_LIST);
  180. return retval;
  181. }
  182. static int read_staticlease(const char *const_line, void *arg)
  183. {
  184. char *line;
  185. char *mac_string;
  186. char *ip_string;
  187. uint8_t *mac_bytes;
  188. uint32_t *ip;
  189. /* Allocate memory for addresses */
  190. mac_bytes = xmalloc(sizeof(unsigned char) * 8);
  191. ip = xmalloc(sizeof(uint32_t));
  192. /* Read mac */
  193. line = (char *) const_line;
  194. mac_string = strtok(line, " \t");
  195. read_mac(mac_string, mac_bytes);
  196. /* Read ip */
  197. ip_string = strtok(NULL, " \t");
  198. read_ip(ip_string, ip);
  199. addStaticLease(arg, mac_bytes, ip);
  200. if (ENABLE_FEATURE_UDHCP_DEBUG) printStaticLeases(arg);
  201. return 1;
  202. }
  203. static const struct config_keyword keywords[] = {
  204. /* keyword handler variable address default */
  205. {"start", read_ip, &(server_config.start), "192.168.0.20"},
  206. {"end", read_ip, &(server_config.end), "192.168.0.254"},
  207. {"interface", read_str, &(server_config.interface), "eth0"},
  208. {"option", read_opt, &(server_config.options), ""},
  209. {"opt", read_opt, &(server_config.options), ""},
  210. {"max_leases", read_u32, &(server_config.max_leases), "254"},
  211. {"remaining", read_yn, &(server_config.remaining), "yes"},
  212. {"auto_time", read_u32, &(server_config.auto_time), "7200"},
  213. {"decline_time",read_u32, &(server_config.decline_time),"3600"},
  214. {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
  215. {"offer_time", read_u32, &(server_config.offer_time), "60"},
  216. {"min_lease", read_u32, &(server_config.min_lease), "60"},
  217. {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
  218. {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
  219. {"notify_file", read_str, &(server_config.notify_file), ""},
  220. {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
  221. {"sname", read_str, &(server_config.sname), ""},
  222. {"boot_file", read_str, &(server_config.boot_file), ""},
  223. {"static_lease",read_staticlease, &(server_config.static_leases), ""},
  224. /*ADDME: static lease */
  225. {"", NULL, NULL, ""}
  226. };
  227. int read_config(const char *file)
  228. {
  229. FILE *in;
  230. char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
  231. int i, lm = 0;
  232. for (i = 0; keywords[i].keyword[0]; i++)
  233. if (keywords[i].def[0])
  234. keywords[i].handler(keywords[i].def, keywords[i].var);
  235. in = fopen(file, "r");
  236. if (!in) {
  237. bb_error_msg("cannot open config file: %s", file);
  238. return 0;
  239. }
  240. while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
  241. char debug_orig[READ_CONFIG_BUF_SIZE];
  242. char *p;
  243. lm++;
  244. p = strchr(buffer, '\n');
  245. if (p) *p = '\0';
  246. if (ENABLE_FEATURE_UDHCP_DEBUG) strcpy(debug_orig, buffer);
  247. p = strchr(buffer, '#');
  248. if (p) *p = '\0';
  249. if (!(token = strtok(buffer, " \t"))) continue;
  250. if (!(line = strtok(NULL, ""))) continue;
  251. /* eat leading whitespace */
  252. line = skip_whitespace(line);
  253. /* eat trailing whitespace */
  254. i = strlen(line) - 1;
  255. while (i >= 0 && isspace(line[i]))
  256. line[i--] = '\0';
  257. for (i = 0; keywords[i].keyword[0]; i++)
  258. if (!strcasecmp(token, keywords[i].keyword))
  259. if (!keywords[i].handler(line, keywords[i].var)) {
  260. bb_error_msg("cannot parse line %d of %s", lm, file);
  261. if (ENABLE_FEATURE_UDHCP_DEBUG)
  262. bb_error_msg("cannot parse '%s'", debug_orig);
  263. /* reset back to the default value */
  264. keywords[i].handler(keywords[i].def, keywords[i].var);
  265. }
  266. }
  267. fclose(in);
  268. return 1;
  269. }
  270. void write_leases(void)
  271. {
  272. int fp;
  273. unsigned i;
  274. time_t curr = time(0);
  275. unsigned long tmp_time;
  276. fp = open(server_config.lease_file, O_WRONLY|O_CREAT|O_TRUNC, 0666);
  277. if (fp < 0) {
  278. bb_error_msg("cannot open %s for writing", server_config.lease_file);
  279. return;
  280. }
  281. for (i = 0; i < server_config.max_leases; i++) {
  282. if (leases[i].yiaddr != 0) {
  283. /* screw with the time in the struct, for easier writing */
  284. tmp_time = leases[i].expires;
  285. if (server_config.remaining) {
  286. if (lease_expired(&(leases[i])))
  287. leases[i].expires = 0;
  288. else leases[i].expires -= curr;
  289. } /* else stick with the time we got */
  290. leases[i].expires = htonl(leases[i].expires);
  291. // FIXME: error check??
  292. full_write(fp, &leases[i], sizeof(leases[i]));
  293. /* then restore it when done */
  294. leases[i].expires = tmp_time;
  295. }
  296. }
  297. close(fp);
  298. if (server_config.notify_file) {
  299. char *cmd = xasprintf("%s %s", server_config.notify_file, server_config.lease_file);
  300. system(cmd);
  301. free(cmd);
  302. }
  303. }
  304. void read_leases(const char *file)
  305. {
  306. int fp;
  307. unsigned int i = 0;
  308. struct dhcpOfferedAddr lease;
  309. fp = open(file, O_RDONLY);
  310. if (fp < 0) {
  311. bb_error_msg("cannot open %s for reading", file);
  312. return;
  313. }
  314. while (i < server_config.max_leases
  315. && full_read(fp, &lease, sizeof(lease)) == sizeof(lease)
  316. ) {
  317. /* ADDME: is it a static lease */
  318. if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
  319. lease.expires = ntohl(lease.expires);
  320. if (!server_config.remaining) lease.expires -= time(0);
  321. if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
  322. bb_error_msg("too many leases while loading %s", file);
  323. break;
  324. }
  325. i++;
  326. }
  327. }
  328. DEBUG("Read %d leases", i);
  329. close(fp);
  330. }