1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
/* This file is part of the KDE Libraries
Copyright (c) 2001 Malte Starostik <malte@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <sys/types.h>
#include <grp.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <time.h>
#include <netdb.h>
#include <netinet/in.h>
#include "dhcp.h"
#ifndef INADDR_NONE /* some OSes don't define this */
#define INADDR_NONE -1
#endif
int set_gid(gid_t);
int set_uid(uid_t);
int get_port(const char *);
int init_socket(void);
uint32_t send_request(int);
void get_reply(int, uint32_t);
int set_gid(gid_t gid)
{
#ifdef HAVE_SHORTSETGROUPS
short x[2];
x[0] = gid;
x[1] = 73; /* catch errors */
if (setgroups(1, x) == -1)
return -1;
#else
if (setgroups(1, &gid) == -1)
return -1;
#endif
return setgid(gid); /* _should_ be redundant, but on some systems it isn't */
}
int set_uid(uid_t uid)
{
return setuid(uid);
}
/* All functions below do an exit(1) on the slightest error */
/* Returns the UDP port number for the given service name */
int get_port(const char *service)
{
struct servent *serv = getservbyname(service, "udp");
if (serv == NULL)
exit(1);
return serv->s_port;
}
/* Opens the UDP socket, binds to the bootpc port and drops root privileges */
int init_socket()
{
struct sockaddr_in addr;
struct protoent *proto;
int sock;
int bcast = 1;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = get_port("bootpc");
if ((proto = getprotobyname("udp")) == NULL ||
(sock = socket(AF_INET, SOCK_DGRAM, proto->p_proto)) == -1)
exit(1);
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &bcast, sizeof(bcast)) == -1 ||
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &bcast, sizeof(bcast)) == -1 ||
bind(sock, (struct sockaddr *)&addr, sizeof(addr)) == -1)
exit(1);
if (set_gid(getgid()) != 0 || /* we don't need it anymore */
set_uid(getuid()) != 0)
exit(1);
return sock;
}
/* Fills the DHCPINFORM request packet, returns the transaction id */
uint32_t send_request(int sock)
{
char hostname[256];
struct hostent *hent;
struct sockaddr_in addr;
struct dhcp_msg request;
uint8_t *offs = request.options;
hostname[0] = '\0';
hostname[255] = '\0';
if (gethostname(hostname, 255) == -1 ||
strlen(hostname) == 0 ||
(hent = gethostbyname(hostname)) == NULL)
exit(1);
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_NONE;
addr.sin_port = get_port("bootps");
memset(&request, 0, sizeof(request));
request.op = DHCP_BOOTREQUEST;
srand(time(NULL));
request.xid = rand();
request.ciaddr = *(uint32_t*)*hent->h_addr_list;
*offs++ = DHCP_MAGIC1;
*offs++ = DHCP_MAGIC2;
*offs++ = DHCP_MAGIC3;
*offs++ = DHCP_MAGIC4;
*offs++ = DHCP_OPT_MSGTYPE;
*offs++ = 1; /* length */
*offs++ = DHCP_INFORM;
*offs++ = DHCP_OPT_PARAMREQ;
*offs++ = 1; /* length */
*offs++ = DHCP_OPT_WPAD;
*offs++ = DHCP_OPT_END;
if (sendto(sock, &request, sizeof(request), 0,
(struct sockaddr *)&addr, sizeof(addr)) != sizeof(request))
exit(1);
return request.xid;
}
/* Reads the reply from the socket, checks it and outputs the URL to STDOUT */
void get_reply(int sock, uint32_t xid)
{
struct dhcp_msg reply;
int len;
char wpad[DHCP_OPT_LEN + 1];
uint8_t wpad_len;
uint8_t *offs = reply.options;
uint8_t *end;
if ((len = recvfrom(sock, &reply, sizeof(reply), 0, NULL, NULL)) <= 0)
exit(1);
end = (uint8_t *)&reply + len;
if (end < offs + 4 ||
end > &reply.options[DHCP_OPT_LEN] ||
reply.op != DHCP_BOOTREPLY ||
reply.xid != xid ||
*offs++ != DHCP_MAGIC1 ||
*offs++ != DHCP_MAGIC2 ||
*offs++ != DHCP_MAGIC3 ||
*offs++ != DHCP_MAGIC4)
exit(1);
for ( ; offs < end - 1; offs += *offs+1)
{
switch (*offs++)
{
case DHCP_OPT_END:
exit(1);
case DHCP_OPT_MSGTYPE:
if (*offs != 1 || (offs >= end - 1) || *(offs + 1) != DHCP_ACK)
exit(1);
break;
case DHCP_OPT_WPAD:
memset(wpad, 0, sizeof(wpad));
wpad_len = *offs++;
if (offs >= end)
exit(1);
if (wpad_len > end - offs)
wpad_len = end - offs;
strncpy(wpad, (char *)offs, wpad_len);
wpad[wpad_len] = 0;
printf("%s\n", wpad);
close(sock);
exit(0);
}
}
exit(1);
}
int main()
{
fd_set rfds;
struct timeval tv;
int sock;
uint32_t xid;
sock = init_socket();
xid = send_request(sock);
FD_ZERO(&rfds);
FD_SET(sock, &rfds);
tv.tv_sec = 5;
tv.tv_usec = 0;
if (select(sock + 1, &rfds, NULL, NULL, &tv) == 1 && FD_ISSET(sock, &rfds))
get_reply(sock, xid);
close(sock);
exit(1);
}
/* vim: ts=4 sw=4 noet
*/
|