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>
33 #include <common/error.h>
37 #define RUNAS_CHILD_STACK_SIZE 10485760
40 int (*cmd
)(void *data
);
47 struct run_as_mkdir_data
{
52 struct run_as_open_data
{
59 * Create recursively directory using the FULL path.
62 int _mkdir_recursive(void *_data
)
64 struct run_as_mkdir_data
*data
= _data
;
66 char *p
, tmp
[PATH_MAX
];
75 ret
= snprintf(tmp
, sizeof(tmp
), "%s", path
);
77 PERROR("snprintf mkdir");
82 if (tmp
[len
- 1] == '/') {
86 for (p
= tmp
+ 1; *p
; p
++) {
89 ret
= stat(tmp
, &statbuf
);
91 ret
= mkdir(tmp
, mode
);
93 if (!(errno
== EEXIST
)) {
94 PERROR("mkdir recursive");
104 ret
= mkdir(tmp
, mode
);
106 if (!(errno
== EEXIST
)) {
107 PERROR("mkdir recursive last piece");
119 int _mkdir(void *_data
)
121 struct run_as_mkdir_data
*data
= _data
;
122 return mkdir(data
->path
, data
->mode
);
126 int _open(void *_data
)
128 struct run_as_open_data
*data
= _data
;
129 return open(data
->path
, data
->flags
, data
->mode
);
133 int child_run_as(void *_data
)
135 struct run_as_data
*data
= _data
;
136 size_t writelen
, writeleft
, index
;
144 * Child: it is safe to drop egid and euid while sharing the
145 * file descriptors with the parent process, since we do not
146 * drop "uid": therefore, the user we are dropping egid/euid to
147 * cannot attach to this process with, e.g. ptrace, nor map this
150 if (data
->gid
!= getegid()) {
151 ret
= setegid(data
->gid
);
157 if (data
->uid
!= geteuid()) {
158 ret
= seteuid(data
->uid
);
165 * Also set umask to 0 for mkdir executable bit.
168 sendret
.i
= (*data
->cmd
)(data
->data
);
169 /* send back return value */
170 writeleft
= sizeof(sendret
);
173 writelen
= write(data
->retval_pipe
, &sendret
.c
[index
],
179 writeleft
-= writelen
;
181 } while (writeleft
> 0);
186 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
188 struct run_as_data run_as_data
;
193 ssize_t readlen
, readleft
, index
;
201 * If we are non-root, we can only deal with our own uid.
203 if (geteuid() != 0) {
204 if (uid
!= geteuid()) {
205 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
211 ret
= pipe(retval_pipe
);
216 run_as_data
.data
= data
;
217 run_as_data
.cmd
= cmd
;
218 run_as_data
.uid
= uid
;
219 run_as_data
.gid
= gid
;
220 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
221 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
222 PROT_WRITE
| PROT_READ
,
223 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| MAP_STACK
,
225 if (child_stack
== MAP_FAILED
) {
231 * Pointing to the middle of the stack to support architectures
232 * where the stack grows up (HPPA).
234 pid
= clone(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
235 CLONE_FILES
| SIGCHLD
,
242 /* receive return value */
243 readleft
= sizeof(retval
);
246 readlen
= read(retval_pipe
[0], &retval
.c
[index
], readleft
);
254 } while (readleft
> 0);
257 * Parent: wait for child to return, in which case the
258 * shared memory map will have been created.
260 pid
= waitpid(pid
, &status
, 0);
261 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
266 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
271 close(retval_pipe
[0]);
272 close(retval_pipe
[1]);
277 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
279 struct run_as_mkdir_data data
;
281 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
282 path
, mode
, uid
, gid
);
285 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
288 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
290 struct run_as_mkdir_data data
;
292 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
293 path
, mode
, uid
, gid
);
296 return run_as(_mkdir
, &data
, uid
, gid
);
300 * Note: open_run_as is currently not working. We'd need to pass the fd
301 * opened in the child to the parent.
303 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
305 struct run_as_open_data data
;
307 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
308 path
, flags
, mode
, uid
, gid
);
312 return run_as(_open
, &data
, uid
, gid
);
This page took 0.0769 seconds and 4 git commands to generate.