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