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

  1 mike  1.1.2.1 /*
  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 mike  1.1.2.1 // 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 <sys/types.h>
 34               #include <sys/stat.h>
 35               #include <unistd.h>
 36               #include <stdlib.h>
 37               #include <string.h>
 38               #include <stdio.h>
 39               #include "Defines.h"
 40               #include "Fatal.h"
 41               #include "Path.h"
 42               #include "File.h"
 43 mike  1.1.2.1 #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                   char path[EXECUTOR_BUFFER_SIZE],
 60                   int uid,
 61                   int gid,
 62                   int sock)
 63               {
 64 mike  1.1.2.1     char sockStr[EXECUTOR_BUFFER_SIZE];
 65                   char repositoryDir[EXECUTOR_BUFFER_SIZE];
 66                   char username[EXECUTOR_BUFFER_SIZE];
 67                   char** execArgv;
 68               
 69                   /* Build argument list, adding "-x <sock>" option if sock non-negative. */
 70               
 71 mike  1.1.2.2     execArgv = (char**)malloc(sizeof(char*) * (argc + 3));
 72                   memcpy(execArgv + 3, argv + 1, sizeof(char*) * argc);
 73 mike  1.1.2.1 
 74                   sprintf(sockStr, "%d", sock);
 75               
 76                   execArgv[0] = CIMSERVERMAIN;
 77 mike  1.1.2.2     execArgv[1] = "-x";
 78                   execArgv[2] = strdup(sockStr);
 79 mike  1.1.2.1 
 80                   /* Locate repository directory. */
 81               
 82                   if (LocateRepositoryDirectory(argc, argv, repositoryDir) != 0)
 83                       Fatal(FL, "failed to locate repository directory");
 84               
 85                   /* Check whether repository directory exists. */
 86               
 87                   if (AccessDir(repositoryDir) != 0)
 88                       Fatal(FL, 
 89                           "failed to access repository directory: %s", repositoryDir);
 90               
 91                   /* 
 92                    * Change ownership of Pegasus repository directory (it should be owned
 93                    * by same user that owns CIMSERVERMAIN).
 94                    */
 95               
 96                   ChangeDirOwnerRecursive(repositoryDir, uid, gid);
 97               
 98                   Log(LL_TRACE, "Pegasus repositoryDir is \"%s\"", repositoryDir);
 99               
100 mike  1.1.2.1     /*
101                    * Downgrade privileges by setting the UID and GID of this process. Use
102                    * the owner of the CIMSERVERMAIN program obtained above.
103                    */
104               
105                   if (uid == 0 || gid == 0)
106                   {
107                       Fatal(FL, "root may not own %s since the program is run as owner",
108                           path);
109                   }
110               
111                   if (setgid(gid) != 0)
112                   {
113                       Fatal(FL, "Failed to set gid to %d", gid);
114                   }
115               
116                   if (setuid(uid) != 0)
117                   {
118                       Fatal(FL, "Failed to set uid to %d", uid);
119                   }
120               
121 mike  1.1.2.1     if ((int)getuid() != uid || 
122                       (int)geteuid() != uid || 
123                       (int)getgid() != gid || 
124                       (int)getegid() != gid)
125                   {
126                       Fatal(FL, "setuid/setgid verification failed\n");
127                   }
128               
129                   /* Log user info. */
130               
131                   if (GetUserName(uid, username) != 0)
132                       Fatal(FL, "cannot resolve user from uid=%d", uid);
133               
134                   Log(LL_TRACE, "%s running as %s (uid=%d, gid=%d)", CIMSERVERMAIN, 
135                       username, uid, gid);
136               
137                   /*
138                    * Precheck that cimxml.socket is owned by cimservermain process. If 
139                    * not, then the bind would fail in the cimservermain process much 
140                    * later and the cause of the error would be difficult to determine.
141                    */
142 mike  1.1.2.1 
143                   /* Flawfinder: ignore */
144                   if (access(PEGASUS_LOCAL_DOMAIN_SOCKET_PATH, F_OK) == 0)
145                   {
146                       struct stat st;
147               
148                       if (stat(PEGASUS_LOCAL_DOMAIN_SOCKET_PATH, &st) != 0 ||
149                           (int)st.st_uid != uid || 
150                           (int)st.st_gid != gid)
151                       {
152                           Fatal(FL, 
153                               "cimservermain process cannot stat or does not own %s",
154                               PEGASUS_LOCAL_DOMAIN_SOCKET_PATH);
155                       }
156                   }
157               
158                   /* Exec child process. */
159               
160                   /* Flawfinder: ignore */
161                   if (execv(path, execArgv) != 0)
162                       Fatal(FL, "failed to exec %s", path);
163 mike  1.1.2.1 
164                   exit(0);
165               }

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2