files.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /*
  2. * files.c -- DHCP server file manipulation *
  3. * Rewrite by Russ Dill <Russ.Dill@asu.edu> July 2001
  4. */
  5. #include <sys/socket.h>
  6. #include <arpa/inet.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #include <ctype.h>
  11. #include <netdb.h>
  12. #include <netinet/ether.h>
  13. #include "static_leases.h"
  14. #include "dhcpd.h"
  15. #include "files.h"
  16. #include "options.h"
  17. #include "common.h"
  18. /*
  19. * Domain names may have 254 chars, and string options can be 254
  20. * chars long. However, 80 bytes will be enough for most, and won't
  21. * hog up memory. If you have a special application, change it
  22. */
  23. #define READ_CONFIG_BUF_SIZE 80
  24. /* on these functions, make sure you datatype matches */
  25. static int read_ip(const char *line, void *arg)
  26. {
  27. struct in_addr *addr = arg;
  28. struct hostent *host;
  29. int retval = 1;
  30. if (!inet_aton(line, addr)) {
  31. if ((host = gethostbyname(line)))
  32. addr->s_addr = *((unsigned long *) host->h_addr_list[0]);
  33. else retval = 0;
  34. }
  35. return retval;
  36. }
  37. static int read_mac(const char *line, void *arg)
  38. {
  39. uint8_t *mac_bytes = arg;
  40. struct ether_addr *temp_ether_addr;
  41. int retval = 1;
  42. temp_ether_addr = ether_aton(line);
  43. if(temp_ether_addr == NULL)
  44. retval = 0;
  45. else
  46. memcpy(mac_bytes, temp_ether_addr, 6);
  47. return retval;
  48. }
  49. static int read_str(const char *line, void *arg)
  50. {
  51. char **dest = arg;
  52. if (*dest) free(*dest);
  53. *dest = strdup(line);
  54. return 1;
  55. }
  56. static int read_u32(const char *line, void *arg)
  57. {
  58. uint32_t *dest = arg;
  59. char *endptr;
  60. *dest = strtoul(line, &endptr, 0);
  61. return endptr[0] == '\0';
  62. }
  63. static int read_yn(const char *line, void *arg)
  64. {
  65. char *dest = arg;
  66. int retval = 1;
  67. if (!strcasecmp("yes", line))
  68. *dest = 1;
  69. else if (!strcasecmp("no", line))
  70. *dest = 0;
  71. else retval = 0;
  72. return retval;
  73. }
  74. /* read a dhcp option and add it to opt_list */
  75. static int read_opt(const char *const_line, void *arg)
  76. {
  77. struct option_set **opt_list = arg;
  78. char *opt, *val, *endptr;
  79. struct dhcp_option *option;
  80. int retval = 0, length;
  81. char buffer[8];
  82. char *line;
  83. uint16_t *result_u16 = (uint16_t *) buffer;
  84. uint32_t *result_u32 = (uint32_t *) buffer;
  85. /* Cheat, the only const line we'll actually get is "" */
  86. line = (char *) const_line;
  87. if (!(opt = strtok(line, " \t="))) return 0;
  88. for (option = dhcp_options; option->code; option++)
  89. if (!strcasecmp(option->name, opt))
  90. break;
  91. if (!option->code) return 0;
  92. do {
  93. if (!(val = strtok(NULL, ", \t"))) break;
  94. length = option_lengths[option->flags & TYPE_MASK];
  95. retval = 0;
  96. opt = buffer; /* new meaning for variable opt */
  97. switch (option->flags & TYPE_MASK) {
  98. case OPTION_IP:
  99. retval = read_ip(val, buffer);
  100. break;
  101. case OPTION_IP_PAIR:
  102. retval = read_ip(val, buffer);
  103. if (!(val = strtok(NULL, ", \t/-"))) retval = 0;
  104. if (retval) retval = read_ip(val, buffer + 4);
  105. break;
  106. case OPTION_STRING:
  107. length = strlen(val);
  108. if (length > 0) {
  109. if (length > 254) length = 254;
  110. opt = val;
  111. retval = 1;
  112. }
  113. break;
  114. case OPTION_BOOLEAN:
  115. retval = read_yn(val, buffer);
  116. break;
  117. case OPTION_U8:
  118. buffer[0] = strtoul(val, &endptr, 0);
  119. retval = (endptr[0] == '\0');
  120. break;
  121. case OPTION_U16:
  122. *result_u16 = htons(strtoul(val, &endptr, 0));
  123. retval = (endptr[0] == '\0');
  124. break;
  125. case OPTION_S16:
  126. *result_u16 = htons(strtol(val, &endptr, 0));
  127. retval = (endptr[0] == '\0');
  128. break;
  129. case OPTION_U32:
  130. *result_u32 = htonl(strtoul(val, &endptr, 0));
  131. retval = (endptr[0] == '\0');
  132. break;
  133. case OPTION_S32:
  134. *result_u32 = htonl(strtol(val, &endptr, 0));
  135. retval = (endptr[0] == '\0');
  136. break;
  137. default:
  138. break;
  139. }
  140. if (retval)
  141. attach_option(opt_list, option, opt, length);
  142. } while (retval && option->flags & OPTION_LIST);
  143. return retval;
  144. }
  145. static int read_staticlease(const char *const_line, void *arg)
  146. {
  147. char *line;
  148. char *mac_string;
  149. char *ip_string;
  150. uint8_t *mac_bytes;
  151. uint32_t *ip;
  152. /* Allocate memory for addresses */
  153. mac_bytes = xmalloc(sizeof(unsigned char) * 8);
  154. ip = xmalloc(sizeof(uint32_t));
  155. /* Read mac */
  156. line = (char *) const_line;
  157. mac_string = strtok(line, " \t");
  158. read_mac(mac_string, mac_bytes);
  159. /* Read ip */
  160. ip_string = strtok(NULL, " \t");
  161. read_ip(ip_string, ip);
  162. addStaticLease(arg, mac_bytes, ip);
  163. #ifdef UDHCP_DEBUG
  164. printStaticLeases(arg);
  165. #endif
  166. return 1;
  167. }
  168. static const struct config_keyword keywords[] = {
  169. /* keyword handler variable address default */
  170. {"start", read_ip, &(server_config.start), "192.168.0.20"},
  171. {"end", read_ip, &(server_config.end), "192.168.0.254"},
  172. {"interface", read_str, &(server_config.interface), "eth0"},
  173. {"option", read_opt, &(server_config.options), ""},
  174. {"opt", read_opt, &(server_config.options), ""},
  175. {"max_leases", read_u32, &(server_config.max_leases), "254"},
  176. {"remaining", read_yn, &(server_config.remaining), "yes"},
  177. {"auto_time", read_u32, &(server_config.auto_time), "7200"},
  178. {"decline_time",read_u32, &(server_config.decline_time),"3600"},
  179. {"conflict_time",read_u32,&(server_config.conflict_time),"3600"},
  180. {"offer_time", read_u32, &(server_config.offer_time), "60"},
  181. {"min_lease", read_u32, &(server_config.min_lease), "60"},
  182. {"lease_file", read_str, &(server_config.lease_file), LEASES_FILE},
  183. {"pidfile", read_str, &(server_config.pidfile), "/var/run/udhcpd.pid"},
  184. {"notify_file", read_str, &(server_config.notify_file), ""},
  185. {"siaddr", read_ip, &(server_config.siaddr), "0.0.0.0"},
  186. {"sname", read_str, &(server_config.sname), ""},
  187. {"boot_file", read_str, &(server_config.boot_file), ""},
  188. {"static_lease",read_staticlease, &(server_config.static_leases), ""},
  189. /*ADDME: static lease */
  190. {"", NULL, NULL, ""}
  191. };
  192. int read_config(const char *file)
  193. {
  194. FILE *in;
  195. char buffer[READ_CONFIG_BUF_SIZE], *token, *line;
  196. #ifdef UDHCP_DEBUG
  197. char orig[READ_CONFIG_BUF_SIZE];
  198. #endif
  199. int i, lm = 0;
  200. for (i = 0; keywords[i].keyword[0]; i++)
  201. if (keywords[i].def[0])
  202. keywords[i].handler(keywords[i].def, keywords[i].var);
  203. if (!(in = fopen(file, "r"))) {
  204. LOG(LOG_ERR, "unable to open config file: %s", file);
  205. return 0;
  206. }
  207. while (fgets(buffer, READ_CONFIG_BUF_SIZE, in)) {
  208. lm++;
  209. if (strchr(buffer, '\n')) *(strchr(buffer, '\n')) = '\0';
  210. #ifdef UDHCP_DEBUG
  211. strcpy(orig, buffer);
  212. #endif
  213. if (strchr(buffer, '#')) *(strchr(buffer, '#')) = '\0';
  214. if (!(token = strtok(buffer, " \t"))) continue;
  215. if (!(line = strtok(NULL, ""))) continue;
  216. /* eat leading whitespace */
  217. line = line + strspn(line, " \t=");
  218. /* eat trailing whitespace */
  219. for (i = strlen(line); i > 0 && isspace(line[i - 1]); i--);
  220. line[i] = '\0';
  221. for (i = 0; keywords[i].keyword[0]; i++)
  222. if (!strcasecmp(token, keywords[i].keyword))
  223. if (!keywords[i].handler(line, keywords[i].var)) {
  224. LOG(LOG_ERR, "Failure parsing line %d of %s", lm, file);
  225. DEBUG(LOG_ERR, "unable to parse '%s'", orig);
  226. /* reset back to the default value */
  227. keywords[i].handler(keywords[i].def, keywords[i].var);
  228. }
  229. }
  230. fclose(in);
  231. return 1;
  232. }
  233. void write_leases(void)
  234. {
  235. FILE *fp;
  236. unsigned int i;
  237. char buf[255];
  238. time_t curr = time(0);
  239. unsigned long tmp_time;
  240. if (!(fp = fopen(server_config.lease_file, "w"))) {
  241. LOG(LOG_ERR, "Unable to open %s for writing", server_config.lease_file);
  242. return;
  243. }
  244. for (i = 0; i < server_config.max_leases; i++) {
  245. if (leases[i].yiaddr != 0) {
  246. /* screw with the time in the struct, for easier writing */
  247. tmp_time = leases[i].expires;
  248. if (server_config.remaining) {
  249. if (lease_expired(&(leases[i])))
  250. leases[i].expires = 0;
  251. else leases[i].expires -= curr;
  252. } /* else stick with the time we got */
  253. leases[i].expires = htonl(leases[i].expires);
  254. fwrite(&leases[i], sizeof(struct dhcpOfferedAddr), 1, fp);
  255. /* Then restore it when done. */
  256. leases[i].expires = tmp_time;
  257. }
  258. }
  259. fclose(fp);
  260. if (server_config.notify_file) {
  261. sprintf(buf, "%s %s", server_config.notify_file, server_config.lease_file);
  262. system(buf);
  263. }
  264. }
  265. void read_leases(const char *file)
  266. {
  267. FILE *fp;
  268. unsigned int i = 0;
  269. struct dhcpOfferedAddr lease;
  270. if (!(fp = fopen(file, "r"))) {
  271. LOG(LOG_ERR, "Unable to open %s for reading", file);
  272. return;
  273. }
  274. while (i < server_config.max_leases && (fread(&lease, sizeof lease, 1, fp) == 1)) {
  275. /* ADDME: is it a static lease */
  276. if (lease.yiaddr >= server_config.start && lease.yiaddr <= server_config.end) {
  277. lease.expires = ntohl(lease.expires);
  278. if (!server_config.remaining) lease.expires -= time(0);
  279. if (!(add_lease(lease.chaddr, lease.yiaddr, lease.expires))) {
  280. LOG(LOG_WARNING, "Too many leases while loading %s\n", file);
  281. break;
  282. }
  283. i++;
  284. }
  285. }
  286. DEBUG(LOG_INFO, "Read %d leases", i);
  287. fclose(fp);
  288. }