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