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