(file) Return to main.c CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Executor

  1 kumpf 1.2 /*
  2           //%2006////////////////////////////////////////////////////////////////////////
  3           //
  4           // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  5           // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  6           // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  7           // IBM Corp.; EMC Corporation, The Open Group.
  8           // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  9           // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
 10           // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 11           // EMC Corporation; VERITAS Software Corporation; The Open Group.
 12           // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 13           // EMC Corporation; Symantec Corporation; The Open Group.
 14           //
 15           // Permission is hereby granted, free of charge, to any person obtaining a copy
 16           // of this software and associated documentation files (the "Software"), to
 17           // deal in the Software without restriction, including without limitation the
 18           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 19           // sell copies of the Software, and to permit persons to whom the Software is
 20           // furnished to do so, subject to the following conditions:
 21           // 
 22 kumpf 1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 23           // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 24           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 25           // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 26           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 27           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 28           // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 29           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 30           //
 31           //%/////////////////////////////////////////////////////////////////////////////
 32           */
 33           #include <stdio.h>
 34           #include <sys/types.h>
 35           #include <sys/stat.h>
 36           #include <unistd.h>
 37           #include <string.h>
 38           #include "Config.h"
 39           #include "Child.h"
 40           #include "Parent.h"
 41           #include "User.h"
 42           #include "Fatal.h"
 43 kumpf 1.2 #include "Process.h"
 44           #include "Path.h"
 45           #include "Globals.h"
 46           #include "Socket.h"
 47           #include "Strlcpy.h"
 48           #include "Strlcat.h"
 49           #include "Log.h"
 50           #include "Policy.h"
 51           #include "Macro.h"
 52           #include "Assert.h"
 53           #include "Options.h"
 54           
 55           /*
 56           **==============================================================================
 57           **
 58           ** GetServerUser
 59           **
 60           **     Determine which user to run CIMSERVERMAIN as.
 61           **
 62           **==============================================================================
 63           */
 64 kumpf 1.2 
 65 kumpf 1.3 int GetServerUser(const char** userName, int* uid, int* gid)
 66 kumpf 1.2 {
 67 kumpf 1.3     *userName = PEGASUS_CIMSERVERMAIN_USER;
 68 kumpf 1.2 
 69 kumpf 1.3     if (GetUserInfo(*userName, uid, gid) != 0)
 70 kumpf 1.2     {
 71                   Fatal(FL,
 72                       "The %s user \"%s\" does not exist.",
 73 kumpf 1.3             CIMSERVERMAIN, *userName);
 74 kumpf 1.2     }
 75           
 76               return 0;
 77           }
 78           
 79           /*
 80           **==============================================================================
 81           **
 82           ** ExecShutdown()
 83           **
 84           **==============================================================================
 85           */
 86           
 87           void ExecShutdown()
 88           {
 89               char* tmpArgv[3];
 90               const char* cimshutdownPath;
 91               const char* shutdownTimeout;
 92           
 93               /* Get shutdownTimeout configuration parameter. */
 94           
 95 kumpf 1.2     if ((shutdownTimeout = FindMacro("shutdownTimeout")) == NULL)
 96                   Fatal(FL, "failed to resolve shutdownTimeout");
 97           
 98               /* Get absolute CIMSHUTDOWN program name. */
 99           
100               if ((cimshutdownPath = FindMacro("cimshutdownPath")) == NULL)
101                   Fatal(FL, "failed to resolve cimshutdownPath");
102           
103               /* Create argument list. */
104           
105               tmpArgv[0] = CIMSHUTDOWN;
106               tmpArgv[1] = (char*)shutdownTimeout;
107               tmpArgv[2] = 0;
108           
109               /* Exec CIMSHUTDOWN program. */
110           
111               /* Flawfinder: ignore */
112               execv(cimshutdownPath, tmpArgv);
113               Fatal(FL, "failed to exec %s", cimshutdownPath);
114           }
115           
116 kumpf 1.2 /*
117           **==============================================================================
118           **
119           ** DefineExecutorMacros()
120           **
121           **     Define macros used by the executor.
122           **
123           **==============================================================================
124           */
125           
126           void DefineExecutorMacros()
127           {
128               /* Define ${internalBinDir} */
129               {
130                   char path[EXECUTOR_BUFFER_SIZE];
131           
132                   if (GetPegasusInternalBinDir(path) != 0)
133                       Fatal(FL, "failed to resolve internal pegasus bin directory");
134           
135                   DefineMacro("internalBinDir", path);
136               }
137 kumpf 1.2 
138               /* Define ${cimservermain} */
139           
140               DefineMacro("cimservermain", CIMSERVERMAIN);
141           
142               /* Define ${cimprovagt} */
143           
144               DefineMacro("cimprovagt", CIMPROVAGT);
145           
146               /* Define ${cimshutdown} */
147           
148               DefineMacro("cimshutdown", CIMSHUTDOWN);
149           
150               /* Define ${cimservera} */
151           
152               DefineMacro("cimservera", CIMSERVERA);
153           
154               /* Define ${cimservermainPath} */
155               {
156                   char path[EXECUTOR_BUFFER_SIZE];
157           
158 kumpf 1.2         if (ExpandMacros("${internalBinDir}/${cimservermain}", path) != 0)
159                       Fatal(FL, "failed to resolve cimservermainPath");
160           
161                   DefineMacro("cimservermainPath", path);
162               }
163           
164               /* Define ${cimprovagtPath} */
165               {
166                   char path[EXECUTOR_BUFFER_SIZE];
167           
168                   if (ExpandMacros("${internalBinDir}/${cimprovagt}", path) != 0)
169                       Fatal(FL, "failed to resolve cimprovagtPath");
170           
171                   DefineMacro("cimprovagtPath", path);
172               }
173           
174               /* Define ${cimshutdownPath} */
175               {
176                   char path[EXECUTOR_BUFFER_SIZE];
177           
178                   if (ExpandMacros("${internalBinDir}/${cimshutdown}", path) != 0)
179 kumpf 1.2             Fatal(FL, "failed to resolve cimshutdownPath");
180           
181                   DefineMacro("cimshutdownPath", path);
182               }
183           
184               /* Define ${cimserveraPath} */
185               {
186                   char path[EXECUTOR_BUFFER_SIZE];
187           
188                   if (ExpandMacros("${internalBinDir}/${cimservera}", path) != 0)
189                       Fatal(FL, "failed to resolve cimserveraPath");
190           
191                   DefineMacro("cimserveraPath", path);
192               }
193           
194               /* Define ${shutdownTimeout} */
195           
196               {
197                   char buffer[EXECUTOR_BUFFER_SIZE];
198           
199                   if (GetConfigParam("shutdownTimeout", buffer) != 0)
200 kumpf 1.2             Strlcpy(buffer, "5", sizeof(buffer));
201           
202                   DefineMacro("shutdownTimeout", buffer);
203               }
204           
205               /* Define ${currentConfigFilePath} */
206               {
207                   char path[EXECUTOR_BUFFER_SIZE];
208           
209                   if (GetHomedPath(PEGASUS_CURRENT_CONFIG_FILE_PATH, path) != 0)
210                   {
211                       Fatal(FL, "GetHomedPath() failed on \"%s\"",
212                           PEGASUS_CURRENT_CONFIG_FILE_PATH);
213                   }
214           
215                   DefineMacro("currentConfigFilePath", path);
216               }
217           
218               /* Define ${plannedConfigFilePath} */
219               {
220                   char path[EXECUTOR_BUFFER_SIZE];
221 kumpf 1.2 
222                   if (GetHomedPath(PEGASUS_PLANNED_CONFIG_FILE_PATH, path) != 0)
223                   {
224                       Fatal(FL, "GetHomedPath() failed on \"%s\"",
225                           PEGASUS_PLANNED_CONFIG_FILE_PATH);
226                   }
227           
228                   DefineMacro("plannedConfigFilePath", path);
229               }
230           
231               /* Define ${passwordFilePath} */
232           
233               if (DefineConfigPathMacro("passwordFilePath", "cimserver.passwd") != 0)
234                   Fatal(FL, "missing \"passwordFilePath\" configuration parameter.");
235           
236               /* Define ${sslKeyFilePath} */
237           
238               if (DefineConfigPathMacro("sslKeyFilePath", "file.pem") != 0)
239                   Fatal(FL, "missing \"sslKeyFilePath\" configuration parameter.");
240           
241               /* Define ${sslTrustStore} */
242 kumpf 1.2 
243               if (DefineConfigPathMacro("sslTrustStore", "cimserver_trust") != 0)
244                   Fatal(FL, "missing \"sslTrustStore\" configuration parameter.");
245           
246               /* Define ${crlStore} */
247           
248               if (DefineConfigPathMacro("crlStore", "crl") != 0)
249                   Fatal(FL, "missing \"crlStore\" configuration parameter.");
250           
251               /* Define ${privilegedUser} */
252           
253               DefineMacro("privilegedUser", "root");
254           
255               /* Define ${cimserverUser} */
256           
257               DefineMacro("cimserverUser", PEGASUS_CIMSERVERMAIN_USER);
258           }
259           
260           /*
261           **==============================================================================
262           **
263 kumpf 1.2 ** main()
264           **
265           **==============================================================================
266           */
267           
268           int main(int argc, char** argv)
269           {
270               const char* cimservermainPath;
271               int pair[2];
272               char username[EXECUTOR_BUFFER_SIZE];
273               int childPid;
274               struct Options options;
275           
276               /* Get options. */
277           
278               GetOptions(&argc, &argv, &options);
279           
280               /* Save argc-argv as globals. */
281           
282               globals.argc = argc;
283               globals.argv = argv;
284 kumpf 1.2 
285               /* Define macros needed by the executor. */
286           
287               DefineExecutorMacros();
288           
289               /* If shuting down, then run CIMSHUTDOWN client. */
290           
291               if (options.shutdown)
292                   ExecShutdown();
293           
294               /* Process --policy and --macros options. */
295           
296               if (options.dump)
297               {
298 kumpf 1.4         DumpPolicy(stdout, 1);
299                   DumpMacros(stdout);
300 kumpf 1.2         exit(0);
301               }
302           
303               /* Get absolute CIMSERVERMAIN program name. */
304           
305               if ((cimservermainPath = FindMacro("cimservermainPath")) == NULL)
306                   Fatal(FL, "Failed to locate %s program", CIMSERVERMAIN);
307           
308               /* If CIMSERVERMAIN is already running, warn and exit now, unless one of
309                * the following options are given: -v, --version, -h, --help (these are
310                * passed through to CIMSERVERMAIN).
311                */
312           
313 kumpf 1.4     if (!options.version &&
314                   !options.help &&
315                   TestProcessRunning(PEGASUS_CIMSERVER_START_FILE, CIMSERVERMAIN) == 0)
316 kumpf 1.2     {
317                   fprintf(stderr,
318                       "%s: cimserver is already running (the PID found in the file "
319                       "\"%s\" corresponds to an existing process named \"%s\").\n\n",
320                       globals.argv[0], PEGASUS_CIMSERVER_START_FILE, CIMSERVERMAIN);
321           
322                   exit(1);
323               }
324           
325               /* Get enableAuthentication configuration option. */
326           
327               {
328                   char buffer[EXECUTOR_BUFFER_SIZE];
329           
330                   if (GetConfigParam("enableAuthentication", buffer) == 0 &&
331                       strcasecmp(buffer, "true") == 0)
332                   {
333                       globals.enableAuthentication = 1;
334                   }
335               }
336           
337 kumpf 1.2     /* Create a socket pair for communicating with the child process. */
338           
339               if (CreateSocketPair(pair) != 0)
340                   Fatal(FL, "Failed to create socket pair");
341           
342               CloseOnExec(pair[1]);
343           
344 kumpf 1.4     /* Initialize the log-level from the configuration parameter. */
345 kumpf 1.2 
346 kumpf 1.4     InitLogLevel();
347 kumpf 1.2 
348               /* Open the log. */
349           
350               OpenLog("cimserver");
351           
352               Log(LL_INFORMATION, "starting");
353           
354               /* Be sure this process is running as root (otherwise fail). */
355           
356               if (setuid(0) != 0 || setgid(0) != 0)
357                   Fatal(FL, "attempted to run program as non-root user");
358           
359               /* Warn if authentication not enabled (suspicious use of executor). */
360           
361               if (!globals.enableAuthentication)
362                   Log(LL_WARNING, "authentication is NOT enabled");
363               else
364                   Log(LL_TRACE, "authentication is enabled");
365           
366               /* Print user info. */
367           
368 kumpf 1.2     if (GetUserName(getuid(), username) != 0)
369                   Fatal(FL, "cannot resolve user from uid=%d", getuid());
370           
371               Log(LL_TRACE, "running as %s (uid=%d, gid=%d)",
372                   username, (int)getuid(), (int)getgid());
373           
374               /* Determine user for running CIMSERVERMAIN. */
375           
376 kumpf 1.3     GetServerUser(&globals.childUserName, &globals.childUid, &globals.childGid);
377 kumpf 1.2 
378               /* Fork child process. */
379           
380               childPid = fork();
381           
382               if (childPid == 0)
383               {
384                   /* Child. */
385                   close(pair[1]);
386 kumpf 1.3         Child(
387                       argc,
388                       argv,
389                       cimservermainPath,
390                       globals.childUserName,
391                       globals.childUid,
392                       globals.childGid,
393                       pair[0]);
394 kumpf 1.2     }
395               else if (childPid > 0)
396               {
397                   /* Parent. */
398                   close(pair[0]);
399                   Parent(pair[1], childPid, options.bindVerbose);
400               }
401               else
402               {
403                   Fatal(FL, "fork() failed");
404               }
405           
406               return 0;
407           }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2