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