"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 | |
| 38 | static int sprintip(char *dest, char *pre, uint8_t *ip) |
| 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'. */ |
| 57 | static void fill_options(char *dest, uint8_t *option, struct dhcp_option *type_p) |
| 58 | { |
| 59 | int type, optlen; |
| 60 | uint16_t val_u16; |
| 61 | int16_t val_s16; |
| 62 | uint32_t val_u32; |
| 63 | int32_t val_s32; |
| 64 | int len = option[OPT_LEN - 2]; |
| 65 | |
| 66 | dest += sprintf(dest, "%s=", type_p->name); |
| 67 | |
| 68 | type = type_p->flags & TYPE_MASK; |
| 69 | optlen = option_lengths[type]; |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame^] | 70 | for (;;) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 71 | switch (type) { |
| 72 | case OPTION_IP_PAIR: |
| 73 | dest += sprintip(dest, "", option); |
| 74 | *(dest++) = '/'; |
| 75 | option += 4; |
| 76 | optlen = 4; |
| 77 | case OPTION_IP: /* Works regardless of host byte order. */ |
| 78 | dest += sprintip(dest, "", option); |
| 79 | break; |
| 80 | case OPTION_BOOLEAN: |
| 81 | dest += sprintf(dest, *option ? "yes" : "no"); |
| 82 | break; |
| 83 | case OPTION_U8: |
| 84 | dest += sprintf(dest, "%u", *option); |
| 85 | break; |
| 86 | case OPTION_U16: |
| 87 | memcpy(&val_u16, option, 2); |
| 88 | dest += sprintf(dest, "%u", ntohs(val_u16)); |
| 89 | break; |
| 90 | case OPTION_S16: |
| 91 | memcpy(&val_s16, option, 2); |
| 92 | dest += sprintf(dest, "%d", ntohs(val_s16)); |
| 93 | break; |
| 94 | case OPTION_U32: |
| 95 | memcpy(&val_u32, option, 4); |
| 96 | dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32)); |
| 97 | break; |
| 98 | case OPTION_S32: |
| 99 | memcpy(&val_s32, option, 4); |
| 100 | dest += sprintf(dest, "%ld", (long) ntohl(val_s32)); |
| 101 | break; |
| 102 | case OPTION_STRING: |
| 103 | memcpy(dest, option, len); |
| 104 | dest[len] = '\0'; |
| 105 | return; /* Short circuit this case */ |
| 106 | } |
| 107 | option += optlen; |
| 108 | len -= optlen; |
| 109 | if (len <= 0) break; |
| 110 | dest += sprintf(dest, " "); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /* put all the parameters into an environment */ |
| 116 | static char **fill_envp(struct dhcpMessage *packet) |
| 117 | { |
| 118 | int num_options = 0; |
| 119 | int i, j; |
| 120 | char **envp; |
| 121 | uint8_t *temp; |
| 122 | struct in_addr subnet; |
| 123 | char over = 0; |
| 124 | |
| 125 | if (packet == NULL) |
| 126 | num_options = 0; |
| 127 | else { |
| 128 | for (i = 0; dhcp_options[i].code; i++) |
| 129 | if (get_option(packet, dhcp_options[i].code)) { |
| 130 | num_options++; |
| 131 | if (dhcp_options[i].code == DHCP_SUBNET) |
| 132 | num_options++; /* for mton */ |
| 133 | } |
| 134 | if (packet->siaddr) num_options++; |
| 135 | if ((temp = get_option(packet, DHCP_OPTION_OVER))) |
| 136 | over = *temp; |
| 137 | if (!(over & FILE_FIELD) && packet->file[0]) num_options++; |
| 138 | if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++; |
| 139 | } |
| 140 | |
Rob Landley | 9ffd423 | 2006-05-21 18:30:35 +0000 | [diff] [blame] | 141 | envp = xzalloc(sizeof(char *) * (num_options + 5)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 142 | j = 0; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 143 | envp[j++] = xasprintf("interface=%s", client_config.interface); |
| 144 | envp[j++] = xasprintf("PATH=%s", |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 145 | getenv("PATH") ? : "/bin:/usr/bin:/sbin:/usr/sbin"); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 146 | envp[j++] = xasprintf("HOME=%s", getenv("HOME") ? : "/"); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 147 | |
| 148 | if (packet == NULL) return envp; |
| 149 | |
| 150 | envp[j] = xmalloc(sizeof("ip=255.255.255.255")); |
| 151 | sprintip(envp[j++], "ip=", (uint8_t *) &packet->yiaddr); |
| 152 | |
| 153 | |
| 154 | for (i = 0; dhcp_options[i].code; i++) { |
| 155 | if (!(temp = get_option(packet, dhcp_options[i].code))) |
| 156 | continue; |
| 157 | envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2], |
| 158 | dhcp_options[i].flags & TYPE_MASK) + strlen(dhcp_options[i].name) + 2); |
| 159 | fill_options(envp[j++], temp, &dhcp_options[i]); |
| 160 | |
| 161 | /* Fill in a subnet bits option for things like /24 */ |
| 162 | if (dhcp_options[i].code == DHCP_SUBNET) { |
| 163 | memcpy(&subnet, temp, 4); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 164 | envp[j++] = xasprintf("mask=%d", mton(&subnet)); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | if (packet->siaddr) { |
| 168 | envp[j] = xmalloc(sizeof("siaddr=255.255.255.255")); |
| 169 | sprintip(envp[j++], "siaddr=", (uint8_t *) &packet->siaddr); |
| 170 | } |
| 171 | if (!(over & FILE_FIELD) && packet->file[0]) { |
| 172 | /* watch out for invalid packets */ |
| 173 | packet->file[sizeof(packet->file) - 1] = '\0'; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 174 | envp[j++] = xasprintf("boot_file=%s", packet->file); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 175 | } |
| 176 | if (!(over & SNAME_FIELD) && packet->sname[0]) { |
| 177 | /* watch out for invalid packets */ |
| 178 | packet->sname[sizeof(packet->sname) - 1] = '\0'; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 179 | envp[j++] = xasprintf("sname=%s", packet->sname); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 180 | } |
| 181 | return envp; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /* Call a script with a par file and env vars */ |
Rob Landley | 3f78561 | 2006-05-28 01:06:36 +0000 | [diff] [blame] | 186 | void udhcp_run_script(struct dhcpMessage *packet, const char *name) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 187 | { |
| 188 | int pid; |
| 189 | char **envp, **curr; |
| 190 | |
| 191 | if (client_config.script == NULL) |
| 192 | return; |
| 193 | |
Denis Vlasenko | 3538b9a | 2006-09-06 18:36:50 +0000 | [diff] [blame] | 194 | DEBUG("vfork'ing and execle'ing %s", client_config.script); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 195 | |
| 196 | envp = fill_envp(packet); |
| 197 | /* call script */ |
| 198 | pid = vfork(); |
| 199 | if (pid) { |
| 200 | waitpid(pid, NULL, 0); |
| 201 | for (curr = envp; *curr; curr++) free(*curr); |
| 202 | free(envp); |
| 203 | return; |
| 204 | } else if (pid == 0) { |
| 205 | /* close fd's? */ |
| 206 | |
| 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 | } |