Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 1 | /* script.c |
| 2 | * |
| 3 | * Functions to call the DHCP client notification scripts |
| 4 | * |
| 5 | * Russ Dill <Russ.Dill@asu.edu> July 2001 |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <sys/socket.h> |
| 27 | #include <netinet/in.h> |
| 28 | #include <arpa/inet.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <errno.h> |
| 32 | |
| 33 | #include "options.h" |
| 34 | #include "dhcpd.h" |
| 35 | #include "dhcpc.h" |
| 36 | #include "packet.h" |
| 37 | #include "options.h" |
| 38 | #include "debug.h" |
| 39 | |
Glenn L McGrath | 6b5bd0e | 2002-12-08 22:17:54 +0000 | [diff] [blame^] | 40 | #include "config.h" |
| 41 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 42 | /* get a rough idea of how long an option will be (rounding up...) */ |
| 43 | static int max_option_length[] = { |
| 44 | [OPTION_IP] = sizeof("255.255.255.255 "), |
| 45 | [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2, |
| 46 | [OPTION_STRING] = 1, |
| 47 | [OPTION_BOOLEAN] = sizeof("yes "), |
| 48 | [OPTION_U8] = sizeof("255 "), |
| 49 | [OPTION_U16] = sizeof("65535 "), |
| 50 | [OPTION_S16] = sizeof("-32768 "), |
| 51 | [OPTION_U32] = sizeof("4294967295 "), |
| 52 | [OPTION_S32] = sizeof("-2147483684 "), |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | static int upper_length(int length, struct dhcp_option *option) |
| 57 | { |
| 58 | return max_option_length[option->flags & TYPE_MASK] * |
| 59 | (length / option_lengths[option->flags & TYPE_MASK]); |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static int sprintip(char *dest, char *pre, unsigned char *ip) { |
| 64 | return sprintf(dest, "%s%d.%d.%d.%d ", pre, ip[0], ip[1], ip[2], ip[3]); |
| 65 | } |
| 66 | |
Glenn L McGrath | 6b5bd0e | 2002-12-08 22:17:54 +0000 | [diff] [blame^] | 67 | #ifdef CONFIG_FEATURE_UDHCPC_IP |
| 68 | /* convert a netmask (255.255.255.0) into the length (24) */ |
| 69 | static int inet_ntom (const char *src, short *dst) |
| 70 | { |
| 71 | in_addr_t mask, num; |
| 72 | |
| 73 | mask = ntohl(*(unsigned int *)src); |
| 74 | |
| 75 | for (num = mask; num & 1; num >>= 1); |
| 76 | |
| 77 | if (num != 0 && mask != 0) |
| 78 | { |
| 79 | for (num = ~mask; num & 1; num >>= 1); |
| 80 | if (num) |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | for (num = 0; mask; mask <<= 1) |
| 85 | num++; |
| 86 | |
| 87 | *dst = num; |
| 88 | |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | static int sprintprefix(char *dest, char *pre, unsigned char *ip) { |
| 93 | short sdest = 0; |
| 94 | inet_ntom(ip, &sdest); |
| 95 | return sprintf(dest, "%s%hd ", pre, sdest); |
| 96 | } |
| 97 | #endif |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 98 | |
| 99 | /* Fill dest with the text of option 'option'. */ |
| 100 | static void fill_options(char *dest, unsigned char *option, struct dhcp_option *type_p) |
| 101 | { |
| 102 | int type, optlen; |
| 103 | u_int16_t val_u16; |
| 104 | int16_t val_s16; |
| 105 | u_int32_t val_u32; |
| 106 | int32_t val_s32; |
| 107 | int len = option[OPT_LEN - 2]; |
| 108 | |
| 109 | dest += sprintf(dest, "%s=", type_p->name); |
| 110 | |
| 111 | type = type_p->flags & TYPE_MASK; |
| 112 | optlen = option_lengths[type]; |
| 113 | for(;;) { |
| 114 | switch (type) { |
| 115 | case OPTION_IP_PAIR: |
| 116 | dest += sprintip(dest, "", option); |
| 117 | *(dest++) = '/'; |
| 118 | option += 4; |
| 119 | optlen = 4; |
Glenn L McGrath | 6b5bd0e | 2002-12-08 22:17:54 +0000 | [diff] [blame^] | 120 | #ifndef CONFIG_FEATURE_UDHCPC_IP |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 121 | case OPTION_IP: /* Works regardless of host byte order. */ |
Glenn L McGrath | 6b5bd0e | 2002-12-08 22:17:54 +0000 | [diff] [blame^] | 122 | #endif |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 123 | dest += sprintip(dest, "", option); |
| 124 | break; |
Glenn L McGrath | 6b5bd0e | 2002-12-08 22:17:54 +0000 | [diff] [blame^] | 125 | #ifdef CONFIG_FEATURE_UDHCPC_IP |
| 126 | case OPTION_IP: /* Works regardless of host byte order. */ |
| 127 | if (type_p->flags & OPTION_PREFIX) { |
| 128 | dest += sprintprefix(dest, "", option); |
| 129 | } else { |
| 130 | dest += sprintip(dest, "", option); |
| 131 | } |
| 132 | break; |
| 133 | #endif |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 134 | case OPTION_BOOLEAN: |
| 135 | dest += sprintf(dest, *option ? "yes " : "no "); |
| 136 | break; |
| 137 | case OPTION_U8: |
| 138 | dest += sprintf(dest, "%u ", *option); |
| 139 | break; |
| 140 | case OPTION_U16: |
| 141 | memcpy(&val_u16, option, 2); |
| 142 | dest += sprintf(dest, "%u ", ntohs(val_u16)); |
| 143 | break; |
| 144 | case OPTION_S16: |
| 145 | memcpy(&val_s16, option, 2); |
| 146 | dest += sprintf(dest, "%d ", ntohs(val_s16)); |
| 147 | break; |
| 148 | case OPTION_U32: |
| 149 | memcpy(&val_u32, option, 4); |
| 150 | dest += sprintf(dest, "%lu ", (unsigned long) ntohl(val_u32)); |
| 151 | break; |
| 152 | case OPTION_S32: |
| 153 | memcpy(&val_s32, option, 4); |
| 154 | dest += sprintf(dest, "%ld ", (long) ntohl(val_s32)); |
| 155 | break; |
| 156 | case OPTION_STRING: |
| 157 | memcpy(dest, option, len); |
| 158 | dest[len] = '\0'; |
| 159 | return; /* Short circuit this case */ |
| 160 | } |
| 161 | option += optlen; |
| 162 | len -= optlen; |
| 163 | if (len <= 0) break; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | |
| 168 | static char *find_env(const char *prefix, char *defaultstr) |
| 169 | { |
| 170 | extern char **environ; |
| 171 | char **ptr; |
| 172 | const int len = strlen(prefix); |
| 173 | |
| 174 | for (ptr = environ; *ptr != NULL; ptr++) { |
| 175 | if (strncmp(prefix, *ptr, len) == 0) |
| 176 | return *ptr; |
| 177 | } |
| 178 | return defaultstr; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | /* put all the paramaters into an environment */ |
| 183 | static char **fill_envp(struct dhcpMessage *packet) |
| 184 | { |
| 185 | int num_options = 0; |
| 186 | int i, j; |
| 187 | char **envp; |
| 188 | unsigned char *temp; |
| 189 | char over = 0; |
| 190 | |
| 191 | if (packet == NULL) |
| 192 | num_options = 0; |
| 193 | else { |
| 194 | for (i = 0; options[i].code; i++) |
| 195 | if (get_option(packet, options[i].code)) |
| 196 | num_options++; |
| 197 | if (packet->siaddr) num_options++; |
| 198 | if ((temp = get_option(packet, DHCP_OPTION_OVER))) |
| 199 | over = *temp; |
| 200 | if (!(over & FILE_FIELD) && packet->file[0]) num_options++; |
| 201 | if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++; |
| 202 | } |
| 203 | |
| 204 | envp = xmalloc((num_options + 5) * sizeof(char *)); |
| 205 | envp[0] = xmalloc(sizeof("interface=") + strlen(client_config.interface)); |
| 206 | sprintf(envp[0], "interface=%s", client_config.interface); |
| 207 | envp[1] = find_env("PATH", "PATH=/bin:/usr/bin:/sbin:/usr/sbin"); |
| 208 | envp[2] = find_env("HOME", "HOME=/"); |
| 209 | |
| 210 | if (packet == NULL) { |
| 211 | envp[3] = NULL; |
| 212 | return envp; |
| 213 | } |
| 214 | |
| 215 | envp[3] = xmalloc(sizeof("ip=255.255.255.255")); |
| 216 | sprintip(envp[3], "ip=", (unsigned char *) &packet->yiaddr); |
| 217 | for (i = 0, j = 4; options[i].code; i++) { |
| 218 | if ((temp = get_option(packet, options[i].code))) { |
| 219 | envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2], &options[i]) + strlen(options[i].name) + 2); |
| 220 | fill_options(envp[j], temp, &options[i]); |
| 221 | j++; |
| 222 | } |
| 223 | } |
| 224 | if (packet->siaddr) { |
| 225 | envp[j] = xmalloc(sizeof("siaddr=255.255.255.255")); |
| 226 | sprintip(envp[j++], "siaddr=", (unsigned char *) &packet->siaddr); |
| 227 | } |
| 228 | if (!(over & FILE_FIELD) && packet->file[0]) { |
| 229 | /* watch out for invalid packets */ |
| 230 | packet->file[sizeof(packet->file) - 1] = '\0'; |
| 231 | envp[j] = xmalloc(sizeof("boot_file=") + strlen(packet->file)); |
| 232 | sprintf(envp[j++], "boot_file=%s", packet->file); |
| 233 | } |
| 234 | if (!(over & SNAME_FIELD) && packet->sname[0]) { |
| 235 | /* watch out for invalid packets */ |
| 236 | packet->sname[sizeof(packet->sname) - 1] = '\0'; |
| 237 | envp[j] = xmalloc(sizeof("sname=") + strlen(packet->sname)); |
| 238 | sprintf(envp[j++], "sname=%s", packet->sname); |
| 239 | } |
| 240 | envp[j] = NULL; |
| 241 | return envp; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /* Call a script with a par file and env vars */ |
| 246 | void run_script(struct dhcpMessage *packet, const char *name) |
| 247 | { |
| 248 | int pid; |
| 249 | char **envp; |
| 250 | |
| 251 | if (client_config.script == NULL) |
| 252 | return; |
| 253 | |
| 254 | /* call script */ |
| 255 | pid = fork(); |
| 256 | if (pid) { |
| 257 | waitpid(pid, NULL, 0); |
| 258 | return; |
| 259 | } else if (pid == 0) { |
| 260 | envp = fill_envp(packet); |
| 261 | |
| 262 | /* close fd's? */ |
| 263 | |
| 264 | /* exec script */ |
| 265 | DEBUG(LOG_INFO, "execle'ing %s", client_config.script); |
| 266 | execle(client_config.script, client_config.script, |
| 267 | name, NULL, envp); |
| 268 | LOG(LOG_ERR, "script %s failed: %s", |
| 269 | client_config.script, strerror(errno)); |
| 270 | exit(1); |
| 271 | } |
| 272 | } |