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