pt_file.c revision 1.4 1 /* $NetBSD: pt_file.c,v 1.4 1995/03/18 14:58:04 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 * All rights reserved.
7 *
8 * This code is derived from software donated to Berkeley by
9 * Jan-Simon Pendry.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 * from: Id: pt_file.c,v 1.1 1992/05/25 21:43:09 jsp Exp
40 * @(#)pt_file.c 8.2 (Berkeley) 3/27/94
41 */
42
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <errno.h>
48 #include <fcntl.h>
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/syslog.h>
52
53 #include "portald.h"
54
55 int portal_file(pcr, key, v, so, fdp)
56 struct portal_cred *pcr;
57 char *key;
58 char **v;
59 int so;
60 int *fdp;
61 {
62 int fd;
63 char pbuf[MAXPATHLEN];
64 int error;
65 gid_t gidset[NGROUPS];
66 int i;
67
68 pbuf[0] = '/';
69 strcpy(pbuf+1, key + (v[1] ? strlen(v[1]) : 0));
70
71 #ifdef DEBUG
72 printf("path = %s, uid = %d, gid = %d\n", pbuf, pcr->pcr_uid, pcr->pcr_groups[0]);
73 #endif
74
75 for (i = 0; i < pcr->pcr_ngroups; i++)
76 gidset[i] = pcr->pcr_groups[i];
77
78 if (setgroups(pcr->pcr_ngroups, gidset) < 0)
79 return (errno);
80
81 if (seteuid(pcr->pcr_uid) < 0)
82 return (errno);
83
84 fd = open(pbuf, O_RDWR|O_CREAT, 0666);
85 if (fd < 0)
86 error = errno;
87 else
88 error = 0;
89
90 if (seteuid((uid_t) 0) < 0) { /* XXX - should reset gidset too */
91 error = errno;
92 syslog(LOG_ERR, "setcred: %s", strerror(error));
93 if (fd >= 0) {
94 (void) close(fd);
95 fd = -1;
96 }
97 }
98
99 if (error == 0)
100 *fdp = fd;
101
102 #ifdef DEBUG
103 fprintf(stderr, "pt_file returns *fdp = %d, error = %d\n", *fdp, error);
104 #endif
105
106 return (error);
107 }
108