Commit | Line | Data |
---|---|---|
e1b631e8 | 1 | /* |
d3faac99 KB |
2 | * Copyright (c) 1992, 1993 |
3 | * The Regents of the University of California. All rights reserved. | |
e1b631e8 JSP |
4 | * All rights reserved. |
5 | * | |
6 | * This code is derived from software donated to Berkeley by | |
7 | * Jan-Simon Pendry. | |
8 | * | |
9 | * %sccs.include.redist.c% | |
10 | * | |
1233d419 | 11 | * @(#)pt_file.c 8.2 (Berkeley) %G% |
e1b631e8 JSP |
12 | * |
13 | * $Id: pt_file.c,v 1.1 1992/05/25 21:43:09 jsp Exp jsp $ | |
14 | */ | |
15 | ||
16 | #include <stdio.h> | |
17 | #include <unistd.h> | |
18 | #include <stdlib.h> | |
1233d419 | 19 | #include <string.h> |
e1b631e8 JSP |
20 | #include <errno.h> |
21 | #include <fcntl.h> | |
22 | #include <sys/types.h> | |
23 | #include <sys/param.h> | |
24 | #include <sys/syslog.h> | |
25 | ||
26 | #include "portald.h" | |
27 | ||
28 | int portal_file(pcr, key, v, so, fdp) | |
29 | struct portal_cred *pcr; | |
30 | char *key; | |
31 | char **v; | |
32 | int so; | |
33 | int *fdp; | |
34 | { | |
35 | int fd; | |
e1b631e8 JSP |
36 | char pbuf[MAXPATHLEN]; |
37 | int error; | |
225429d9 JSP |
38 | int gidset[NGROUPS]; |
39 | int i; | |
e1b631e8 JSP |
40 | |
41 | pbuf[0] = '/'; | |
42 | strcpy(pbuf+1, key + (v[1] ? strlen(v[1]) : 0)); | |
43 | ||
44 | #ifdef DEBUG | |
225429d9 | 45 | printf("path = %s, uid = %d, gid = %d\n", pbuf, pcr->pcr_uid, pcr->pcr_groups[0]); |
e1b631e8 JSP |
46 | #endif |
47 | ||
225429d9 JSP |
48 | for (i = 0; i < pcr->pcr_ngroups; i++) |
49 | gidset[i] = pcr->pcr_groups[i]; | |
e1b631e8 | 50 | |
225429d9 | 51 | if (setgroups(pcr->pcr_ngroups, gidset) < 0) |
e1b631e8 JSP |
52 | return (errno); |
53 | ||
225429d9 | 54 | if (seteuid(pcr->pcr_uid) < 0) |
e1b631e8 JSP |
55 | return (errno); |
56 | ||
57 | fd = open(pbuf, O_RDWR|O_CREAT, 0666); | |
58 | if (fd < 0) | |
59 | error = errno; | |
60 | else | |
61 | error = 0; | |
62 | ||
225429d9 | 63 | if (seteuid((uid_t) 0) < 0) { /* XXX - should reset gidset too */ |
e1b631e8 JSP |
64 | error = errno; |
65 | syslog(LOG_ERR, "setcred: %s", strerror(error)); | |
66 | if (fd >= 0) { | |
67 | (void) close(fd); | |
68 | fd = -1; | |
69 | } | |
70 | } | |
71 | ||
72 | if (error == 0) | |
73 | *fdp = fd; | |
74 | ||
75 | #ifdef DEBUG | |
76 | fprintf(stderr, "pt_file returns *fdp = %d, error = %d\n", *fdp, error); | |
77 | #endif | |
78 | ||
79 | return (error); | |
80 | } |