pt_file.c revision 1.15 1 /* $NetBSD: pt_file.c,v 1.15 2003/08/07 10:04:30 agc 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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * from: Id: pt_file.c,v 1.1 1992/05/25 21:43:09 jsp Exp
35 * @(#)pt_file.c 8.3 (Berkeley) 7/3/94
36 */
37
38 #include <sys/cdefs.h>
39 #ifndef lint
40 __RCSID("$NetBSD: pt_file.c,v 1.15 2003/08/07 10:04:30 agc Exp $");
41 #endif /* not lint */
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 #ifdef DEBUG
56 #define DEBUG_SYSLOG syslog
57 #else
58 /*
59 * The "if (0) ..." will be optimized away by the compiler if
60 * DEBUG is not defined.
61 */
62 #define DEBUG_SYSLOG if (0) syslog
63 #endif
64
65 int
66 lose_credentials(pcr)
67 struct portal_cred *pcr;
68 {
69 /*
70 * If we are root, then switch into the caller's credentials.
71 * By the way, we _always_ log failures, to make
72 * sure questionable activity is noticed.
73 */
74 if (getuid() == 0) {
75 /* Set groups first ... */
76 if (setgroups(pcr->pcr_ngroups, pcr->pcr_groups) < 0) {
77 syslog(LOG_WARNING,
78 "lose_credentials: setgroups() failed (%m)");
79 return (errno);
80 }
81 /* ... then gid ... */
82 if (setgid(pcr->pcr_gid) < 0) {
83 syslog(LOG_WARNING,
84 "lose_credentials: setgid(%d) failed (%m)",
85 pcr->pcr_gid);
86 }
87 /*
88 * ... and now do the setuid() where we lose all special
89 * powers (both real and effective userid).
90 */
91 if (setuid(pcr->pcr_uid) < 0) {
92 syslog(LOG_WARNING,
93 "lose_credentials: setuid(%d) failed (%m)",
94 pcr->pcr_uid);
95 }
96 /* The credential change was successful! */
97 DEBUG_SYSLOG(LOG_DEBUG, "Root-owned mount process lowered credentials -- returning successfully!\n");
98 return 0;
99 }
100 DEBUG_SYSLOG(LOG_DEBUG, "Actual/effective/caller's creds are:");
101 DEBUG_SYSLOG(LOG_DEBUG, "%d/%d %d/%d %d/%d", getuid(),
102 getgid(), geteuid(), getegid(), pcr->pcr_uid, pcr->pcr_gid);
103 /*
104 * Else, fail if the uid is neither actual or effective
105 * uid of mount process...
106 */
107 if ((getuid() != pcr->pcr_uid) && (geteuid() != pcr->pcr_uid)) {
108 syslog(LOG_WARNING,
109 "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_WARNING,
119 "lose_credentials: gid %d != gid %d, or egid %d != gid %d",
120 getgid(), pcr->pcr_gid, getegid(), pcr->pcr_gid);
121 return EPERM;
122 }
123 /*
124 * If we make it here, we have a uid _and_ gid match! Allow the
125 * access.
126 */
127 DEBUG_SYSLOG(LOG_DEBUG, "Returning successfully!\n");
128 return 0;
129 }
130
131 int
132 portal_file(pcr, key, v, so, fdp)
133 struct portal_cred *pcr;
134 char *key;
135 char **v;
136 int so;
137 int *fdp;
138 {
139 int fd;
140 char pbuf[MAXPATHLEN];
141 int error;
142 int origuid, origgid;
143
144 origuid = getuid();
145 origgid = getgid();
146 pbuf[0] = '/';
147 strlcpy(pbuf + 1, key + (v[1] ? strlen(v[1]) : 0), sizeof(pbuf) - 1);
148 DEBUG_SYSLOG(LOG_DEBUG, "path = %s, uid = %d, gid = %d",
149 pbuf, pcr->pcr_uid, pcr->pcr_gid);
150
151 if ((error = lose_credentials(pcr)) != 0) {
152 DEBUG_SYSLOG(LOG_DEBUG, "portal_file: Credential err %d", error);
153 return error;
154 }
155 error = 0;
156 /*
157 * Be careful -- only set error to errno if there is an error.
158 * errno could hold an old, uncaught value, from a routine
159 * called long before now.
160 */
161 fd = open(pbuf, O_RDWR | O_CREAT, 0666);
162 if (fd < 0) {
163 error = errno;
164 if (error == EACCES) {
165 DEBUG_SYSLOG(LOG_DEBUG, "Error: could not open '%s' "
166 "read/write with create flag. "
167 "Trying read-only open...", pbuf);
168 /* Try opening read-only. */
169 fd = open(pbuf, O_RDONLY, 0);
170 if (fd < 0) {
171 error = errno;
172 DEBUG_SYSLOG(LOG_DEBUG, "That failed too! %m");
173 } else {
174 /* Clear the error indicator. */
175 error = 0;
176 }
177 } else {
178 DEBUG_SYSLOG(LOG_DEBUG, "Error: could not open '%s': %m", pbuf);
179 }
180
181 }
182 if (seteuid((uid_t) origuid) < 0) { /* XXX - should reset gidset
183 * too */
184 error = errno;
185 syslog(LOG_WARNING, "setcred: %m");
186 if (fd >= 0) {
187 (void) close(fd);
188 fd = -1;
189 }
190 }
191 if (error == 0)
192 *fdp = fd;
193
194 DEBUG_SYSLOG(LOG_DEBUG, "pt_file returns *fdp = %d, error = %d", *fdp,
195 error);
196 return (error);
197 }
198