blob: e08d765bbaf788edebf69db421e5d59acf253d46 [file] [log] [blame]
Russ Dill61fb4892002-10-14 21:41:28 +00001/* script.c
2 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003 * Functions to call the DHCP client notification scripts
Russ Dill61fb4892002-10-14 21:41:28 +00004 *
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 Dill61fb4892002-10-14 21:41:28 +000031
32#include "options.h"
33#include "dhcpd.h"
34#include "dhcpc.h"
Glenn L McGrath24833432003-06-10 17:22:49 +000035#include "common.h"
"Vladimir N. Oleynik"2f9c30a2005-09-22 15:07:33 +000036#include "script.h"
Russ Dill61fb4892002-10-14 21:41:28 +000037
38/* get a rough idea of how long an option will be (rounding up...) */
Glenn L McGrath24833432003-06-10 17:22:49 +000039static const int max_option_length[] = {
Russ Dill61fb4892002-10-14 21:41:28 +000040 [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 McGrath24833432003-06-10 17:22:49 +000052static inline int upper_length(int length, int opt_index)
Russ Dill61fb4892002-10-14 21:41:28 +000053{
Glenn L McGrath24833432003-06-10 17:22:49 +000054 return max_option_length[opt_index] *
55 (length / option_lengths[opt_index]);
Russ Dill61fb4892002-10-14 21:41:28 +000056}
57
58
Eric Andersenad953732004-01-30 23:45:53 +000059static int sprintip(char *dest, char *pre, uint8_t *ip)
Glenn L McGrath6b5bd0e2002-12-08 22:17:54 +000060{
Russ Dill4e864a32003-12-18 22:25:38 +000061 return sprintf(dest, "%s%d.%d.%d.%d", pre, ip[0], ip[1], ip[2], ip[3]);
Glenn L McGrath6b5bd0e2002-12-08 22:17:54 +000062}
63
Russ Dill1eb7a172002-12-11 21:12:45 +000064
65/* really simple implementation, just count the bits */
66static int mton(struct in_addr *mask)
67{
68 int i;
Russ Dill858fad72003-02-12 22:20:19 +000069 unsigned long bits = ntohl(mask->s_addr);
70 /* too bad one can't check the carry bit, etc in c bit
71 * shifting */
72 for (i = 0; i < 32 && !((bits >> i) & 1); i++);
73 return 32 - i;
Glenn L McGrath6b5bd0e2002-12-08 22:17:54 +000074}
Russ Dill1eb7a172002-12-11 21:12:45 +000075
Russ Dill61fb4892002-10-14 21:41:28 +000076
77/* Fill dest with the text of option 'option'. */
Eric Andersenad953732004-01-30 23:45:53 +000078static void fill_options(char *dest, uint8_t *option, struct dhcp_option *type_p)
Russ Dill61fb4892002-10-14 21:41:28 +000079{
80 int type, optlen;
Eric Andersenad953732004-01-30 23:45:53 +000081 uint16_t val_u16;
Russ Dill61fb4892002-10-14 21:41:28 +000082 int16_t val_s16;
Eric Andersenad953732004-01-30 23:45:53 +000083 uint32_t val_u32;
Russ Dill61fb4892002-10-14 21:41:28 +000084 int32_t val_s32;
85 int len = option[OPT_LEN - 2];
86
87 dest += sprintf(dest, "%s=", type_p->name);
88
89 type = type_p->flags & TYPE_MASK;
90 optlen = option_lengths[type];
91 for(;;) {
92 switch (type) {
93 case OPTION_IP_PAIR:
Russ Dill4e864a32003-12-18 22:25:38 +000094 dest += sprintip(dest, "", option);
Russ Dill61fb4892002-10-14 21:41:28 +000095 *(dest++) = '/';
96 option += 4;
97 optlen = 4;
98 case OPTION_IP: /* Works regardless of host byte order. */
Russ Dill4e864a32003-12-18 22:25:38 +000099 dest += sprintip(dest, "", option);
Russ Dill61fb4892002-10-14 21:41:28 +0000100 break;
Russ Dill61fb4892002-10-14 21:41:28 +0000101 case OPTION_BOOLEAN:
Russ Dill1eb7a172002-12-11 21:12:45 +0000102 dest += sprintf(dest, *option ? "yes" : "no");
Russ Dill61fb4892002-10-14 21:41:28 +0000103 break;
104 case OPTION_U8:
Russ Dill1eb7a172002-12-11 21:12:45 +0000105 dest += sprintf(dest, "%u", *option);
Russ Dill61fb4892002-10-14 21:41:28 +0000106 break;
107 case OPTION_U16:
108 memcpy(&val_u16, option, 2);
Russ Dill1eb7a172002-12-11 21:12:45 +0000109 dest += sprintf(dest, "%u", ntohs(val_u16));
Russ Dill61fb4892002-10-14 21:41:28 +0000110 break;
111 case OPTION_S16:
112 memcpy(&val_s16, option, 2);
Russ Dill1eb7a172002-12-11 21:12:45 +0000113 dest += sprintf(dest, "%d", ntohs(val_s16));
Russ Dill61fb4892002-10-14 21:41:28 +0000114 break;
115 case OPTION_U32:
116 memcpy(&val_u32, option, 4);
Russ Dill1eb7a172002-12-11 21:12:45 +0000117 dest += sprintf(dest, "%lu", (unsigned long) ntohl(val_u32));
Russ Dill61fb4892002-10-14 21:41:28 +0000118 break;
119 case OPTION_S32:
120 memcpy(&val_s32, option, 4);
Russ Dill1eb7a172002-12-11 21:12:45 +0000121 dest += sprintf(dest, "%ld", (long) ntohl(val_s32));
Russ Dill61fb4892002-10-14 21:41:28 +0000122 break;
123 case OPTION_STRING:
124 memcpy(dest, option, len);
125 dest[len] = '\0';
126 return; /* Short circuit this case */
127 }
128 option += optlen;
129 len -= optlen;
130 if (len <= 0) break;
Russ Dill1eb7a172002-12-11 21:12:45 +0000131 dest += sprintf(dest, " ");
Russ Dill61fb4892002-10-14 21:41:28 +0000132 }
133}
134
135
Eric Andersenaff114c2004-04-14 17:51:38 +0000136/* put all the parameters into an environment */
Russ Dill61fb4892002-10-14 21:41:28 +0000137static char **fill_envp(struct dhcpMessage *packet)
138{
139 int num_options = 0;
140 int i, j;
141 char **envp;
Eric Andersenad953732004-01-30 23:45:53 +0000142 uint8_t *temp;
Russ Dill1eb7a172002-12-11 21:12:45 +0000143 struct in_addr subnet;
Russ Dill61fb4892002-10-14 21:41:28 +0000144 char over = 0;
145
146 if (packet == NULL)
147 num_options = 0;
148 else {
Russ Dill4a9e34c2003-12-15 22:09:36 +0000149 for (i = 0; dhcp_options[i].code; i++)
Russ Dilld242e492003-12-24 19:57:13 +0000150 if (get_option(packet, dhcp_options[i].code)) {
Russ Dill61fb4892002-10-14 21:41:28 +0000151 num_options++;
Russ Dilld242e492003-12-24 19:57:13 +0000152 if (dhcp_options[i].code == DHCP_SUBNET)
153 num_options++; /* for mton */
154 }
Russ Dill61fb4892002-10-14 21:41:28 +0000155 if (packet->siaddr) num_options++;
156 if ((temp = get_option(packet, DHCP_OPTION_OVER)))
157 over = *temp;
158 if (!(over & FILE_FIELD) && packet->file[0]) num_options++;
Russ Dill4e864a32003-12-18 22:25:38 +0000159 if (!(over & SNAME_FIELD) && packet->sname[0]) num_options++;
Russ Dill61fb4892002-10-14 21:41:28 +0000160 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000161
Russ Dill4e864a32003-12-18 22:25:38 +0000162 envp = xcalloc(sizeof(char *), num_options + 5);
Russ Dill1eb7a172002-12-11 21:12:45 +0000163 j = 0;
Glenn L McGrath24833432003-06-10 17:22:49 +0000164 asprintf(&envp[j++], "interface=%s", client_config.interface);
Russ Dill4e864a32003-12-18 22:25:38 +0000165 asprintf(&envp[j++], "%s=%s", "PATH",
166 getenv("PATH") ? : "/bin:/usr/bin:/sbin:/usr/sbin");
167 asprintf(&envp[j++], "%s=%s", "HOME", getenv("HOME") ? : "/");
Russ Dill61fb4892002-10-14 21:41:28 +0000168
Russ Dill4e864a32003-12-18 22:25:38 +0000169 if (packet == NULL) return envp;
Russ Dill61fb4892002-10-14 21:41:28 +0000170
Russ Dill4e864a32003-12-18 22:25:38 +0000171 envp[j] = xmalloc(sizeof("ip=255.255.255.255"));
Eric Andersenad953732004-01-30 23:45:53 +0000172 sprintip(envp[j++], "ip=", (uint8_t *) &packet->yiaddr);
Russ Dill1eb7a172002-12-11 21:12:45 +0000173
174
Russ Dill4a9e34c2003-12-15 22:09:36 +0000175 for (i = 0; dhcp_options[i].code; i++) {
176 if (!(temp = get_option(packet, dhcp_options[i].code)))
Russ Dill1eb7a172002-12-11 21:12:45 +0000177 continue;
Russ Dill4a9e34c2003-12-15 22:09:36 +0000178 envp[j] = xmalloc(upper_length(temp[OPT_LEN - 2],
179 dhcp_options[i].flags & TYPE_MASK) + strlen(dhcp_options[i].name) + 2);
180 fill_options(envp[j++], temp, &dhcp_options[i]);
Russ Dill1eb7a172002-12-11 21:12:45 +0000181
182 /* Fill in a subnet bits option for things like /24 */
Russ Dill4a9e34c2003-12-15 22:09:36 +0000183 if (dhcp_options[i].code == DHCP_SUBNET) {
Russ Dill1eb7a172002-12-11 21:12:45 +0000184 memcpy(&subnet, temp, 4);
Glenn L McGrath24833432003-06-10 17:22:49 +0000185 asprintf(&envp[j++], "mask=%d", mton(&subnet));
Russ Dill61fb4892002-10-14 21:41:28 +0000186 }
187 }
188 if (packet->siaddr) {
Russ Dill4e864a32003-12-18 22:25:38 +0000189 envp[j] = xmalloc(sizeof("siaddr=255.255.255.255"));
Eric Andersenad953732004-01-30 23:45:53 +0000190 sprintip(envp[j++], "siaddr=", (uint8_t *) &packet->siaddr);
Russ Dill61fb4892002-10-14 21:41:28 +0000191 }
192 if (!(over & FILE_FIELD) && packet->file[0]) {
193 /* watch out for invalid packets */
194 packet->file[sizeof(packet->file) - 1] = '\0';
Glenn L McGrath24833432003-06-10 17:22:49 +0000195 asprintf(&envp[j++], "boot_file=%s", packet->file);
Russ Dill61fb4892002-10-14 21:41:28 +0000196 }
197 if (!(over & SNAME_FIELD) && packet->sname[0]) {
198 /* watch out for invalid packets */
199 packet->sname[sizeof(packet->sname) - 1] = '\0';
Glenn L McGrath24833432003-06-10 17:22:49 +0000200 asprintf(&envp[j++], "sname=%s", packet->sname);
Russ Dilld242e492003-12-24 19:57:13 +0000201 }
Russ Dill61fb4892002-10-14 21:41:28 +0000202 return envp;
203}
204
205
206/* Call a script with a par file and env vars */
207void run_script(struct dhcpMessage *packet, const char *name)
208{
209 int pid;
Russ Dill57f49d32004-05-19 07:45:47 +0000210 char **envp, **curr;
Russ Dill61fb4892002-10-14 21:41:28 +0000211
212 if (client_config.script == NULL)
213 return;
214
Russ Dilla8992632003-12-24 19:31:08 +0000215 DEBUG(LOG_INFO, "vforking and execle'ing %s", client_config.script);
216
Russ Dill57f49d32004-05-19 07:45:47 +0000217 envp = fill_envp(packet);
Russ Dill61fb4892002-10-14 21:41:28 +0000218 /* call script */
Russ Dill62419df2003-12-15 22:11:26 +0000219 pid = vfork();
Russ Dill61fb4892002-10-14 21:41:28 +0000220 if (pid) {
221 waitpid(pid, NULL, 0);
Russ Dill57f49d32004-05-19 07:45:47 +0000222 for (curr = envp; *curr; curr++) free(*curr);
223 free(envp);
Russ Dill61fb4892002-10-14 21:41:28 +0000224 return;
225 } else if (pid == 0) {
Russ Dill61fb4892002-10-14 21:41:28 +0000226 /* close fd's? */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000227
Russ Dill61fb4892002-10-14 21:41:28 +0000228 /* exec script */
Russ Dill61fb4892002-10-14 21:41:28 +0000229 execle(client_config.script, client_config.script,
230 name, NULL, envp);
Glenn L McGrath24833432003-06-10 17:22:49 +0000231 LOG(LOG_ERR, "script %s failed: %m", client_config.script);
Russ Dill61fb4892002-10-14 21:41:28 +0000232 exit(1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000233 }
Russ Dill61fb4892002-10-14 21:41:28 +0000234}