"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 "dhcpc.h" |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 13 | #include "options.h" |
| 14 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 15 | |
| 16 | /* get a rough idea of how long an option will be (rounding up...) */ |
Denis Vlasenko | 3f11231 | 2007-08-19 21:21:16 +0000 | [diff] [blame] | 17 | static const uint8_t max_option_length[] = { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 18 | [OPTION_IP] = sizeof("255.255.255.255 "), |
| 19 | [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2, |
| 20 | [OPTION_STRING] = 1, |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 21 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 22 | [OPTION_STR1035] = 1, |
| 23 | #endif |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 24 | [OPTION_BOOLEAN] = sizeof("yes "), |
| 25 | [OPTION_U8] = sizeof("255 "), |
| 26 | [OPTION_U16] = sizeof("65535 "), |
| 27 | [OPTION_S16] = sizeof("-32768 "), |
| 28 | [OPTION_U32] = sizeof("4294967295 "), |
| 29 | [OPTION_S32] = sizeof("-2147483684 "), |
| 30 | }; |
| 31 | |
| 32 | |
| 33 | static inline int upper_length(int length, int opt_index) |
| 34 | { |
| 35 | return max_option_length[opt_index] * |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 36 | (length / dhcp_option_lengths[opt_index]); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | |
Denys Vlasenko | 56f2d06 | 2009-06-16 10:25:35 +0200 | [diff] [blame] | 40 | /* note: ip is a pointer to an IP in network order, possibly misaliged */ |
| 41 | static int sprint_nip(char *dest, const char *pre, const uint8_t *ip) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 42 | { |
| 43 | return sprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | /* really simple implementation, just count the bits */ |
Denis Vlasenko | 35ff746 | 2007-11-28 19:23:12 +0000 | [diff] [blame] | 48 | static int mton(uint32_t mask) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 49 | { |
Denis Vlasenko | 35ff746 | 2007-11-28 19:23:12 +0000 | [diff] [blame] | 50 | int i = 0; |
| 51 | mask = ntohl(mask); /* 111110000-like bit pattern */ |
| 52 | while (mask) { |
| 53 | i++; |
| 54 | mask <<= 1; |
| 55 | } |
| 56 | return i; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 60 | /* Allocate and fill with the text of option 'option'. */ |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 61 | static char *alloc_fill_opts(uint8_t *option, const struct dhcp_option *type_p, const char *opt_name) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 62 | { |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 63 | int len, type, optlen; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 64 | uint16_t val_u16; |
| 65 | int16_t val_s16; |
| 66 | uint32_t val_u32; |
| 67 | int32_t val_s32; |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 68 | char *dest, *ret; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 70 | len = option[OPT_LEN - 2]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 71 | type = type_p->flags & TYPE_MASK; |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 72 | optlen = dhcp_option_lengths[type]; |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 73 | |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 74 | dest = ret = xmalloc(upper_length(len, type) + strlen(opt_name) + 2); |
| 75 | dest += sprintf(ret, "%s=", opt_name); |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 76 | |
Denis Vlasenko | 5a3395b | 2006-11-18 19:51:32 +0000 | [diff] [blame] | 77 | for (;;) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 78 | switch (type) { |
| 79 | case OPTION_IP_PAIR: |
Denys Vlasenko | 56f2d06 | 2009-06-16 10:25:35 +0200 | [diff] [blame] | 80 | dest += sprint_nip(dest, "", option); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 81 | *dest++ = '/'; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 82 | option += 4; |
| 83 | optlen = 4; |
| 84 | case OPTION_IP: /* Works regardless of host byte order. */ |
Denys Vlasenko | 56f2d06 | 2009-06-16 10:25:35 +0200 | [diff] [blame] | 85 | dest += sprint_nip(dest, "", option); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 86 | break; |
| 87 | case OPTION_BOOLEAN: |
| 88 | dest += sprintf(dest, *option ? "yes" : "no"); |
| 89 | break; |
| 90 | case OPTION_U8: |
| 91 | dest += sprintf(dest, "%u", *option); |
| 92 | break; |
| 93 | case OPTION_U16: |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 94 | move_from_unaligned16(val_u16, option); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 95 | dest += sprintf(dest, "%u", ntohs(val_u16)); |
| 96 | break; |
| 97 | case OPTION_S16: |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 98 | move_from_unaligned16(val_s16, option); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 99 | dest += sprintf(dest, "%d", ntohs(val_s16)); |
| 100 | break; |
| 101 | case OPTION_U32: |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 102 | move_from_unaligned32(val_u32, option); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 103 | dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32)); |
| 104 | break; |
| 105 | case OPTION_S32: |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 106 | move_from_unaligned32(val_s32, option); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 107 | dest += sprintf(dest, "%ld", (long) ntohl(val_s32)); |
| 108 | break; |
| 109 | case OPTION_STRING: |
| 110 | memcpy(dest, option, len); |
| 111 | dest[len] = '\0'; |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 112 | return ret; /* Short circuit this case */ |
Denis Vlasenko | 35a064b | 2008-11-06 00:49:59 +0000 | [diff] [blame] | 113 | #if ENABLE_FEATURE_UDHCP_RFC3397 |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 114 | case OPTION_STR1035: |
| 115 | /* unpack option into dest; use ret for prefix (i.e., "optname=") */ |
| 116 | dest = dname_dec(option, len, ret); |
| 117 | free(ret); |
| 118 | return dest; |
| 119 | #endif |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 120 | } |
| 121 | option += optlen; |
| 122 | len -= optlen; |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 123 | if (len <= 0) |
| 124 | break; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 125 | dest += sprintf(dest, " "); |
| 126 | } |
Denis Vlasenko | 5066473 | 2007-02-27 21:15:08 +0000 | [diff] [blame] | 127 | return ret; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | |
| 131 | /* put all the parameters into an environment */ |
| 132 | static char **fill_envp(struct dhcpMessage *packet) |
| 133 | { |
| 134 | int num_options = 0; |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 135 | int i; |
| 136 | char **envp, **curr; |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 137 | const char *opt_name; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 138 | uint8_t *temp; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 139 | uint8_t over = 0; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 141 | if (packet) { |
| 142 | for (i = 0; dhcp_options[i].code; i++) { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 143 | if (get_option(packet, dhcp_options[i].code)) { |
| 144 | num_options++; |
| 145 | if (dhcp_options[i].code == DHCP_SUBNET) |
| 146 | num_options++; /* for mton */ |
| 147 | } |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 148 | } |
Denys Vlasenko | 56f2d06 | 2009-06-16 10:25:35 +0200 | [diff] [blame] | 149 | if (packet->siaddr_nip) |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 150 | num_options++; |
Denis Vlasenko | bd79c3d | 2009-04-01 12:36:09 +0000 | [diff] [blame] | 151 | temp = get_option(packet, DHCP_OPTION_OVERLOAD); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 152 | if (temp) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 153 | over = *temp; |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 154 | if (!(over & FILE_FIELD) && packet->file[0]) |
| 155 | num_options++; |
| 156 | if (!(over & SNAME_FIELD) && packet->sname[0]) |
| 157 | num_options++; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 160 | curr = envp = xzalloc(sizeof(char *) * (num_options + 3)); |
| 161 | *curr = xasprintf("interface=%s", client_config.interface); |
| 162 | putenv(*curr++); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 163 | |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 164 | if (packet == NULL) |
| 165 | return envp; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 166 | |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 167 | *curr = xmalloc(sizeof("ip=255.255.255.255")); |
Denys Vlasenko | 56f2d06 | 2009-06-16 10:25:35 +0200 | [diff] [blame] | 168 | sprint_nip(*curr, "ip=", (uint8_t *) &packet->yiaddr); |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 169 | putenv(*curr++); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 170 | |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 171 | opt_name = dhcp_option_strings; |
| 172 | i = 0; |
| 173 | while (*opt_name) { |
Denis Vlasenko | c90c3f3 | 2006-11-23 12:57:49 +0000 | [diff] [blame] | 174 | temp = get_option(packet, dhcp_options[i].code); |
Denis Vlasenko | c2f5b02 | 2006-11-28 00:21:46 +0000 | [diff] [blame] | 175 | if (!temp) |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 176 | goto next; |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 177 | *curr = alloc_fill_opts(temp, &dhcp_options[i], opt_name); |
| 178 | putenv(*curr++); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 179 | |
| 180 | /* Fill in a subnet bits option for things like /24 */ |
| 181 | if (dhcp_options[i].code == DHCP_SUBNET) { |
Denis Vlasenko | 35ff746 | 2007-11-28 19:23:12 +0000 | [diff] [blame] | 182 | uint32_t subnet; |
Denis Vlasenko | efb545b | 2008-12-08 22:56:18 +0000 | [diff] [blame] | 183 | move_from_unaligned32(subnet, temp); |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 184 | *curr = xasprintf("mask=%d", mton(subnet)); |
| 185 | putenv(*curr++); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 186 | } |
Denis Vlasenko | b539c84 | 2007-11-29 08:17:45 +0000 | [diff] [blame] | 187 | next: |
| 188 | opt_name += strlen(opt_name) + 1; |
| 189 | i++; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 190 | } |
Denys Vlasenko | 56f2d06 | 2009-06-16 10:25:35 +0200 | [diff] [blame] | 191 | if (packet->siaddr_nip) { |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 192 | *curr = xmalloc(sizeof("siaddr=255.255.255.255")); |
Denys Vlasenko | 56f2d06 | 2009-06-16 10:25:35 +0200 | [diff] [blame] | 193 | sprint_nip(*curr, "siaddr=", (uint8_t *) &packet->siaddr_nip); |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 194 | putenv(*curr++); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 195 | } |
| 196 | if (!(over & FILE_FIELD) && packet->file[0]) { |
| 197 | /* watch out for invalid packets */ |
| 198 | packet->file[sizeof(packet->file) - 1] = '\0'; |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 199 | *curr = xasprintf("boot_file=%s", packet->file); |
| 200 | putenv(*curr++); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 201 | } |
| 202 | if (!(over & SNAME_FIELD) && packet->sname[0]) { |
| 203 | /* watch out for invalid packets */ |
| 204 | packet->sname[sizeof(packet->sname) - 1] = '\0'; |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 205 | *curr = xasprintf("sname=%s", packet->sname); |
| 206 | putenv(*curr++); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 207 | } |
| 208 | return envp; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | /* Call a script with a par file and env vars */ |
Denis Vlasenko | f1980f6 | 2008-09-26 09:34:59 +0000 | [diff] [blame] | 213 | void FAST_FUNC udhcp_run_script(struct dhcpMessage *packet, const char *name) |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 214 | { |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 215 | char **envp, **curr; |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 216 | char *argv[3]; |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 217 | |
| 218 | if (client_config.script == NULL) |
| 219 | return; |
| 220 | |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 221 | DEBUG("vfork'ing and exec'ing %s", client_config.script); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 222 | |
| 223 | envp = fill_envp(packet); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 224 | |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 225 | /* call script */ |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 226 | argv[0] = (char*) client_config.script; |
| 227 | argv[1] = (char*) name; |
| 228 | argv[2] = NULL; |
| 229 | wait4pid(spawn(argv)); |
| 230 | |
| 231 | for (curr = envp; *curr; curr++) { |
| 232 | bb_unsetenv(*curr); |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 233 | free(*curr); |
Denis Vlasenko | 76ddc2e | 2008-12-30 05:05:31 +0000 | [diff] [blame] | 234 | } |
Denis Vlasenko | fbd2918 | 2007-04-07 01:05:47 +0000 | [diff] [blame] | 235 | free(envp); |
Mike Frysinger | 7031f62 | 2006-05-08 03:20:50 +0000 | [diff] [blame] | 236 | } |