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