cbb70ee193469dfe35439b8735fcbdda9ebad938
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 * We allocate a stack of twice this size and point in the middle of it
36 * to support archs with stack growing up and down.
38 #define CHILD_STACK_SIZE 2048
41 int (*cmd
)(void *data
);
60 * Create recursively directory using the FULL path.
63 int _mkdir_recursive(void *_data
)
65 struct mkdir_data
*data
= _data
;
67 char *p
, tmp
[PATH_MAX
];
76 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
78 PERROR("snprintf mkdir");
83 if (tmp
[len
- 1] == '/') {
87 for (p
= tmp
+ 1; *p
; p
++) {
90 ret
= stat(tmp
, &statbuf
);
92 ret
= mkdir(tmp
, mode
);
94 if (!(errno
== EEXIST
)) {
95 PERROR("mkdir recursive");
105 ret
= mkdir(tmp
, mode
);
107 if (!(errno
== EEXIST
)) {
108 PERROR("mkdir recursive last piece");
120 int _mkdir(void *_data
)
122 struct mkdir_data
*data
= _data
;
123 return mkdir(data
->path
, data
->mode
);
127 int _open(void *_data
)
129 struct open_data
*data
= _data
;
130 return open(data
->path
, data
->flags
, data
->mode
);
134 int child_run_as(void *_data
)
136 struct run_as_data
*data
= _data
;
137 size_t writelen
, writeleft
, index
;
145 * Child: it is safe to drop egid and euid while sharing the
146 * file descriptors with the parent process, since we do not
147 * drop "uid": therefore, the user we are dropping egid/euid to
148 * cannot attach to this process with, e.g. ptrace, nor map this
151 ret
= setegid(data
->gid
);
156 ret
= seteuid(data
->uid
);
162 * Also set umask to 0 for mkdir executable bit.
165 sendret
.i
= (*data
->cmd
)(data
->data
);
166 /* send back return value */
167 writeleft
= sizeof(sendret
);
170 writelen
= write(data
->retval_pipe
, &sendret
.c
[index
],
176 writeleft
-= writelen
;
178 } 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
;
192 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 child_stack
= malloc(CHILD_STACK_SIZE
* 2);
213 ret
= pipe(retval_pipe
);
218 run_as_data
.data
= data
;
219 run_as_data
.cmd
= cmd
;
220 run_as_data
.uid
= uid
;
221 run_as_data
.gid
= gid
;
222 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
224 pid
= clone(child_run_as
, child_stack
+ CHILD_STACK_SIZE
,
225 CLONE_FILES
| SIGCHLD
, &run_as_data
, NULL
);
231 /* receive return value */
232 readleft
= sizeof(retval
);
235 readlen
= read(retval_pipe
[0], &retval
.c
[index
], readleft
);
243 } while (readleft
> 0);
246 * Parent: wait for child to return, in which case the
247 * shared memory map will have been created.
250 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
255 close(retval_pipe
[0]);
256 close(retval_pipe
[1]);
262 int mkdir_recursive_run_as(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
264 struct mkdir_data data
;
266 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
267 path
, mode
, uid
, gid
);
270 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
273 int mkdir_run_as(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
275 struct mkdir_data data
;
277 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
278 path
, mode
, uid
, gid
);
281 return run_as(_mkdir
, &data
, uid
, gid
);
285 * Note: open_run_as is currently not working. We'd need to pass the fd
286 * opened in the child to the parent.
288 int open_run_as(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
290 struct open_data data
;
295 return run_as(_open
, &data
, uid
, gid
);
This page took 0.036725 seconds and 4 git commands to generate.