1273d1419SAndrew Rist /**************************************************************
2273d1419SAndrew Rist  *
3273d1419SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4273d1419SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5273d1419SAndrew Rist  * distributed with this work for additional information
6273d1419SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7273d1419SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8273d1419SAndrew Rist  * "License"); you may not use this file except in compliance
9273d1419SAndrew Rist  * with the License.  You may obtain a copy of the License at
10273d1419SAndrew Rist  *
11273d1419SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12273d1419SAndrew Rist  *
13273d1419SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14273d1419SAndrew Rist  * software distributed under the License is distributed on an
15273d1419SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16273d1419SAndrew Rist  * KIND, either express or implied.  See the License for the
17273d1419SAndrew Rist  * specific language governing permissions and limitations
18273d1419SAndrew Rist  * under the License.
19273d1419SAndrew Rist  *
20273d1419SAndrew Rist  *************************************************************/
21273d1419SAndrew Rist 
22cdf0e10cSrcweir #include <fcntl.h>
23cdf0e10cSrcweir #include <sys/types.h>
24cdf0e10cSrcweir #include <sys/stat.h>
25cdf0e10cSrcweir #include <unistd.h>
26cdf0e10cSrcweir #include <dlfcn.h>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #ifdef _cplusplus
29cdf0e10cSrcweir extern "C" {
30cdf0e10cSrcweir #endif
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #ifdef SOLARIS
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <sys/systeminfo.h>
35cdf0e10cSrcweir #include <strings.h>
36cdf0e10cSrcweir 
chown(const char * path,uid_t owner,gid_t group)37cdf0e10cSrcweir int   chown  (const char *path, uid_t owner, gid_t group) {return 0;}
lchown(const char * path,uid_t owner,gid_t group)38cdf0e10cSrcweir int   lchown (const char *path, uid_t owner, gid_t group) {return 0;}
fchown(int fildes,uid_t owner,gid_t group)39cdf0e10cSrcweir int   fchown (int fildes, uid_t owner, gid_t group)       {return 0;}
40cdf0e10cSrcweir 
getuid(void)41cdf0e10cSrcweir uid_t getuid  (void) {return 0;}
42cdf0e10cSrcweir int stat(const char *path,  struct stat *buf);
43cdf0e10cSrcweir #ifdef __notdef__
geteuid(void)44cdf0e10cSrcweir uid_t geteuid (void) {return 0;}
getgid(void)45cdf0e10cSrcweir gid_t getgid  (void) {return 0;}
getegid(void)46cdf0e10cSrcweir gid_t getegid (void) {return 0;}
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir 
setuid(uid_t p)49cdf0e10cSrcweir int   setuid  (uid_t p)  {return 0;}
setgid(gid_t p)50cdf0e10cSrcweir int   setgid  (gid_t p)  {return 0;}
51cdf0e10cSrcweir 
52cdf0e10cSrcweir /* This is to fool cpio and pkgmk */
fstat(int fildes,struct stat * buf)53cdf0e10cSrcweir int fstat(int fildes, struct stat *buf)
54cdf0e10cSrcweir {
55cdf0e10cSrcweir     int ret = 0;
56cdf0e10cSrcweir     static int (*p_fstat) (int fildes, struct stat *buf) = NULL;
57cdf0e10cSrcweir     if (p_fstat == NULL)
58cdf0e10cSrcweir         p_fstat = (int (*)(int fildes, struct stat *buf))
59cdf0e10cSrcweir             dlsym (RTLD_NEXT, "fstat");
60cdf0e10cSrcweir     ret = (*p_fstat)(fildes, buf);
61cdf0e10cSrcweir     if (buf != NULL)
62cdf0e10cSrcweir     {
63cdf0e10cSrcweir         buf->st_uid = 0; /* root */
64cdf0e10cSrcweir         buf->st_gid = 2; /* bin */
65cdf0e10cSrcweir     }
66cdf0e10cSrcweir 
67cdf0e10cSrcweir     return ret;
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir /* this is to fool mkdir, don't allow to remove owner execute right from directories */
chmod(const char * path,mode_t mode)71cdf0e10cSrcweir int chmod(const char *path, mode_t mode)
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     int ret = 0;
74cdf0e10cSrcweir     static int (*p_chmod) (const char *path, mode_t mode) = NULL;
75cdf0e10cSrcweir     if (p_chmod == NULL)
76cdf0e10cSrcweir         p_chmod = (int (*)(const char *path, mode_t mode))
77cdf0e10cSrcweir             dlsym (RTLD_NEXT, "chmod");
78cdf0e10cSrcweir 
79cdf0e10cSrcweir 	if ((mode & S_IXUSR) == 0)
80cdf0e10cSrcweir 	{
81cdf0e10cSrcweir 		struct stat statbuf;
82cdf0e10cSrcweir 		if (stat(path, &statbuf) == 0)
83cdf0e10cSrcweir 		{
84cdf0e10cSrcweir 			if ((statbuf.st_mode & S_IFDIR) != 0)
85cdf0e10cSrcweir 				mode = (mode | S_IXUSR);
86cdf0e10cSrcweir 		}
87cdf0e10cSrcweir 	}
88cdf0e10cSrcweir 
89cdf0e10cSrcweir     ret = (*p_chmod)(path, mode);
90cdf0e10cSrcweir     return ret;
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 
95cdf0e10cSrcweir /* This is to fool tar */
fstatat64(int fildes,const char * path,struct stat64 * buf,int flag)96cdf0e10cSrcweir int fstatat64(int fildes, const char *path, struct stat64  *buf, int flag)
97cdf0e10cSrcweir {
98cdf0e10cSrcweir     int ret = 0;
99cdf0e10cSrcweir     static int (*p_fstatat) (int fildes, const char *path, struct stat64 *buf, int flag) = NULL;
100cdf0e10cSrcweir     if (p_fstatat == NULL)
101cdf0e10cSrcweir         p_fstatat = (int (*)(int fildes, const char *path, struct stat64 *buf, int flag))
102cdf0e10cSrcweir             dlsym (RTLD_NEXT, "fstatat64");
103cdf0e10cSrcweir     ret = (*p_fstatat)(fildes, path, buf, flag);
104cdf0e10cSrcweir     if (buf != NULL)
105cdf0e10cSrcweir     {
106cdf0e10cSrcweir         buf->st_uid = 0; /* root */
107cdf0e10cSrcweir         buf->st_gid = 2; /* bin */
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir     return ret;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir #elif  defined LINUX
113cdf0e10cSrcweir 
114*f827b318SJim Jagielski uid_t getuid  (void)    {return 0;}
115*f827b318SJim Jagielski uid_t geteuid (void)    {return 0;}
116*f827b318SJim Jagielski int setgid    (gid_t p) {return 0;}
117cdf0e10cSrcweir 
118*f827b318SJim Jagielski /* This is to fool epm, tar, dpkg, et.al. into thinking we are root */
119cdf0e10cSrcweir #ifdef X86_64
120cdf0e10cSrcweir int __lxstat(int n, const char *path, struct stat *buf)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     int ret = 0;
123cdf0e10cSrcweir     static int (*p_lstat) (int n, const char *path, struct stat *buf) = NULL;
124cdf0e10cSrcweir     if (p_lstat == NULL)
125cdf0e10cSrcweir         p_lstat = (int (*)(int n, const char *path, struct stat *buf))
126cdf0e10cSrcweir             dlsym (RTLD_NEXT, "__lxstat");
127cdf0e10cSrcweir     ret = (*p_lstat)(n, path, buf);
128cdf0e10cSrcweir     if (buf != NULL)
129cdf0e10cSrcweir     {
130cdf0e10cSrcweir         buf->st_uid = 0; /* root */
131cdf0e10cSrcweir         buf->st_gid = 0; /* root */
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir     return ret;
134cdf0e10cSrcweir }
135cdf0e10cSrcweir #else
136cdf0e10cSrcweir int __lxstat64(int n, const char *path, struct stat64 *buf)
137cdf0e10cSrcweir {
138cdf0e10cSrcweir     int ret = 0;
139cdf0e10cSrcweir     static int (*p_lstat) (int n, const char *path, struct stat64 *buf) = NULL;
140cdf0e10cSrcweir     if (p_lstat == NULL)
141cdf0e10cSrcweir         p_lstat = (int (*)(int n, const char *path, struct stat64 *buf))
142cdf0e10cSrcweir             dlsym (RTLD_NEXT, "__lxstat64");
143cdf0e10cSrcweir     ret = (*p_lstat)(n, path, buf);
144cdf0e10cSrcweir     if (buf != NULL)
145cdf0e10cSrcweir     {
146cdf0e10cSrcweir         buf->st_uid = 0;
147cdf0e10cSrcweir         buf->st_gid = 0;
148cdf0e10cSrcweir     }
149cdf0e10cSrcweir     return ret;
150cdf0e10cSrcweir }
151cdf0e10cSrcweir #endif
152cdf0e10cSrcweir #endif
153cdf0e10cSrcweir 
154cdf0e10cSrcweir #ifdef _cplusplus
155cdf0e10cSrcweir }
156cdf0e10cSrcweir #endif
157cdf0e10cSrcweir 
158