1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*
* auth_clnt.c This module takes care of request authorization.
*
* Authors: Don Becker, <becker@super.org>
* Rick Sladkey, <jrs@world.std.com>
* Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
* Olaf Kirch, <okir@monad.swb.de>
*
* This software maybe be used for any purpose provided
* the above copyright notice is retained. It is supplied
* as is, with no warranty expressed or implied.
*/
#include "nfsd.h"
#include "fakefsuid.h"
#ifndef svc_getcaller
#define svc_getcaller(x) ((struct sockaddr_in *) &(x)->xp_rtaddr.buf)
#endif
#if defined(HAVE_SETFSUID) || defined(MAYBE_HAVE_SETFSUID)
static void setfsids(uid_t, gid_t, gid_t *, int);
#endif
#ifndef HAVE_SETFSUID
static void seteids(uid_t, gid_t, gid_t *, int);
#endif
uid_t auth_uid = 0; /* Current effective user ids */
gid_t auth_gid = 0;
GETGROUPS_T auth_gids[NGRPS]; /* Current supplementary gids */
int auth_gidlen = -1;
uid_t cred_uid;
gid_t cred_gid;
gid_t *cred_gids;
int cred_len;
#ifdef HAVE_AUTHDES_GETUCRED
/* authdes_getucred is not exported in svcauth.h even if present. */
extern int authdes_getucred(caddr_t credptr, short *uid,
short *gid, short *nrgids, int *groups);
#endif
/*
* For an RPC request, look up the NFS client info along with the
* list of directories exported to that client.
*/
nfs_client *
auth_clnt(struct svc_req *rqstp)
{
nfs_client *cp = NULL;
struct in_addr addr = svc_getcaller(rqstp->rq_xprt)->sin_addr;
/* Get the client and list of exports */
if ((cp = auth_clientbyaddr(addr)) != NULL)
return cp;
/* We don't know you */
if (trace_spoof) {
Dprintf(L_ERROR, "Unauthorized access by NFS client %s.\n",
inet_ntoa(addr));
}
return (NULL);
}
nfs_mount *
auth_path(nfs_client *cp, struct svc_req *rqstp, char *path)
{
nfs_mount *mp;
/* Check if the specified client is permitted to access this */
if ((mp = auth_match_mount(cp, path)) == NULL) {
if (cp->flags != 0 || trace_spoof) {
Dprintf(L_ERROR, "NFS client %s tried to access %s\n",
cp->clnt_name, path);
}
return NULL;
}
/* Check request originated on a privileged port. */
if (!allow_non_root && mp->o.secure_port &&
ntohs(svc_getcaller(rqstp->rq_xprt)->sin_port) >= IPPORT_RESERVED) {
Dprintf(L_ERROR,
"NFS request from %s originated on insecure port, %s\n",
cp->clnt_name,
"psychoanalysis suggested");
return (NULL);
}
if (logging_enabled(D_AUTH)) {
Dprintf(D_AUTH, "auth_path(%s): mount point %s, (%s%s%s%s%s)\n",
path, mp->path,
mp->o.all_squash? "all_squash " : (
mp->o.root_squash? "root_squash " : ""),
(mp->o.uidmap == map_daemon)? "uidmap " : "",
mp->o.secure_port? "secure " : "insecure ",
mp->o.link_relative? "linkrel " : "",
mp->o.read_only? "ro" : "rw");
}
return mp;
}
void auth_user(nfs_mount *mp, struct svc_req *rqstp)
{
uid_t cuid;
gid_t cgid;
GETGROUPS_T cgids[NGRPS];
int squash = mp->o.all_squash;
int cred_set, i, clen;
cred_set = 0;
if (rqstp->rq_cred.oa_flavor == AUTH_UNIX) {
struct authunix_parms *unix_cred;
unix_cred = (struct authunix_parms *) rqstp->rq_clntcred;
cred_uid = unix_cred->aup_uid;
cred_gid = unix_cred->aup_gid;
cred_len = unix_cred->aup_len;
cred_gids = unix_cred->aup_gids;
cred_set = 1;
#ifdef HAVE_AUTHDES_GETUCRED
} else if (rqstp->rq_cred.oa_flavor == AUTH_DES) {
static GETGROUPS_T des_gids[NGRPS];
short uid, gid, grplen = NGRPS;
int groups[NGRPS], i;
i = authdes_getucred(rqstp->rq_clntcred, &uid, &gid,
&grplen, groups);
if (i != 0) {
cred_set = 1;
cred_uid = (uid_t) uid;
cred_gid = (gid_t) gid;
cred_len = grplen;
cred_gids = des_gids;
if (cred_len < 0)
cred_len = 0;
else if (cred_len > NGRPS)
cred_len = NGRPS;
for (i = 0; i < cred_len; i++)
cred_gids[i] = groups[i];
}
#endif
}
/* We will want to support AUTH_DES/AUTH_KRB one day,
* but for now we treat all other authentication flavor
* as AUTH_NULL.
*/
if (!cred_set)
squash = 1;
if (!squash) {
/* Do the uid/gid mapping here.
* Note that we check cred_uid (which is a short), not
* unix_cred->aup_uid to avoid the uid masking bug.
*/
if (cred_len < 0)
cred_len = 0;
else if (cred_len > NGRPS)
cred_len = NGRPS;
cuid = luid(cred_uid, mp, rqstp);
cgid = lgid(cred_gid, mp, rqstp);
clen = cred_len;
for (i = 0; i < cred_len; i++)
cgids[i] = lgid(cred_gids[i], mp, rqstp);
} else {
cuid = mp->o.nobody_uid;
cgid = mp->o.nobody_gid;
/* Construct a list of one gid. */
cgids[0] = cgid;
clen = 1;
}
/* This code is a little awkward because setfsuid has been present
* in the Linux kernel for quite some time but not in libc.
* The startup code tests for the setfsuid syscall and sets
* have_setfsuid accordingly.
*/
#if defined(HAVE_SETFSUID)
setfsids(cuid, cgid, cgids, clen);
#else
#if defined(MAYBE_HAVE_SETFSUID)
if (have_setfsuid)
setfsids(cuid, cgid, cgids, clen);
else
#endif
seteids(cuid, cgid, cgids, clen);
#endif
}
/*
* The following functions deal with setting the client's uid/gid.
*/
void
auth_override_uid(uid_t uid)
{
#if defined(HAVE_SETFSUID)
setfsuid(uid);
#else
#if defined(MAYBE_HAVE_SETFSUID)
if (have_setfsuid)
setfsuid(uid);
else
#endif
seteuid(uid);
#endif
}
#if defined(HAVE_SETFSUID) || defined(MAYBE_HAVE_SETFSUID)
static void
setfsids(uid_t cred_uid, gid_t cred_gid, gid_t *cred_gids, int cred_len)
{
/* First, set the user ID. */
if (auth_uid != cred_uid) {
if (setfsuid(cred_uid) < 0)
Dprintf(L_ERROR, "Unable to setfsuid %d: %s\n",
cred_uid, strerror(errno));
else
auth_uid = cred_uid;
}
/* Next, the group ID. */
if (auth_gid != cred_gid) {
if (setfsgid(cred_gid) < 0)
Dprintf(L_ERROR, "Unable to setfsgid %d: %s\n",
cred_gid, strerror(errno));
else
auth_gid = cred_gid;
}
#ifdef HAVE_SETGROUPS
/* Finally, set the supplementary group IDs if possible. */
if (cred_len < 0 || cred_len > NGRPS)
Dprintf(L_ERROR, "Negative or huge cred_len: %d\n", cred_len);
else if (cred_len != auth_gidlen
|| memcmp(cred_gids, auth_gids, auth_gidlen*sizeof(gid_t))) {
if (setgroups(cred_len, cred_gids) < 0)
Dprintf(L_ERROR, "Unable to setgroups: %s\n",
strerror(errno));
else {
memcpy(auth_gids, cred_gids, cred_len*sizeof(gid_t));
auth_gidlen = cred_len;
}
}
#endif /* HAVE_SETGROUPS */
}
#endif
#if !defined(HAVE_SETFSUID)
static void
seteids(uid_t cred_uid, gid_t cred_gid, gid_t *cred_gids, int cred_len)
{
/* To set any IDs we first need to be root. What a pain. */
/* First set the group ID. */
if (auth_gid != cred_gid) {
if (auth_uid != ROOT_UID) {
if (seteuid(ROOT_UID) < 0)
Dprintf(L_ERROR, "Unable to seteuid(%d): %s\n",
ROOT_UID, strerror(errno));
else
auth_uid = ROOT_UID;
}
if (setegid(cred_gid) < 0)
Dprintf(L_ERROR, "Unable to setegid(%d): %s\n",
cred_gid, strerror(errno));
else
auth_gid = cred_gid;
}
#ifdef HAVE_SETGROUPS
/* Next set the supplementary group IDs if possible. */
if (cred_len < 0 || cred_len > NGRPS)
Dprintf(L_ERROR, "Negative or huge cred_len: %d\n", cred_len);
else if (cred_len != auth_gidlen
|| memcmp(cred_gids, auth_gids, auth_gidlen*sizeof(gid_t))) {
if (auth_uid != ROOT_UID) {
if (seteuid(ROOT_UID) < 0)
Dprintf(L_ERROR, "Unable to seteuid(%d): %s\n",
ROOT_UID, strerror(errno));
else
auth_uid = ROOT_UID;
}
if (setgroups(cred_len, cred_gids) < 0)
Dprintf(L_ERROR, "Unable to setgroups: %s\n",
strerror(errno));
else {
memcpy(auth_gids, cred_gids, cred_len*sizeof(gid_t));
auth_gidlen = cred_len;
}
}
#endif /* HAVE_SETGROUPS */
/* Finally, set the user ID. */
if (auth_uid != cred_uid) {
if (auth_uid != ROOT_UID && seteuid(ROOT_UID) < 0)
Dprintf(L_ERROR, "Unable to seteuid(%d): %s\n",
ROOT_UID, strerror(errno));
if (seteuid(cred_uid) < 0)
Dprintf(L_ERROR, "Unable to seteuid(%d): %s\n",
cred_uid, strerror(errno));
else
auth_uid = cred_uid;
}
}
#endif
|