pt_file.c revision 1.11 1 /* $NetBSD: pt_file.c,v 1.11 1999/08/16 06:38:12 bgrayson Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software donated to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 * from: Id: pt_file.c,v 1.1 1992/05/25 21:43:09 jsp Exp
39 * @(#)pt_file.c 8.3 (Berkeley) 7/3/94
40 */
41
42 #include <sys/cdefs.h>
43 #ifndef lint
44 __RCSID("$NetBSD: pt_file.c,v 1.11 1999/08/16 06:38:12 bgrayson Exp $");
45 #endif /* not lint */
46
47 #include <stdio.h>
48 #include <unistd.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <errno.h>
52 #include <fcntl.h>
53 #include <sys/types.h>
54 #include <sys/param.h>
55 #include <sys/syslog.h>
56
57 #include "portald.h"
58
59 #ifdef DEBUG
60 #define DEBUG_SYSLOG syslog
61 #else
62 /*
63 * The "if (0) ..." will be optimized away by the compiler if
64 * DEBUG is not defined.
65 */
66 #define DEBUG_SYSLOG if (0) syslog
67 #endif
68
69 int
70 lose_credentials(pcr)
71 struct portal_cred *pcr;
72 {
73 /*
74 * If we are root, then switch into the caller's credentials.
75 * By the way, we _always_ log failures, to make
76 * sure questionable activity is noticed.
77 */
78 if (getuid() == 0) {
79 /* Set egid, then groups, then uid. */
80 if (setegid(pcr->pcr_gid) < 0) {
81 syslog(LOG_ERR,
82 "lose_credentials: setegid(%d) failed (%m)",
83 pcr->pcr_gid);
84 return (errno);
85 }
86 if (setgroups(pcr->pcr_ngroups, pcr->pcr_groups) < 0) {
87 syslog(LOG_ERR,
88 "lose_credentials: setgroups() failed (%m)");
89 return (errno);
90 }
91 if (seteuid(pcr->pcr_uid) < 0) {
92 syslog(LOG_ERR,
93 "lose_credentials: seteuid(%d) failed (%m)",
94 pcr->pcr_uid);
95 return (errno);
96 }
97 /* The credential change was successful! */
98 DEBUG_SYSLOG(LOG_ERR, "Root-owned mount process lowered credentials -- returning successfully!\n");
99 return 0;
100 }
101 DEBUG_SYSLOG(LOG_ERR, "Actual/effective/caller's creds are:");
102 DEBUG_SYSLOG(LOG_ERR, "%d/%d %d/%d %d/%d", getuid(),
103 getgid(), geteuid(), getegid(), pcr->pcr_uid, pcr->pcr_gid);
104 /*
105 * Else, fail if the uid is neither actual or effective
106 * uid of mount process...
107 */
108 if ((getuid() != pcr->pcr_uid) && (geteuid() != pcr->pcr_uid)) {
109 syslog(LOG_ERR, "lose_credentials: uid %d != uid %d, or euid %d != uid %d",
110 getuid(), pcr->pcr_uid, geteuid(), pcr->pcr_uid);
111 return EPERM;
112 }
113 /*
114 * ... or the gid is neither the actual or effective
115 * gid of the mount process.
116 */
117 if ((getgid() != pcr->pcr_gid) && (getegid() != pcr->pcr_gid)) {
118 syslog(LOG_ERR, "lose_credentials: gid %d != gid %d, or egid %d != gid %d",
119 getgid(), pcr->pcr_gid, getegid(), pcr->pcr_gid);
120 return EPERM;
121 }
122 /*
123 * If we make it here, we have a uid _and_ gid match! Allow the
124 * access.
125 */
126 DEBUG_SYSLOG(LOG_ERR, "Returning successfully!\n");
127 return 0;
128 }
129
130 int
131 portal_file(pcr, key, v, so, fdp)
132 struct portal_cred *pcr;
133 char *key;
134 char **v;
135 int so;
136 int *fdp;
137 {
138 int fd;
139 char pbuf[MAXPATHLEN];
140 int error;
141 int origuid, origgid;
142
143 origuid = getuid();
144 origgid = getgid();
145 pbuf[0] = '/';
146 strcpy(pbuf + 1, key + (v[1] ? strlen(v[1]) : 0));
147 DEBUG_SYSLOG(LOG_ERR, "path = %s, uid = %d, gid = %d",
148 pbuf, pcr->pcr_uid, pcr->pcr_gid);
149
150 if ((error = lose_credentials(pcr)) != 0) {
151 DEBUG_SYSLOG(LOG_ERR, "portal_file: Credential err %d", error);
152 return error;
153 }
154 error = 0;
155 /*
156 * Be careful -- only set error to errno if there is an error.
157 * errno could hold an old, uncaught value, from a routine
158 * called long before now.
159 */
160 fd = open(pbuf, O_RDWR | O_CREAT, 0666);
161 if (fd < 0) {
162 error = errno;
163 if (error == EACCES) {
164 DEBUG_SYSLOG(LOG_ERR, "Error: could not open '%s' "
165 "read/write with create flag. "
166 "Trying read-only open...", pbuf);
167 /* Try opening read-only. */
168 fd = open(pbuf, O_RDONLY, 0);
169 if (fd < 0) {
170 error = errno;
171 DEBUG_SYSLOG(LOG_ERR, "That failed too! %m");
172 } else {
173 /* Clear the error indicator. */
174 error = 0;
175 }
176 } else {
177 DEBUG_SYSLOG(LOG_ERR, "Error: could not open '%s': %m", pbuf);
178 }
179
180 }
181 if (seteuid((uid_t) origuid) < 0) { /* XXX - should reset gidset
182 * too */
183 error = errno;
184 syslog(LOG_ERR, "setcred: %m");
185 if (fd >= 0) {
186 (void) close(fd);
187 fd = -1;
188 }
189 }
190 if (error == 0)
191 *fdp = fd;
192
193 DEBUG_SYSLOG(LOG_ERR, "pt_file returns *fdp = %d, error = %d", *fdp,
194 error);
195 return (error);
196 }
197