1*273d1419SAndrew Rist /**************************************************************
2*273d1419SAndrew Rist  *
3*273d1419SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*273d1419SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*273d1419SAndrew Rist  * distributed with this work for additional information
6*273d1419SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*273d1419SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*273d1419SAndrew Rist  * "License"); you may not use this file except in compliance
9*273d1419SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*273d1419SAndrew Rist  *
11*273d1419SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*273d1419SAndrew Rist  *
13*273d1419SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*273d1419SAndrew Rist  * software distributed under the License is distributed on an
15*273d1419SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*273d1419SAndrew Rist  * KIND, either express or implied.  See the License for the
17*273d1419SAndrew Rist  * specific language governing permissions and limitations
18*273d1419SAndrew Rist  * under the License.
19*273d1419SAndrew Rist  *
20*273d1419SAndrew Rist  *************************************************************/
21*273d1419SAndrew 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 
37cdf0e10cSrcweir int   chown  (const char *path, uid_t owner, gid_t group) {return 0;}
38cdf0e10cSrcweir int   lchown (const char *path, uid_t owner, gid_t group) {return 0;}
39cdf0e10cSrcweir int   fchown (int fildes, uid_t owner, gid_t group)       {return 0;}
40cdf0e10cSrcweir 
41cdf0e10cSrcweir uid_t getuid  (void) {return 0;}
42cdf0e10cSrcweir int stat(const char *path,  struct stat *buf);
43cdf0e10cSrcweir #ifdef __notdef__
44cdf0e10cSrcweir uid_t geteuid (void) {return 0;}
45cdf0e10cSrcweir gid_t getgid  (void) {return 0;}
46cdf0e10cSrcweir gid_t getegid (void) {return 0;}
47cdf0e10cSrcweir #endif
48cdf0e10cSrcweir 
49cdf0e10cSrcweir int   setuid  (uid_t p)  {return 0;}
50cdf0e10cSrcweir int   setgid  (gid_t p)  {return 0;}
51cdf0e10cSrcweir 
52cdf0e10cSrcweir /* This is to fool cpio and pkgmk */
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 */
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 */
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 
114cdf0e10cSrcweir uid_t getuid  (void) {return 0;}
115cdf0e10cSrcweir uid_t geteuid (void) {return 0;}
116cdf0e10cSrcweir 
117cdf0e10cSrcweir /* This is to fool tar */
118cdf0e10cSrcweir #ifdef X86_64
119cdf0e10cSrcweir int __lxstat(int n, const char *path, struct stat *buf)
120cdf0e10cSrcweir {
121cdf0e10cSrcweir     int ret = 0;
122cdf0e10cSrcweir     static int (*p_lstat) (int n, const char *path, struct stat *buf) = NULL;
123cdf0e10cSrcweir     if (p_lstat == NULL)
124cdf0e10cSrcweir         p_lstat = (int (*)(int n, const char *path, struct stat *buf))
125cdf0e10cSrcweir             dlsym (RTLD_NEXT, "__lxstat");
126cdf0e10cSrcweir     ret = (*p_lstat)(n, path, buf);
127cdf0e10cSrcweir     if (buf != NULL)
128cdf0e10cSrcweir     {
129cdf0e10cSrcweir         buf->st_uid = 0; /* root */
130cdf0e10cSrcweir         buf->st_gid = 0; /* root */
131cdf0e10cSrcweir     }
132cdf0e10cSrcweir     return ret;
133cdf0e10cSrcweir }
134cdf0e10cSrcweir #else
135cdf0e10cSrcweir int __lxstat64(int n, const char *path, struct stat64 *buf)
136cdf0e10cSrcweir {
137cdf0e10cSrcweir     int ret = 0;
138cdf0e10cSrcweir     static int (*p_lstat) (int n, const char *path, struct stat64 *buf) = NULL;
139cdf0e10cSrcweir     if (p_lstat == NULL)
140cdf0e10cSrcweir         p_lstat = (int (*)(int n, const char *path, struct stat64 *buf))
141cdf0e10cSrcweir             dlsym (RTLD_NEXT, "__lxstat64");
142cdf0e10cSrcweir     ret = (*p_lstat)(n, path, buf);
143cdf0e10cSrcweir     if (buf != NULL)
144cdf0e10cSrcweir     {
145cdf0e10cSrcweir         buf->st_uid = 0;
146cdf0e10cSrcweir         buf->st_gid = 0;
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir     return ret;
149cdf0e10cSrcweir }
150cdf0e10cSrcweir #endif
151cdf0e10cSrcweir #endif
152cdf0e10cSrcweir 
153cdf0e10cSrcweir #ifdef _cplusplus
154cdf0e10cSrcweir }
155cdf0e10cSrcweir #endif
156cdf0e10cSrcweir 
157