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 | |
| 40 | /* get a rough idea of how long an option will be (rounding up...) */ |
| 41 | static int max_option_length[] = { |
| 42 | [OPTION_IP] = sizeof("255.255.255.255 "), |
| 43 | [OPTION_IP_PAIR] = sizeof("255.255.255.255 ") * 2, |
| 44 | [OPTION_STRING] = 1, |
| 45 | [OPTION_BOOLEAN] = sizeof("yes "), |
| 46 | [OPTION_U8] = sizeof("255 "), |
| 47 | [OPTION_U16] = sizeof("65535 "), |
| 48 | [OPTION_S16] = sizeof("-32768 "), |
| 49 | [OPTION_U32] = sizeof("4294967295 "), |
| 50 | [OPTION_S32] = sizeof("-2147483684 "), |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | static int upper_length(int length, struct dhcp_option *option) |
| 55 | { |
| 56 | return max_option_length[option->flags & TYPE_MASK] * |
| 57 | (length / option_lengths[option->flags & TYPE_MASK]); |
| 58 | } |
| 59 | |
| 60 | |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 61 | static int sprintip(char *dest, char *pre, unsigned char *ip) |
Glenn L McGrath | 6b5bd0e | 2002-12-08 22:17:54 +0000 | [diff] [blame] | 62 | { |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 63 | return sprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]); |
Glenn L McGrath | 6b5bd0e | 2002-12-08 22:17:54 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 66 | |
| 67 | /* really simple implementation, just count the bits */ |
| 68 | static int mton(struct in_addr *mask) |
| 69 | { |
| 70 | int i; |
| 71 | /* note: mask will always be in network byte order, so |
| 72 | * there are no endian issues */ |
| 73 | for (i = 31; i >= 0 && !((mask->s_addr >> i) & 1); i--); |
| 74 | return i + 1; |
Glenn L McGrath | 6b5bd0e | 2002-12-08 22:17:54 +0000 | [diff] [blame] | 75 | } |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 76 | |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 77 | |
| 78 | /* Fill dest with the text of option 'option'. */ |
| 79 | static void fill_options(char *dest, unsigned char *option, struct dhcp_option *type_p) |
| 80 | { |
| 81 | int type, optlen; |
| 82 | u_int16_t val_u16; |
| 83 | int16_t val_s16; |
| 84 | u_int32_t val_u32; |
| 85 | int32_t val_s32; |
| 86 | int len = option[OPT_LEN - 2]; |
| 87 | |
| 88 | dest += sprintf(dest, "%s=", type_p->name); |
| 89 | |
| 90 | type = type_p->flags & TYPE_MASK; |
| 91 | optlen = option_lengths[type]; |
| 92 | for(;;) { |
| 93 | switch (type) { |
| 94 | case OPTION_IP_PAIR: |
| 95 | dest += sprintip(dest, "", option); |
| 96 | *(dest++) = '/'; |
| 97 | option += 4; |
| 98 | optlen = 4; |
| 99 | case OPTION_IP: /* Works regardless of host byte order. */ |
| 100 | dest += sprintip(dest, "", option); |
| 101 | break; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 102 | case OPTION_BOOLEAN: |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 103 | dest += sprintf(dest, *option ? "yes" : "no"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 104 | break; |
| 105 | case OPTION_U8: |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 106 | dest += sprintf(dest, "%u", *option); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 107 | break; |
| 108 | case OPTION_U16: |
| 109 | memcpy(&val_u16, option, 2); |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 110 | dest += sprintf(dest, "%u", ntohs(val_u16)); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 111 | break; |
| 112 | case OPTION_S16: |
| 113 | memcpy(&val_s16, option, 2); |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 114 | dest += sprintf(dest, "%d", ntohs(val_s16)); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 115 | break; |
| 116 | case OPTION_U32: |
| 117 | memcpy(&val_u32, option, 4); |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 118 | dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32)); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 119 | break; |
| 120 | case OPTION_S32: |
| 121 | memcpy(&val_s32, option, 4); |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 122 | dest += sprintf(dest, "%ld", (long) ntohl(val_s32)); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 123 | break; |
| 124 | case OPTION_STRING: |
| 125 | memcpy(dest, option, len); |
| 126 | dest[len] = '\0'; |
| 127 | return; /* Short circuit this case */ |
| 128 | } |
| 129 | option += optlen; |
| 130 | len -= optlen; |
| 131 | if (len <= 0) break; |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 132 | dest += sprintf(dest, " "); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | |
| 137 | static char *find_env(const char *prefix, char *defaultstr) |
| 138 | { |
| 139 | extern char **environ; |
| 140 | char **ptr; |
| 141 | const int len = strlen(prefix); |
| 142 | |
| 143 | for (ptr = environ; *ptr != NULL; ptr++) { |
| 144 | if (strncmp(prefix, *ptr, len) == 0) |
| 145 | return *ptr; |
| 146 | } |
| 147 | return defaultstr; |
| 148 | } |
| 149 | |
| 150 | |
| 151 | /* put all the paramaters into an environment */ |
| 152 | static char **fill_envp(struct dhcpMessage *packet) |
| 153 | { |
| 154 | int num_options = 0; |
| 155 | int i, j; |
| 156 | char **envp; |
| 157 | unsigned char *temp; |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 158 | struct in_addr subnet; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 159 | char over = 0; |
| 160 | |
| 161 | if (packet == NULL) |
| 162 | num_options = 0; |
| 163 | else { |
| 164 | for (i = 0; options[i].code; i++) |
| 165 | if (get_option(packet, options[i].code)) |
| 166 | num_options++; |
| 167 | if (packet->siaddr) num_options++; |
| 168 | if ((temp = get_option(packet, DHCP_OPTION_OVER))) |
| 169 | over = *temp; |
| 170 | if (!(over & FILE_FIELD) && packet->file[0]) num_options++; |
| 171 | if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++; |
| 172 | } |
| 173 | |
| 174 | envp = xmalloc((num_options + 5) * sizeof(char *)); |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 175 | j = 0; |
| 176 | envp[j++] = xmalloc(sizeof("interface=") + strlen(client_config.interface)); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 177 | sprintf(envp[0], "interface=%s", client_config.interface); |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 178 | envp[j++] = find_env("PATH", "PATH=/bin:/usr/bin:/sbin:/usr/sbin"); |
| 179 | envp[j++] = find_env("HOME", "HOME=/"); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 180 | |
| 181 | if (packet == NULL) { |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 182 | envp[j++] = NULL; |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 183 | return envp; |
| 184 | } |
| 185 | |
Russ Dill | 1eb7a17 | 2002-12-11 21:12:45 +0000 | [diff] [blame^] | 186 | envp[j] = xmalloc(sizeof("ip=255.255.255.255")); |
| 187 | sprintip(envp[j++], "ip=", (unsigned char *) &packet->yiaddr); |
| 188 | |
| 189 | |
| 190 | for (i = 0; options[i].code; i++) { |
| 191 | if (!(temp = get_option(packet, options[i].code))) |
| 192 | continue; |
| 193 | envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2], &options[i]) + strlen(options[i].name) + 2); |
| 194 | fill_options(envp[j++], temp, &options[i]); |
| 195 | |
| 196 | /* Fill in a subnet bits option for things like /24 */ |
| 197 | if (options[i].code == DHCP_SUBNET) { |
| 198 | envp[j] = xmalloc(sizeof("mask=32")); |
| 199 | memcpy(&subnet, temp, 4); |
| 200 | sprintf(envp[j++], "mask=%d", mton(&subnet)); |
Russ Dill | 61fb489 | 2002-10-14 21:41:28 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | if (packet->siaddr) { |
| 204 | envp[j] = xmalloc(sizeof("siaddr=255.255.255.255")); |
| 205 | sprintip(envp[j++], "siaddr=", (unsigned char *) &packet->siaddr); |
| 206 | } |
| 207 | if (!(over & FILE_FIELD) && packet->file[0]) { |
| 208 | /* watch out for invalid packets */ |
| 209 | packet->file[sizeof(packet->file) - 1] = '\0'; |
| 210 | envp[j] = xmalloc(sizeof("boot_file=") + strlen(packet->file)); |
| 211 | sprintf(envp[j++], "boot_file=%s", packet->file); |
| 212 | } |
| 213 | if (!(over & SNAME_FIELD) && packet->sname[0]) { |
| 214 | /* watch out for invalid packets */ |
| 215 | packet->sname[sizeof(packet->sname) - 1] = '\0'; |
| 216 | envp[j] = xmalloc(sizeof("sname=") + strlen(packet->sname)); |
| 217 | sprintf(envp[j++], "sname=%s", packet->sname); |
| 218 | } |
| 219 | envp[j] = NULL; |
| 220 | return envp; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /* Call a script with a par file and env vars */ |
| 225 | void run_script(struct dhcpMessage *packet, const char *name) |
| 226 | { |
| 227 | int pid; |
| 228 | char **envp; |
| 229 | |
| 230 | if (client_config.script == NULL) |
| 231 | return; |
| 232 | |
| 233 | /* call script */ |
| 234 | pid = fork(); |
| 235 | if (pid) { |
| 236 | waitpid(pid, NULL, 0); |
| 237 | return; |
| 238 | } else if (pid == 0) { |
| 239 | envp = fill_envp(packet); |
| 240 | |
| 241 | /* close fd's? */ |
| 242 | |
| 243 | /* exec script */ |
| 244 | DEBUG(LOG_INFO, "execle'ing %s", client_config.script); |
| 245 | execle(client_config.script, client_config.script, |
| 246 | name, NULL, envp); |
| 247 | LOG(LOG_ERR, "script %s failed: %s", |
| 248 | client_config.script, strerror(errno)); |
| 249 | exit(1); |
| 250 | } |
| 251 | } |