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

  1 kumpf 1.2 /*
  2 martin 1.8 //%LICENSE////////////////////////////////////////////////////////////////
  3 martin 1.10 //
  4 martin 1.8  // Licensed to The Open Group (TOG) under one or more contributor license
  5             // agreements.  Refer to the OpenPegasusNOTICE.txt file distributed with
  6             // this work for additional information regarding copyright ownership.
  7             // Each contributor licenses this file to you under the OpenPegasus Open
  8             // Source License; you may not use this file except in compliance with the
  9             // License.
 10 martin 1.10 //
 11 martin 1.8  // Permission is hereby granted, free of charge, to any person obtaining a
 12             // copy of this software and associated documentation files (the "Software"),
 13             // to deal in the Software without restriction, including without limitation
 14             // the rights to use, copy, modify, merge, publish, distribute, sublicense,
 15             // and/or sell copies of the Software, and to permit persons to whom the
 16             // Software is furnished to do so, subject to the following conditions:
 17 martin 1.10 //
 18 martin 1.8  // The above copyright notice and this permission notice shall be included
 19             // in all copies or substantial portions of the Software.
 20 martin 1.10 //
 21 martin 1.8  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 22 martin 1.10 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 23 martin 1.8  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 24             // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 25             // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 26             // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 27             // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 28 martin 1.10 //
 29 martin 1.8  //////////////////////////////////////////////////////////////////////////
 30 martin 1.9  */
 31 martin 1.8  
 32 kumpf  1.2  #include <sys/types.h>
 33             #include <sys/stat.h>
 34             #include <unistd.h>
 35             #include <stdlib.h>
 36             #include <string.h>
 37             #include <stdio.h>
 38 kumpf  1.3  #include <grp.h>
 39 kumpf  1.2  #include "Defines.h"
 40 kumpf  1.6  #include "Globals.h"
 41 kumpf  1.2  #include "Fatal.h"
 42             #include "Path.h"
 43             #include "Log.h"
 44             #include "User.h"
 45             
 46             /*
 47             **==============================================================================
 48             **
 49             ** Child
 50             **
 51             **     The child process.
 52             **
 53             **==============================================================================
 54             */
 55             
 56             void Child(
 57                 int argc,
 58                 char** argv,
 59                 const char* path,
 60 kumpf  1.3      const char* userName,
 61 kumpf  1.2      int uid,
 62                 int gid,
 63                 int sock)
 64             {
 65                 char sockStr[EXECUTOR_BUFFER_SIZE];
 66                 char** execArgv;
 67             
 68 kumpf  1.6      globals.isChildProcess = 1;
 69             
 70 kumpf  1.2      /* Build argument list, adding "--executor-socket <sock>" option if
 71                  * sock non-negative.
 72                  */
 73             
 74 dmitry.mikulin 1.7      if ((execArgv = (char**)malloc(sizeof(char*) * (argc + 3))) == NULL)
 75                         {
 76                             Fatal(FL, "Memory allocation failed");
 77                         }
 78 kumpf          1.2      memcpy(execArgv + 3, argv + 1, sizeof(char*) * argc);
 79                     
 80                         sprintf(sockStr, "%d", sock);
 81                     
 82                         execArgv[0] = CIMSERVERMAIN;
 83                         execArgv[1] = "--executor-socket";
 84                         execArgv[2] = strdup(sockStr);
 85                     
 86                         /*
 87                          * Downgrade privileges by setting the UID and GID of this process. Use
 88                          * the owner of the CIMSERVERMAIN program obtained above.
 89                          */
 90                     
 91                         if (uid == 0 || gid == 0)
 92                         {
 93                             Fatal(FL, "root may not own %s since the program is run as owner",
 94                                 path);
 95                         }
 96                     
 97                         if (setgid(gid) != 0)
 98                         {
 99 kumpf          1.2          Fatal(FL, "Failed to set gid to %d", gid);
100                         }
101                     
102 kumpf          1.3      if (initgroups(userName, gid) != 0)
103                         {
104                             Fatal(FL, "Failed to initialize groups for user %s", userName);
105                         }
106                     
107 kumpf          1.2      if (setuid(uid) != 0)
108                         {
109                             Fatal(FL, "Failed to set uid to %d", uid);
110                         }
111                     
112                         if ((int)getuid() != uid ||
113                             (int)geteuid() != uid ||
114                             (int)getgid() != gid ||
115                             (int)getegid() != gid)
116                         {
117                             Fatal(FL, "setuid/setgid verification failed\n");
118                         }
119                     
120                         /* Log user info. */
121                     
122                         Log(LL_TRACE, "%s running as %s (uid=%d, gid=%d)", CIMSERVERMAIN,
123 kumpf          1.4          userName, uid, gid);
124 kumpf          1.2  
125 kumpf          1.5      /* Exec child process. */
126 kumpf          1.2  
127                         /* Flawfinder: ignore */
128 kumpf          1.5      execv(path, execArgv);
129 kumpf          1.2  
130 kumpf          1.5      /* If we are still here, the exec failed. */
131                         Fatal(FL, "failed to exec %s", path);
132 kumpf          1.2  }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2