summaryrefslogtreecommitdiffstats
path: root/net/pxe/files/patch-pxe.cc
blob: 58332071f742b87b3f3dc8e1bf36664c1ac681d1 (plain)
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
--- pxe.cc.orig	2003-02-02 12:39:26 UTC
+++ pxe.cc
@@ -208,6 +208,31 @@ int StartPxeService(const char *configfi
 	return(retval);
 }
 
+/******************************************************************************
+ * DoSetUID - set uid and gid                                                 *
+ ******************************************************************************/
+void DoSetUID()
+{
+	// 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
+}
+
 
 /******************************************************************************
  * main - kick things off and do cool things                                  *
@@ -247,6 +272,15 @@ int main(int argc, char **argv)
 	}
 	debug.close();
 
+	// 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, or left-over pid file " << LOCKFILE << " exists?\n";
+		std::cerr << "Aborting startup.\n";
+		return(-1);
+	}
+
 	// redirect the file descriptors
 	if (0 == _debug) {
 		debug.open("/dev/null", std::ios::out);
@@ -258,34 +292,6 @@ int main(int argc, char **argv)
 		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);
@@ -320,6 +326,7 @@ int main(int argc, char **argv)
 				}
 				close(chk);
 
+				DoSetUID;
 				StartPxeService(configfile);
 
 				exit(0);
@@ -328,6 +335,7 @@ int main(int argc, char **argv)
 		}
 
 	} else { // debug
+		DoSetUID;
 		StartPxeService(configfile);
 	}
 	
OpenPOWER on IntegriCloud