1 /* $OpenBSD: authenticate.c,v 1.25 2015/11/24 22:03:33 millert Exp $ */ 2 3 /*- 4 * Copyright (c) 1997 Berkeley Software Design, Inc. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by Berkeley Software Design, 17 * Inc. 18 * 4. The name of Berkeley Software Design, Inc. may not be used to endorse 19 * or promote products derived from this software without specific prior 20 * written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN, INC. ``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 BERKELEY SOFTWARE DESIGN, INC. 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 * BSDI $From: authenticate.c,v 2.21 1999/09/08 22:33:26 prb Exp $ 35 */ 36 37 #include <sys/stat.h> 38 39 #include <ctype.h> 40 #include <err.h> 41 #include <fcntl.h> 42 #include <limits.h> 43 #include <login_cap.h> 44 #include <paths.h> 45 #include <pwd.h> 46 #include <stdarg.h> 47 #include <stdio.h> 48 #include <stdlib.h> 49 #include <string.h> 50 #include <syslog.h> 51 #include <unistd.h> 52 53 #include <bsd_auth.h> 54 55 static int _auth_checknologin(login_cap_t *, int); 56 57 char * 58 auth_mkvalue(char *value) 59 { 60 char *big, *p; 61 62 big = malloc(strlen(value) * 4 + 1); 63 if (big == NULL) 64 return (NULL); 65 /* 66 * XXX - There should be a more standardized 67 * routine for doing this sort of thing. 68 */ 69 for (p = big; *value; ++value) { 70 switch (*value) { 71 case '\r': 72 *p++ = '\\'; 73 *p++ = 'r'; 74 break; 75 case '\n': 76 *p++ = '\\'; 77 *p++ = 'n'; 78 break; 79 case '\\': 80 *p++ = '\\'; 81 *p++ = *value; 82 break; 83 case '\t': 84 case ' ': 85 if (p == big) 86 *p++ = '\\'; 87 *p++ = *value; 88 break; 89 default: 90 if (!isprint((unsigned char)*value)) { 91 *p++ = '\\'; 92 *p++ = ((*value >> 6) & 0x3) + '0'; 93 *p++ = ((*value >> 3) & 0x7) + '0'; 94 *p++ = ((*value ) & 0x7) + '0'; 95 } else 96 *p++ = *value; 97 break; 98 } 99 } 100 *p = '\0'; 101 return (big); 102 } 103 DEF_WEAK(auth_mkvalue); 104 105 void 106 auth_checknologin(login_cap_t *lc) 107 { 108 if (_auth_checknologin(lc, 1)) 109 exit(1); 110 } 111 DEF_WEAK(auth_checknologin); 112 113 static int 114 _auth_checknologin(login_cap_t *lc, int print) 115 { 116 struct stat sb; 117 char *nologin; 118 int mustfree; 119 120 if (login_getcapbool(lc, "ignorenologin", 0)) 121 return (0); 122 123 /* 124 * If we fail to get the nologin file due to a database error, 125 * assume there should have been one... 126 */ 127 nologin = login_getcapstr(lc, "nologin", "", NULL); 128 mustfree = nologin && *nologin != '\0'; 129 if (nologin == NULL) 130 goto print_nologin; 131 132 /* First try the nologin file specified in login.conf. */ 133 if (*nologin != '\0' && stat(nologin, &sb) == 0) 134 goto print_nologin; 135 if (mustfree) { 136 free(nologin); 137 mustfree = 0; 138 } 139 140 /* If that doesn't exist try _PATH_NOLOGIN. */ 141 if (stat(_PATH_NOLOGIN, &sb) == 0) { 142 nologin = _PATH_NOLOGIN; 143 goto print_nologin; 144 } 145 146 /* Couldn't stat any nologin files, must be OK to login. */ 147 return (0); 148 149 print_nologin: 150 if (print) { 151 if (!nologin || *nologin == '\0' || auth_cat(nologin) == 0) { 152 puts("Logins are not allowed at this time."); 153 fflush(stdout); 154 } 155 } 156 if (mustfree) 157 free(nologin); 158 return (-1); 159 } 160 161 int 162 auth_cat(char *file) 163 { 164 int fd, nchars; 165 char tbuf[8192]; 166 167 if ((fd = open(file, O_RDONLY, 0)) < 0) 168 return (0); 169 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0) 170 (void)write(fileno(stdout), tbuf, nchars); 171 (void)close(fd); 172 return (1); 173 } 174 DEF_WEAK(auth_cat); 175 176 int 177 auth_approval(auth_session_t *as, login_cap_t *lc, char *name, char *type) 178 { 179 int close_on_exit, close_lc_on_exit, len; 180 struct passwd pwstore, *pwd; 181 char *approve, *s, path[PATH_MAX], pwbuf[_PW_BUF_LEN]; 182 183 pwd = NULL; 184 close_on_exit = as == NULL; 185 close_lc_on_exit = lc == NULL; 186 187 if (as != NULL && name == NULL) 188 name = auth_getitem(as, AUTHV_NAME); 189 190 if (as != NULL) 191 pwd = auth_getpwd(as); 192 193 if (pwd == NULL) { 194 if (name != NULL) { 195 getpwnam_r(name, &pwstore, pwbuf, sizeof(pwbuf), &pwd); 196 } else { 197 getpwuid_r(getuid(), &pwstore, pwbuf, sizeof(pwbuf), 198 &pwd); 199 if (pwd == NULL) { 200 syslog(LOG_ERR, "no such user id %u", getuid()); 201 warnx("cannot approve who we don't recognize"); 202 return (0); 203 } 204 name = pwd->pw_name; 205 } 206 } 207 208 if (name == NULL) 209 name = pwd->pw_name; 210 211 if (lc == NULL) { 212 if (strlen(name) >= PATH_MAX) { 213 syslog(LOG_ERR, "username to login %.*s...", 214 PATH_MAX, name); 215 warnx("username too long"); 216 return (0); 217 } 218 if (pwd == NULL && (approve = strchr(name, '.')) != NULL) { 219 strlcpy(path, name, sizeof path); 220 path[approve-name] = '\0'; 221 getpwnam_r(name, &pwstore, pwbuf, sizeof(pwbuf), &pwd); 222 } 223 lc = login_getclass(pwd ? pwd->pw_class : NULL); 224 if (lc == NULL) { 225 warnx("unable to classify user"); 226 return (0); 227 } 228 } 229 230 if (!type) 231 type = LOGIN_DEFSERVICE; 232 else { 233 if (strncmp(type, "approve-", 8) == 0) 234 type += 8; 235 236 len = snprintf(path, sizeof(path), "approve-%s", type); 237 if (len < 0 || len >= sizeof(path)) { 238 if (close_lc_on_exit) 239 login_close(lc); 240 syslog(LOG_ERR, "approval path too long %.*s...", 241 PATH_MAX, type); 242 warnx("approval script path too long"); 243 return (0); 244 } 245 } 246 247 if ((approve = login_getcapstr(lc, s = path, NULL, NULL)) == NULL) 248 approve = login_getcapstr(lc, s = "approve", NULL, NULL); 249 250 if (approve && approve[0] != '/') { 251 if (close_lc_on_exit) 252 login_close(lc); 253 syslog(LOG_ERR, "Invalid %s script: %s", s, approve); 254 warnx("invalid path to approval script"); 255 free(approve); 256 return (0); 257 } 258 259 if (as == NULL && (as = auth_open()) == NULL) { 260 if (close_lc_on_exit) 261 login_close(lc); 262 syslog(LOG_ERR, "%m"); 263 warn(NULL); 264 free(approve); 265 return (0); 266 } 267 268 auth_setstate(as, AUTH_OKAY); 269 if (auth_setitem(as, AUTHV_NAME, name) < 0) { 270 syslog(LOG_ERR, "%m"); 271 warn(NULL); 272 goto out; 273 } 274 if (auth_check_expire(as) < 0) /* is this account expired */ 275 goto out; 276 if (_auth_checknologin(lc, 277 auth_getitem(as, AUTHV_INTERACTIVE) != NULL)) { 278 auth_setstate(as, (auth_getstate(as) & ~AUTH_ALLOW)); 279 goto out; 280 } 281 if (login_getcapbool(lc, "requirehome", 0) && pwd && pwd->pw_dir && 282 pwd->pw_dir[0]) { 283 struct stat sb; 284 285 if (stat(pwd->pw_dir, &sb) < 0 || 286 (sb.st_mode & 0170000) != S_IFDIR || 287 (pwd->pw_uid && sb.st_uid == pwd->pw_uid && 288 (sb.st_mode & S_IXUSR) == 0)) { 289 auth_setstate(as, (auth_getstate(as) & ~AUTH_ALLOW)); 290 goto out; 291 } 292 } 293 if (approve) 294 auth_call(as, approve, strrchr(approve, '/') + 1, name, 295 lc->lc_class, type, (char *)NULL); 296 297 out: 298 free(approve); 299 if (close_lc_on_exit) 300 login_close(lc); 301 302 if (close_on_exit) 303 return (auth_close(as)); 304 return (auth_getstate(as) & AUTH_ALLOW); 305 } 306 DEF_WEAK(auth_approval); 307 308 auth_session_t * 309 auth_usercheck(char *name, char *style, char *type, char *password) 310 { 311 char namebuf[LOGIN_NAME_MAX + 1 + NAME_MAX + 1]; 312 char pwbuf[_PW_BUF_LEN]; 313 auth_session_t *as; 314 login_cap_t *lc; 315 struct passwd pwstore, *pwd = NULL; 316 char *slash; 317 318 if (strlcpy(namebuf, name, sizeof(namebuf)) >= sizeof(namebuf)) 319 return (NULL); 320 name = namebuf; 321 322 /* 323 * Split up user:style names if we were not given a style 324 */ 325 if (style == NULL && (style = strchr(name, ':')) != NULL) 326 *style++ = '\0'; 327 328 /* 329 * Cope with user/instance. We are only using this to get 330 * the class so it is okay if we strip a /root instance 331 * The actual login script will pay attention to the instance. 332 */ 333 getpwnam_r(name, &pwstore, pwbuf, sizeof(pwbuf), &pwd); 334 if (pwd == NULL) { 335 if ((slash = strchr(name, '/')) != NULL) { 336 *slash = '\0'; 337 getpwnam_r(name, &pwstore, pwbuf, sizeof(pwbuf), &pwd); 338 *slash = '/'; 339 } 340 } 341 if ((lc = login_getclass(pwd ? pwd->pw_class : NULL)) == NULL) 342 return (NULL); 343 344 if ((style = login_getstyle(lc, style, type)) == NULL) { 345 login_close(lc); 346 return (NULL); 347 } 348 349 if (password) { 350 if ((as = auth_open()) == NULL) { 351 login_close(lc); 352 return (NULL); 353 } 354 auth_setitem(as, AUTHV_SERVICE, "response"); 355 auth_setdata(as, "", 1); 356 auth_setdata(as, password, strlen(password) + 1); 357 explicit_bzero(password, strlen(password)); 358 } else 359 as = NULL; 360 as = auth_verify(as, style, name, lc->lc_class, (char *)NULL); 361 login_close(lc); 362 return (as); 363 } 364 DEF_WEAK(auth_usercheck); 365 366 int 367 auth_userokay(char *name, char *style, char *type, char *password) 368 { 369 auth_session_t *as; 370 371 as = auth_usercheck(name, style, type, password); 372 373 return (as != NULL ? auth_close(as) : 0); 374 } 375 DEF_WEAK(auth_userokay); 376 377 auth_session_t * 378 auth_userchallenge(char *name, char *style, char *type, char **challengep) 379 { 380 char namebuf[LOGIN_NAME_MAX + 1 + NAME_MAX + 1]; 381 auth_session_t *as; 382 login_cap_t *lc; 383 struct passwd pwstore, *pwd = NULL; 384 char *slash, pwbuf[_PW_BUF_LEN]; 385 386 if (strlen(name) >= sizeof(namebuf)) 387 return (NULL); 388 strlcpy(namebuf, name, sizeof namebuf); 389 name = namebuf; 390 391 /* 392 * Split up user:style names if we were not given a style 393 */ 394 if (style == NULL && (style = strchr(name, ':')) != NULL) 395 *style++ = '\0'; 396 397 /* 398 * Cope with user/instance. We are only using this to get 399 * the class so it is okay if we strip a /root instance 400 * The actual login script will pay attention to the instance. 401 */ 402 getpwnam_r(name, &pwstore, pwbuf, sizeof(pwbuf), &pwd); 403 if (pwd == NULL) { 404 if ((slash = strchr(name, '/')) != NULL) { 405 *slash = '\0'; 406 getpwnam_r(name, &pwstore, pwbuf, sizeof(pwbuf), &pwd); 407 *slash = '/'; 408 } 409 } 410 if ((lc = login_getclass(pwd ? pwd->pw_class : NULL)) == NULL) 411 return (NULL); 412 413 if ((style = login_getstyle(lc, style, type)) == NULL || 414 (as = auth_open()) == NULL) { 415 login_close(lc); 416 return (NULL); 417 } 418 if (auth_setitem(as, AUTHV_STYLE, style) < 0 || 419 auth_setitem(as, AUTHV_NAME, name) < 0 || 420 auth_setitem(as, AUTHV_CLASS, lc->lc_class) < 0) { 421 auth_close(as); 422 login_close(lc); 423 return (NULL); 424 } 425 login_close(lc); 426 *challengep = auth_challenge(as); 427 return (as); 428 } 429 DEF_WEAK(auth_userchallenge); 430 431 int 432 auth_userresponse(auth_session_t *as, char *response, int more) 433 { 434 char path[PATH_MAX]; 435 char *style, *name, *challenge, *class; 436 int len; 437 438 if (as == NULL) 439 return (0); 440 441 auth_setstate(as, 0); 442 443 if ((style = auth_getitem(as, AUTHV_STYLE)) == NULL || 444 (name = auth_getitem(as, AUTHV_NAME)) == NULL) { 445 if (more == 0) 446 return (auth_close(as)); 447 return(0); 448 } 449 450 len = snprintf(path, sizeof(path), _PATH_AUTHPROG "%s", style); 451 if (len < 0 || len >= sizeof(path)) { 452 if (more == 0) 453 return (auth_close(as)); 454 return (0); 455 } 456 457 challenge = auth_getitem(as, AUTHV_CHALLENGE); 458 class = auth_getitem(as, AUTHV_CLASS); 459 460 if (challenge) 461 auth_setdata(as, challenge, strlen(challenge) + 1); 462 else 463 auth_setdata(as, "", 1); 464 if (response) { 465 auth_setdata(as, response, strlen(response) + 1); 466 explicit_bzero(response, strlen(response)); 467 } else 468 auth_setdata(as, "", 1); 469 470 auth_call(as, path, style, "-s", "response", name, class, (char *)NULL); 471 472 /* 473 * If they authenticated then make sure they did not expire 474 */ 475 if (auth_getstate(as) & AUTH_ALLOW) 476 auth_check_expire(as); 477 if (more == 0) 478 return (auth_close(as)); 479 return (auth_getstate(as) & AUTH_ALLOW); 480 } 481 DEF_WEAK(auth_userresponse); 482 483 /* 484 * Authenticate name with the specified style. 485 * If ``as'' is NULL a new session is formed with the default service. 486 * Returns NULL only if ``as'' is NULL and we were unable to allocate 487 * a new session. 488 * 489 * Use auth_close() or auth_getstate() to determine if the authentication 490 * worked. 491 */ 492 auth_session_t * 493 auth_verify(auth_session_t *as, char *style, char *name, ...) 494 { 495 va_list ap; 496 char path[PATH_MAX]; 497 498 if ((name == NULL || style == NULL) && as == NULL) 499 return (as); 500 501 if (as == NULL && (as = auth_open()) == NULL) 502 return (NULL); 503 auth_setstate(as, 0); 504 505 if (style != NULL && auth_setitem(as, AUTHV_STYLE, style) < 0) 506 return (as); 507 508 if (name != NULL && auth_setitem(as, AUTHV_NAME, name) < 0) 509 return (as); 510 511 style = auth_getitem(as, AUTHV_STYLE); 512 name = auth_getitem(as, AUTHV_NAME); 513 514 snprintf(path, sizeof(path), _PATH_AUTHPROG "%s", style); 515 va_start(ap, name); 516 auth_set_va_list(as, ap); 517 auth_call(as, path, auth_getitem(as, AUTHV_STYLE), "-s", 518 auth_getitem(as, AUTHV_SERVICE), name, (char *)NULL); 519 va_end(ap); 520 return (as); 521 } 522 DEF_WEAK(auth_verify); 523