File: pxe.cc

package info (click to toggle)
pxe 1.4.2-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 444 kB
  • ctags: 208
  • sloc: sh: 2,813; cpp: 2,353; makefile: 84
file content (335 lines) | stat: -rw-r--r-- 8,691 bytes parent folder | download | duplicates (2)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * PXE daemon - enable the remote booting of PXE enabled machines.
 * Copyright (C) 2000 Tim Hurman (kano@kano.org.uk)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */
/******************************************************************************
 * pxe.c - a pxe server, made better than intel's hack                        *
 ******************************************************************************/

#include <sys/types.h>
#include <iostream>
#include <fstream>

#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <signal.h>
#include <sys/signal.h>
#include <unistd.h>
#include <pwd.h>

#include "sock.h"
#include "logfile.h"
#include "packetstore.h"
#include "options.h"
#include "sysexception.h"
#include "posix_signal.h"
#include "autoconf.h"

int service_requests=1;
int watchchld = 1;

#define BUFFER_SZ 2048

/*
 * can only bind to one address, otherwise multicast is inherently
 * messed up. There were big problems with the multicast address
 * as it can only send multicast when bound to 0.0.0.0
 */

/******************************************************************************
 * Usage - show the usage and exit                                            *
 ******************************************************************************/
void Usage(char *progname)
{
	std::cerr << "Usage: " << progname << " [-c <configfile>] [-d]\n";
	std::cerr << "Tim Hurman (kano@kano.org.uk) " << __DATE__ << "\n";
	exit(1);
}


/******************************************************************************
 * HandleSig - handle some standard signals                                   *
 ******************************************************************************/
void HandleSig(int signo)
{
	service_requests=0;
}


/******************************************************************************
 * HandleSigChld - handle the death of a child                                *
 ******************************************************************************/
void HandleSigChld(int signo)
{
	int status;

	while(waitpid(-1, &status, WNOHANG) > 0)
		watchchld = 0;
}


/******************************************************************************
 * StartPxeService - service incoming pxe requests                            *
 ******************************************************************************/
int StartPxeService(const char *configfile)
{
	LogFile logger;
	Options *opts = NULL;
	Sock *connection = NULL;
	Signal sig(&logger);
	PacketStore request(&logger);
	PacketStore reply(&logger);
	PacketStore test(&logger);
	int retval = 0;
	int recvlen;
	char *buf;
	struct sockaddr_in server_addr, client_addr;
	bootp_packet_t *pkt;

	// register some signal handlers
	sig.Set(SIGINT, HandleSig);
	sig.Set(SIGTERM, HandleSig);
	sig.Set(SIGHUP, (void(*)(int))SIG_IGN);

	// assign memory
	buf = new char[BUFFER_SZ];

	// read the config file
	std::cout << "Opening " << configfile << "\n";

	try {
		opts = new Options(&logger, configfile);
	} catch (SysException *e) {
		std::cerr << "An error occurred, please check the logfile\n";
		if(e->HaveMessage())
			logger.Event(LEVEL_FATAL, e->GetWhere(), 1, e->GetMessage());
		else
			logger.Event(LEVEL_FATAL, e->GetWhere(), 1, strerror(e->GetErrno()));
		delete e;
		retval = 1;
		goto MainCleanup;
	}

	// open the socket
	try {
		connection = new Sock(&logger, opts->GetInterface(), opts->GetPort());
		connection->SetDefAddr(opts->GetDefAddr());
	
		if(opts->UseBroadcast())
			connection->AllowBroadcast();

		if(opts->UseMulticast())
			connection->JoinMulticast(opts->GetMulticast());

	} catch (SysException *e) {
		std::cerr << "An error occurred, please check the logfile\n";
		if(e->HaveMessage())
			logger.Event(LEVEL_FATAL, e->GetWhere(), 1, e->GetMessage());
		else
			logger.Event(LEVEL_FATAL, e->GetWhere(), 1, strerror(e->GetErrno()));
		delete e;
		retval = 1;
		goto MainCleanup;
	}
	
	// receive packets
	while(service_requests)
	{
		try {
			// blank the reply socket
			reply.Initalise();
			request.Initalise();

			// need try statement
			recvlen = connection->Read((unsigned char*)buf, BUFFER_SZ,
			  &client_addr, &server_addr);
		
			if(recvlen <= 0)
				goto service_requests_next;

			// parse the request
			request.ReadPacket((unsigned char*)buf, recvlen);
			request.SetAddress(&client_addr);
			std::cout << "\n---Request---\n" << request << "\n";

			if(reply.MakeReply(request, opts, &server_addr) == -1)
				goto service_requests_next;

			// print the packet
			std::cout  << "\n---Reply---\n\n" << reply << "\n";
			pkt = reply.PackPacket();

			// send the packet back to the client
			connection->Send((unsigned char*)pkt->data, pkt->len,
			  &client_addr, &server_addr);

			delete [] pkt->data;
			delete pkt;

			service_requests_next:
			recvlen=recvlen;
		} catch (SysException *e) {
			std::cerr << "An error occurred, please check the logfile\n";
			if(e->HaveMessage())
				logger.Event(LEVEL_FATAL, e->GetWhere(), 1, e->GetMessage());
			else
				logger.Event(LEVEL_FATAL, e->GetWhere(), 1,
				  strerror(e->GetErrno()));
			delete e;
			retval = 1;
		}
	}

	// tidy up and exit
	MainCleanup:
	if(opts != NULL)
		delete opts;
	if(connection != NULL)
		delete connection;
	delete[] buf;
	unlink(LOCKFILE);
	return(retval);
}


/******************************************************************************
 * main - kick things off and do cool things                                  *
 ******************************************************************************/
int main(int argc, char **argv)
{
	int chk;
	char pidnum[8];
	int _debug, c, errflg;
	const char *configfile=PXECONFIGFILE;
	std::fstream debug;

	errflg = _debug = 0;
	// get the command line opts
	while ((c = getopt(argc, argv, "dc:")) != EOF)
		switch(c)
		{
		case 'c':
			configfile = optarg;
			break;
		case 'd':
			_debug = 1;
			break;
		default:
			errflg++;
		}

	// errors?
	if(errflg)
		Usage(argv[0]);

	// check the config file exists
	debug.open(configfile, std::ios::in);
	if (!debug.is_open()) {
		std::cerr << "Unable to open the config file\n";
		exit (1);
	}
	debug.close();

	// redirect the file descriptors
	if (0 == _debug) {
		debug.open("/dev/null", std::ios::out);
		std::cout.rdbuf(debug.rdbuf());
		std::cerr.rdbuf(debug.rdbuf());
		debug.close();
		debug.open("/dev/zero", std::ios::in);
		std::cin.rdbuf(debug.rdbuf());
		debug.close();
	}


	// set the UID/GID to a low user
#ifndef NO_SUID
	struct passwd *pw;
	pw = getpwnam(SETUID);

	if(NULL == pw)
		std::cout << "Unable to find passwd entry for " << SETUID
		     << ", continuing with user id " << getuid() << "\n";
	else
	{
		if((-1 == setgid(pw->pw_gid)) || (-1 == setegid(pw->pw_gid)))
			std::cout << "Unable to change group id, continuing with group id "
			     << getgid() << "\n";
		if((-1 == setuid(pw->pw_uid)) || (-1 == seteuid(pw->pw_uid)))
			std::cout << "Unable to change user id, continuing with user id "
			     << getuid() << "\n";
	}
#endif

	// check to see if the daemon is already running
	chk = open(LOCKFILE, O_WRONLY|O_CREAT|O_EXCL, 0644);
	if(-1 == chk)
	{
		std::cerr << "PXE daemon already running\n";
		return(-1);
	}

	// if not in debug mode, fork and go
	if (0 == _debug) {
		signal(SIGCHLD, SIG_IGN);

		// set up the daemon
		switch (fork()) {
		case -1:
			std::cerr << "Unable to fork child\n";
			exit(-1);
		case 0:
			// become the process group session leader
			setsid();

			// the second fork
			switch(fork()) {
			case -1:
				std::cerr << "Unable to fork child\n";
				exit(-1);
			case 0:
				// change the working dir
				chdir("/");

				// clear the mask
				umask(0);

				// write out the pid
				sprintf(pidnum, "%ld", (long)getpid());
				if(write(chk, pidnum, strlen(pidnum)) !=
				  (ssize_t)strlen(pidnum)) {
					std::cerr << "Unable to write lockfile\n";
					exit(-1);
				}
				close(chk);

				StartPxeService(configfile);

				exit(0);
			}
			exit(0);
		}

	} else { // debug
		StartPxeService(configfile);
	}
	
	return(0);
}