"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 2 | /* script.c |
| 3 | * |
| 4 | * Functions to call the DHCP client notification scripts |
| 5 | * |
| 6 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 7 | * |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 11 | #include "common.h" |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 12 | #include "dhcpd.h" |
| 13 | #include "dhcpc.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 14 | #include "options.h" |
| 15 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 16 | |
| 17 | /* get a rough idea of how long an option will be (rounding up...) */ |
| 18 | static const int max_option_length[] = { |
| 19 | [OPTION_IP] = sizeof("255.255.255.255 "), |
| 20 | [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2, |
| 21 | [OPTION_STRING] = 1, |
| 22 | [OPTION_BOOLEAN] = sizeof("yes "), |
| 23 | [OPTION_U8] = sizeof("255 "), |
| 24 | [OPTION_U16] = sizeof("65535 "), |
| 25 | [OPTION_S16] = sizeof("-32768 "), |
| 26 | [OPTION_U32] = sizeof("4294967295 "), |
| 27 | [OPTION_S32] = sizeof("-2147483684 "), |
| 28 | }; |
| 29 | |
| 30 | |
| 31 | static inline int upper_length(int length, int opt_index) |
| 32 | { |
| 33 | return max_option_length[opt_index] * |
| 34 | (length / option_lengths[opt_index]); |
| 35 | } |
| 36 | |
| 37 | |
Denis Vlasenko | b6aae0f | 2007-01-29 22:51:25 +0000 | [diff] [blame] | 38 | static int sprintip(char *dest, const char *pre, const uint8_t *ip) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 39 | { |
| 40 | return sprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]); |
| 41 | } |
| 42 | |
| 43 | |
| 44 | /* really simple implementation, just count the bits */ |
| 45 | static int mton(struct in_addr *mask) |
| 46 | { |
| 47 | int i; |
| 48 | unsigned long bits = ntohl(mask->s_addr); |
| 49 | /* too bad one can't check the carry bit, etc in c bit |
| 50 | * shifting */ |
| 51 | for (i = 0; i < 32 && !((bits >> i) & 1); i++); |
| 52 | return 32 - i; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /* Fill dest with the text of option 'option'. */ |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 57 | static void fill_options(char *dest, uint8_t *option, |
| 58 | const struct dhcp_option *type_p) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 59 | { |
| 60 | int type, optlen; |
| 61 | uint16_t val_u16; |
| 62 | int16_t val_s16; |
| 63 | uint32_t val_u32; |
| 64 | int32_t val_s32; |
| 65 | int len = option[OPT_LEN - 2]; |
| 66 | |
| 67 | dest += sprintf(dest, "%s=", type_p->name); |
| 68 | |
| 69 | type = type_p->flags & TYPE_MASK; |
| 70 | optlen = option_lengths[type]; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 71 | for (;;) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 72 | switch (type) { |
| 73 | case OPTION_IP_PAIR: |
| 74 | dest += sprintip(dest, "", option); |
| 75 | *(dest++) = '/'; |
| 76 | option += 4; |
| 77 | optlen = 4; |
| 78 | case OPTION_IP: /* Works regardless of host byte order. */ |
| 79 | dest += sprintip(dest, "", option); |
| 80 | break; |
| 81 | case OPTION_BOOLEAN: |
| 82 | dest += sprintf(dest, *option ? "yes" : "no"); |
| 83 | break; |
| 84 | case OPTION_U8: |
| 85 | dest += sprintf(dest, "%u", *option); |
| 86 | break; |
| 87 | case OPTION_U16: |
| 88 | memcpy(&val_u16, option, 2); |
| 89 | dest += sprintf(dest, "%u", ntohs(val_u16)); |
| 90 | break; |
| 91 | case OPTION_S16: |
| 92 | memcpy(&val_s16, option, 2); |
| 93 | dest += sprintf(dest, "%d", ntohs(val_s16)); |
| 94 | break; |
| 95 | case OPTION_U32: |
| 96 | memcpy(&val_u32, option, 4); |
| 97 | dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32)); |
| 98 | break; |
| 99 | case OPTION_S32: |
| 100 | memcpy(&val_s32, option, 4); |
| 101 | dest += sprintf(dest, "%ld", (long) ntohl(val_s32)); |
| 102 | break; |
| 103 | case OPTION_STRING: |
| 104 | memcpy(dest, option, len); |
| 105 | dest[len] = '\0'; |
| 106 | return; /* Short circuit this case */ |
| 107 | } |
| 108 | option += optlen; |
| 109 | len -= optlen; |
| 110 | if (len <= 0) break; |
| 111 | dest += sprintf(dest, " "); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | /* put all the parameters into an environment */ |
| 117 | static char **fill_envp(struct dhcpMessage *packet) |
| 118 | { |
| 119 | int num_options = 0; |
| 120 | int i, j; |
| 121 | char **envp; |
| 122 | uint8_t *temp; |
| 123 | struct in_addr subnet; |
| 124 | char over = 0; |
| 125 | |
| 126 | if (packet == NULL) |
| 127 | num_options = 0; |
| 128 | else { |
| 129 | for (i = 0; dhcp_options[i].code; i++) |
| 130 | if (get_option(packet, dhcp_options[i].code)) { |
| 131 | num_options++; |
| 132 | if (dhcp_options[i].code == DHCP_SUBNET) |
| 133 | num_options++; /* for mton */ |
| 134 | } |
| 135 | if (packet->siaddr) num_options++; |
| 136 | if ((temp = get_option(packet, DHCP_OPTION_OVER))) |
| 137 | over = *temp; |
| 138 | if (!(over & FILE_FIELD) && packet->file[0]) num_options++; |
| 139 | if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++; |
| 140 | } |
| 141 | |
Rob Landley | 9ffd423 | 2006-05-21 18:30:35 +0000 | [diff] [blame] | 142 | envp = xzalloc(sizeof(char *) * (num_options + 5)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 143 | j = 0; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 144 | envp[j++] = xasprintf("interface=%s", client_config.interface); |
| 145 | envp[j++] = xasprintf("PATH=%s", |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 146 | getenv("PATH") ? : "/bin:/usr/bin:/sbin:/usr/sbin"); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 147 | envp[j++] = xasprintf("HOME=%s", getenv("HOME") ? : "/"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 148 | |
| 149 | if (packet == NULL) return envp; |
| 150 | |
| 151 | envp[j] = xmalloc(sizeof("ip=255.255.255.255")); |
| 152 | sprintip(envp[j++], "ip=", (uint8_t *) &packet->yiaddr); |
| 153 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 154 | for (i = 0; dhcp_options[i].code; i++) { |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 155 | temp = get_option(packet, dhcp_options[i].code); |
Denis Vlasenko | c2f5b02 | 2006-11-28 00:21:46 +0000 | [diff] [blame] | 156 | if (!temp) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 157 | continue; |
| 158 | envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2], |
| 159 | dhcp_options[i].flags & TYPE_MASK) + strlen(dhcp_options[i].name) + 2); |
| 160 | fill_options(envp[j++], temp, &dhcp_options[i]); |
| 161 | |
| 162 | /* Fill in a subnet bits option for things like /24 */ |
| 163 | if (dhcp_options[i].code == DHCP_SUBNET) { |
| 164 | memcpy(&subnet, temp, 4); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 165 | envp[j++] = xasprintf("mask=%d", mton(&subnet)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | if (packet->siaddr) { |
| 169 | envp[j] = xmalloc(sizeof("siaddr=255.255.255.255")); |
| 170 | sprintip(envp[j++], "siaddr=", (uint8_t *) &packet->siaddr); |
| 171 | } |
| 172 | if (!(over & FILE_FIELD) && packet->file[0]) { |
| 173 | /* watch out for invalid packets */ |
| 174 | packet->file[sizeof(packet->file) - 1] = '\0'; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 175 | envp[j++] = xasprintf("boot_file=%s", packet->file); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 176 | } |
| 177 | if (!(over & SNAME_FIELD) && packet->sname[0]) { |
| 178 | /* watch out for invalid packets */ |
| 179 | packet->sname[sizeof(packet->sname) - 1] = '\0'; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 180 | envp[j++] = xasprintf("sname=%s", packet->sname); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 181 | } |
| 182 | return envp; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | /* Call a script with a par file and env vars */ |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 187 | void udhcp_run_script(struct dhcpMessage *packet, const char *name) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 188 | { |
| 189 | int pid; |
| 190 | char **envp, **curr; |
| 191 | |
| 192 | if (client_config.script == NULL) |
| 193 | return; |
| 194 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 195 | DEBUG("vfork'ing and execle'ing %s", client_config.script); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 196 | |
| 197 | envp = fill_envp(packet); |
| 198 | /* call script */ |
| 199 | pid = vfork(); |
| 200 | if (pid) { |
| 201 | waitpid(pid, NULL, 0); |
| 202 | for (curr = envp; *curr; curr++) free(*curr); |
| 203 | free(envp); |
| 204 | return; |
| 205 | } else if (pid == 0) { |
| 206 | /* close fd's? */ |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 207 | /* exec script */ |
| 208 | execle(client_config.script, client_config.script, |
| 209 | name, NULL, envp); |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 210 | bb_perror_msg("script %s failed", client_config.script); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 211 | exit(1); |
| 212 | } |
| 213 | } |