f612ccc7554590a44fdeca167a33f29d4b0ee0d0
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; only version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
35 #define CHILD_STACK_SIZE 10485760
38 int (*cmd
)(void *data
);
57 * Create recursively directory using the FULL path.
60 int _mkdir_recursive(void *_data
)
62 struct mkdir_data
*data
= _data
;
64 char *p
, tmp
[PATH_MAX
];
73 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
75 PERROR("snprintf mkdir");
80 if (tmp
[len
- 1] == '/') {
84 for (p
= tmp
+ 1; *p
; p
++) {
87 ret
= stat(tmp
, &statbuf
);
89 ret
= mkdir(tmp
, mode
);
91 if (!(errno
== EEXIST
)) {
92 PERROR("mkdir recursive");
102 ret
= mkdir(tmp
, mode
);
104 if (!(errno
== EEXIST
)) {
105 PERROR("mkdir recursive last piece");
117 int _mkdir(void *_data
)
119 struct mkdir_data
*data
= _data
;
120 return mkdir(data
->path
, data
->mode
);
124 int _open(void *_data
)
126 struct open_data
*data
= _data
;
127 return open(data
->path
, data
->flags
, data
->mode
);
131 int child_run_as(void *_data
)
133 struct run_as_data
*data
= _data
;
134 size_t writelen
, writeleft
, index
;
142 * Child: it is safe to drop egid and euid while sharing the
143 * file descriptors with the parent process, since we do not
144 * drop "uid": therefore, the user we are dropping egid/euid to
145 * cannot attach to this process with, e.g. ptrace, nor map this
148 if (data
->gid
!= getegid()) {
149 ret
= setegid(data
->gid
);
155 if (data
->uid
!= geteuid()) {
156 ret
= seteuid(data
->uid
);
163 * Also set umask to 0 for mkdir executable bit.
166 sendret
.i
= (*data
->cmd
)(data
->data
);
167 /* send back return value */
168 writeleft
= sizeof(sendret
);
171 writelen
= write(data
->retval_pipe
, &sendret
.c
[index
],
177 writeleft
-= writelen
;
179 } while (writeleft
> 0);
184 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
186 struct run_as_data run_as_data
;
191 ssize_t readlen
, readleft
, index
;
199 * If we are non-root, we can only deal with our own uid.
201 if (geteuid() != 0) {
202 if (uid
!= geteuid()) {
203 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
209 ret
= pipe(retval_pipe
);
214 run_as_data
.data
= data
;
215 run_as_data
.cmd
= cmd
;
216 run_as_data
.uid
= uid
;
217 run_as_data
.gid
= gid
;
218 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
219 child_stack
= mmap(NULL
, CHILD_STACK_SIZE
,
220 PROT_WRITE
| PROT_READ
,
221 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| MAP_STACK
,
223 if (child_stack
== MAP_FAILED
) {
229 * Pointing to the middle of the stack to support architectures
230 * where the stack grows up (HPPA).
232 pid
= clone(child_run_as
, child_stack
+ (CHILD_STACK_SIZE
/ 2),
233 CLONE_FILES
| SIGCHLD
| CLONE_VM
,
240 /* receive return value */
241 readleft
= sizeof(retval
);
244 readlen
= read(retval_pipe
[0], &retval
.c
[index
], readleft
);
252 } while (readleft
> 0);
255 * Parent: wait for child to return, in which case the
256 * shared memory map will have been created.
258 pid
= waitpid(pid
, &status
, 0);
259 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
264 ret
= munmap(child_stack
, CHILD_STACK_SIZE
);
269 close(retval_pipe
[0]);
270 close(retval_pipe
[1]);
275 int mkdir_recursive_run_as(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
277 struct mkdir_data data
;
279 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
280 path
, mode
, uid
, gid
);
283 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
286 int mkdir_run_as(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
288 struct mkdir_data data
;
290 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
291 path
, mode
, uid
, gid
);
294 return run_as(_mkdir
, &data
, uid
, gid
);
298 * Note: open_run_as is currently not working. We'd need to pass the fd
299 * opened in the child to the parent.
301 int open_run_as(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
303 struct open_data data
;
305 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
306 path
, flags
, mode
, uid
, gid
);
310 return run_as(_open
, &data
, uid
, gid
);
This page took 0.034402 seconds and 3 git commands to generate.