1 /* $NetBSD: pt_file.c,v 1.16 2005/02/09 13:57:57 xtraeme Exp $ */
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software donated to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
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
38 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: pt_file.c,v 1.16 2005/02/09 13:57:57 xtraeme Exp $");
49 #include <sys/types.h>
50 #include <sys/param.h>
51 #include <sys/syslog.h>
56 #define DEBUG_SYSLOG syslog
59 * The "if (0) ..." will be optimized away by the compiler if
60 * DEBUG is not defined.
62 #define DEBUG_SYSLOG if (0) syslog
66 lose_credentials(struct portal_cred
*pcr
)
69 * If we are root, then switch into the caller's credentials.
70 * By the way, we _always_ log failures, to make
71 * sure questionable activity is noticed.
74 /* Set groups first ... */
75 if (setgroups(pcr
->pcr_ngroups
, pcr
->pcr_groups
) < 0) {
77 "lose_credentials: setgroups() failed (%m)");
80 /* ... then gid ... */
81 if (setgid(pcr
->pcr_gid
) < 0) {
83 "lose_credentials: setgid(%d) failed (%m)",
87 * ... and now do the setuid() where we lose all special
88 * powers (both real and effective userid).
90 if (setuid(pcr
->pcr_uid
) < 0) {
92 "lose_credentials: setuid(%d) failed (%m)",
95 /* The credential change was successful! */
96 DEBUG_SYSLOG(LOG_DEBUG
, "Root-owned mount process lowered credentials -- returning successfully!\n");
99 DEBUG_SYSLOG(LOG_DEBUG
, "Actual/effective/caller's creds are:");
100 DEBUG_SYSLOG(LOG_DEBUG
, "%d/%d %d/%d %d/%d", getuid(),
101 getgid(), geteuid(), getegid(), pcr
->pcr_uid
, pcr
->pcr_gid
);
103 * Else, fail if the uid is neither actual or effective
104 * uid of mount process...
106 if ((getuid() != pcr
->pcr_uid
) && (geteuid() != pcr
->pcr_uid
)) {
108 "lose_credentials: uid %d != uid %d, or euid %d != uid %d",
109 getuid(), pcr
->pcr_uid
, geteuid(), pcr
->pcr_uid
);
113 * ... or the gid is neither the actual or effective
114 * gid of the mount process.
116 if ((getgid() != pcr
->pcr_gid
) && (getegid() != pcr
->pcr_gid
)) {
118 "lose_credentials: gid %d != gid %d, or egid %d != gid %d",
119 getgid(), pcr
->pcr_gid
, getegid(), pcr
->pcr_gid
);
123 * If we make it here, we have a uid _and_ gid match! Allow the
126 DEBUG_SYSLOG(LOG_DEBUG
, "Returning successfully!\n");
131 portal_file(struct portal_cred
*pcr
, char *key
, char **v
, int *fdp
)
134 char pbuf
[MAXPATHLEN
];
136 int origuid
, origgid
;
141 strlcpy(pbuf
+ 1, key
+ (v
[1] ? strlen(v
[1]) : 0), sizeof(pbuf
) - 1);
142 DEBUG_SYSLOG(LOG_DEBUG
, "path = %s, uid = %d, gid = %d",
143 pbuf
, pcr
->pcr_uid
, pcr
->pcr_gid
);
145 if ((error
= lose_credentials(pcr
)) != 0) {
146 DEBUG_SYSLOG(LOG_DEBUG
, "portal_file: Credential err %d", error
);
151 * Be careful -- only set error to errno if there is an error.
152 * errno could hold an old, uncaught value, from a routine
153 * called long before now.
155 fd
= open(pbuf
, O_RDWR
| O_CREAT
, 0666);
158 if (error
== EACCES
) {
159 DEBUG_SYSLOG(LOG_DEBUG
, "Error: could not open '%s' "
160 "read/write with create flag. "
161 "Trying read-only open...", pbuf
);
162 /* Try opening read-only. */
163 fd
= open(pbuf
, O_RDONLY
, 0);
166 DEBUG_SYSLOG(LOG_DEBUG
, "That failed too! %m");
168 /* Clear the error indicator. */
172 DEBUG_SYSLOG(LOG_DEBUG
, "Error: could not open '%s': %m", pbuf
);
176 if (seteuid((uid_t
) origuid
) < 0) { /* XXX - should reset gidset
179 syslog(LOG_WARNING
, "setcred: %m");
188 DEBUG_SYSLOG(LOG_DEBUG
, "pt_file returns *fdp = %d, error = %d", *fdp
,