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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
|
/*
* Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006, 2008,
* 2009, 2010, 2011, 2012, 2013
* Inferno Nettverk A/S, Norway. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. The above copyright notice, this list of conditions and the following
* disclaimer must appear in all copies of the software, derivative works
* or modified versions, and any portions thereof, aswell as in all
* supporting documentation.
* 2. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by
* Inferno Nettverk A/S, Norway.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Inferno Nettverk A/S requests users of this software to return to
*
* Software Distribution Coordinator or sdc@inet.no
* Inferno Nettverk A/S
* Oslo Research Park
* Gaustadallen 21
* NO-0349 Oslo
* Norway
*
* any improvements or extensions that they make and grant Inferno Nettverk A/S
* the rights to redistribute these changes.
*
*/
#include "common.h"
static const char rcsid[] =
"$Id: accesscheck.c,v 1.89 2013/10/27 15:24:42 karls Exp $";
int
usermatch(auth, userlist)
const authmethod_t *auth;
const linkedname_t *userlist;
{
/* const char *function = "usermatch()"; */
const char *name;
if ((name = authname(auth)) == NULL)
return 0; /* no username, no match. */
do
if (strcmp(name, userlist->name) == 0)
break;
while ((userlist = userlist->next) != NULL);
if (userlist == NULL)
return 0; /* no match. */
return 1;
}
int
groupmatch(auth, grouplist)
const authmethod_t *auth;
const linkedname_t *grouplist;
{
const char *function = "groupmatch()";
const char *username;
struct passwd *pw;
struct group *groupent;
SASSERTX(grouplist != NULL);
if ((username = authname(auth)) == NULL)
return 0; /* no username, no match. */
/*
* First check the primary group of the user against grouplist.
* If the groupname given there matches, we don't need to go through
* all users in the list of group.
*/
if ((pw = getpwnam(username)) != NULL
&& (groupent = getgrgid(pw->pw_gid)) != NULL) {
const linkedname_t *listent = grouplist;
do
if (strcmp(groupent->gr_name, listent->name) == 0)
return 1;
while ((listent = listent->next) != NULL);
}
else {
if (pw == NULL)
slog(LOG_DEBUG, "%s: unknown username \"%s\"", function, username);
else if (groupent == NULL)
slog(LOG_DEBUG, "%s: unknown primary groupid %ld",
function, (long)pw->pw_gid);
}
/*
* Go through grouplist, matching username against each groupmember of
* all the groups in grouplist.
*/
do {
char **groupname;
if ((groupent = getgrnam(grouplist->name)) == NULL) {
swarn("%s: unknown groupname \"%s\"", function, grouplist->name);
continue;
}
groupname = groupent->gr_mem;
while (*groupname != NULL) {
if (strcmp(username, *groupname) == 0)
return 1; /* match. */
++groupname;
}
} while ((grouplist = grouplist->next) != NULL);
return 0;
}
#if HAVE_LDAP
int
ldapgroupmatch(auth, rule)
const authmethod_t *auth;
const rule_t *rule;
{
const char *function = "ldapgroupmatch()";
const linkedname_t *grouplist;
const char *username;
char *userdomain, *groupdomain;
int retval;
if ((username = authname(auth)) == NULL)
return 0; /* no username, no match. */
#if !HAVE_GSSAPI
if (!rule->state.ldap.ldapurl)
SERRX(rule->state.ldap.ldapurl != NULL);
#endif /* !HAVE_GSSAPI */
if ((userdomain = strchr(username, '@')) != NULL)
++userdomain;
if (userdomain == NULL && *rule->state.ldap.domain == NUL && rule->state.ldap.ldapurl == NULL) {
slog(LOG_DEBUG, "%s: cannot check ldap group membership for user %s: "
"user has no domain postfix and no ldap url is defined",
function, username);
return 0;
}
if ((retval = ldap_user_is_cached(username)) >= 0)
return retval;
/* go through grouplist, matching username against members of each group. */
grouplist = rule->ldapgroup;
do {
char groupname[MAXNAMELEN];
slog(LOG_DEBUG, "%s: checking if user %s is member of ldap group %s",
function, username, grouplist->name);
STRCPY_ASSERTLEN(groupname, grouplist->name);
if ((groupdomain = strchr(groupname, '@')) != NULL) {
*groupdomain = NUL; /* separates groupname from groupdomain. */
++groupdomain;
}
if (groupdomain != NULL && userdomain != NULL) {
if (strcmp(groupdomain, userdomain) != 0
&& strcmp(groupdomain, "") != 0) {
slog(LOG_DEBUG, "%s: userdomain \"%s\" does not match groupdomain "
"\"%s\" and groupdomain is not default domain. "
"Trying next entry",
function, userdomain, groupdomain);
continue;
}
}
if (ldapgroupmatches(username, userdomain, groupname, groupdomain, rule)){
cache_ldap_user(username, 1);
return 1;
}
} while ((grouplist = grouplist->next) != NULL);
cache_ldap_user(username, 0);
return 0;
}
#endif /* HAVE_LDAP */
int
accesscheck(s, auth, src, dst, emsg, emsgsize)
int s;
authmethod_t *auth;
const struct sockaddr_storage *src, *dst;
char *emsg;
size_t emsgsize;
{
const char *function = "accesscheck()";
char srcstr[MAXSOCKADDRSTRING], dststr[sizeof(srcstr)];
int match, authresultisfixed;
if (sockscf.option.debug)
slog(LOG_DEBUG, "%s: method: %s, %s -> %s ",
function,
method2string(auth->method),
src == NULL ?
"<unknown>" : sockaddr2string(src, srcstr, sizeof(srcstr)),
dst == NULL ?
"<unknown>" : sockaddr2string(dst, dststr, sizeof(dststr)));
/*
* We don't want to re-check the same method. This could
* happen in several cases:
* - was checked as client-rule, is now checked as socks-rule.
* - a different rule with the same method. The client is however
* the same, so if the auth for method 'k' failed in previous rule,
* it will fail the next time also.
*/
if (methodisset(auth->method, auth->methodv, (size_t)auth->methodc)) {
slog(LOG_DEBUG, "%s: method %s already checked, matches",
function, method2string(auth->method));
return 1; /* already checked, matches. */
}
if (methodisset(auth->method, auth->badmethodv, (size_t)auth->badmethodc)) {
snprintf(emsg, emsgsize,
"authentication provided by client for method \"%s\" "
"does not match",
method2string(auth->method));
slog(LOG_DEBUG, "%s: method %s already checked, does not match",
function, method2string(auth->method));
return 0; /* already checked, won't match. */
}
match = 0;
switch (auth->method) {
/*
* Methods where no further checking is done at this point, either
* because there's nothing to check, or it has already been checked.
*/
case AUTHMETHOD_NONE:
#if HAVE_GSSAPI
case AUTHMETHOD_GSSAPI:
#endif /* HAVE_GSSAPI */
match = 1;
break;
case AUTHMETHOD_UNAME:
if (passwordcheck((const char *)auth->mdata.uname.name,
(const char *)auth->mdata.uname.password,
emsg,
emsgsize) == 0)
match = 1;
break;
#if HAVE_LIBWRAP
case AUTHMETHOD_RFC931:
if (passwordcheck((const char *)auth->mdata.rfc931.name,
NULL,
emsg,
emsgsize) == 0)
match = 1;
break;
#endif /* HAVE_LIBWRAP */
#if HAVE_PAM
case AUTHMETHOD_PAM_ANY:
case AUTHMETHOD_PAM_ADDRESS:
case AUTHMETHOD_PAM_USERNAME: {
#if DIAGNOSTIC
const int freec
= freedescriptors(sockscf.option.debug ? "start" : NULL, NULL);
#endif /* DIAGNOSTIC */
if (pam_passwordcheck(s,
src,
dst,
&auth->mdata.pam,
emsg,
emsgsize) == 0)
match = 1;
#if DIAGNOSTIC
if (freec
!= freedescriptors(sockscf.option.debug ? "end" : NULL, NULL))
swarnx("%s: lost %d file descriptor%s in pam_passwordcheck()",
function,
freec - freedescriptors(NULL, NULL),
freec - freedescriptors(NULL, NULL) == 1 ? "" : "s");
#endif /* DIAGNOSTIC */
break;
}
#endif /* HAVE_PAM */
#if HAVE_BSDAUTH
case AUTHMETHOD_BSDAUTH: {
#if DIAGNOSTIC
const int freec
= freedescriptors(sockscf.option.debug ? "start" : NULL, NULL);
#endif /* DIAGNOSTIC */
if (bsdauth_passwordcheck(s,
src,
dst,
&auth->mdata.bsd,
emsg,
emsgsize) == 0)
match = 1;
#if DIAGNOSTIC
if (freec
!= freedescriptors(sockscf.option.debug ? "end" : NULL, NULL))
swarnx("%s: lost %d file descriptor%s in bsdauth_passwordcheck()",
function,
freec - freedescriptors(NULL, NULL),
freec - freedescriptors(NULL, NULL) == 1 ? "" : "s");
#endif /* DIAGNOSTIC */
break;
}
#endif /* HAVE_BSDAUTH */
default:
SERRX(auth->method);
}
/*
* Some methods can be called with different values for the
* same client, based on values configured in the rules.
* Others cannot and we want to mark those that cannot as
* "tried", so we don't waste time on re-trying them.
*/
switch (auth->method) {
#if HAVE_PAM
case AUTHMETHOD_PAM_ANY:
case AUTHMETHOD_PAM_ADDRESS:
case AUTHMETHOD_PAM_USERNAME:
if (*sockscf.state.pamservicename == NUL)
authresultisfixed = 0;
else
authresultisfixed = 1;
break;
#endif /* HAVE_PAM */
#if HAVE_BSDAUTH
case AUTHMETHOD_BSDAUTH:
if (sockscf.state.bsdauthstylename == NULL)
authresultisfixed = 0;
else
authresultisfixed = 1;
break;
#endif /* HAVE_PAM */
#if HAVE_GSSAPI
case AUTHMETHOD_GSSAPI:
if (*sockscf.state.gssapiservicename == NUL
|| *sockscf.state.gssapikeytab == NUL)
authresultisfixed = 0;
else
authresultisfixed = 1;
break;
#endif /* HAVE_GSSAPI */
case AUTHMETHOD_NONE:
case AUTHMETHOD_UNAME:
case AUTHMETHOD_RFC931:
authresultisfixed = 1;
break;
default:
SERRX(auth->method);
}
if (authresultisfixed) {
if (match) {
SASSERTX(auth->methodc + 1 <= ELEMENTS(auth->methodv));
auth->methodv[auth->methodc++] = auth->method;
}
else {
SASSERTX(auth->badmethodc + 1 <= ELEMENTS(auth->badmethodv));
auth->badmethodv[auth->badmethodc++] = auth->method;
}
/*
* Unfortunately we can not bzero() the password for several reasons:
* 1) If UDP, perhaps packets to different targets will require different
* authentication. E.g. username was used when establishing the
* control-connection, while pam is used when sending packets to
* certain targets. Unlikely, but not impossible.
*
* 2) If we are forwarding to an upstream proxy that we are
* configured to offer the username/password from the user.
*/
}
if (match)
slog(LOG_DEBUG, "%s: authentication matched", function);
else
slog(LOG_DEBUG, "%s: no match for authentication%s %s",
function,
emsgsize > 0 ? ":" : "",
emsgsize > 0 ? emsg : "");
return match;
}
|