1 /* $NetBSD: checkpasswd.c,v 1.5 2003/04/15 22:26:42 dsl Exp $ */ 2 3 /*- 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 * @(#)gets.c 8.1 (Berkeley) 6/11/93 29 */ 30 31 #ifdef _STANDALONE 32 #include <lib/libkern/libkern.h> 33 #else 34 #include <string.h> 35 #endif 36 37 #include "stand.h" 38 39 char * 40 getpass(prompt) 41 const char *prompt; 42 { 43 int c; 44 char *lp; 45 static char buf[128]; /* == _PASSWORD_LEN */ 46 47 printf(prompt); 48 49 for (lp = buf;;) 50 switch (c = getchar() & 0177) { 51 case '\n': 52 case '\r': 53 *lp = '\0'; 54 putchar('\n'); 55 return (buf); 56 case '\b': 57 case '\177': 58 if (lp > buf) { 59 lp--; 60 putchar('\b'); 61 putchar(' '); 62 putchar('\b'); 63 } 64 break; 65 #if HASH_ERASE 66 case '#': 67 if (lp > buf) 68 --lp; 69 break; 70 #endif 71 case 'r'&037: { 72 char *p; 73 74 putchar('\n'); 75 for (p = buf; p < lp; ++p) 76 putchar('*'); 77 break; 78 } 79 #if AT_ERASE 80 case '@': 81 #endif 82 case 'u'&037: 83 case 'w'&037: 84 lp = buf; 85 putchar('\n'); 86 break; 87 default: 88 *lp++ = c; 89 putchar('*'); 90 } 91 /*NOTREACHED*/ 92 } 93 94 #include <sys/md5.h> 95 96 char bootpasswd[16] = {'\0'}; /* into data segment! */ 97 98 int 99 checkpasswd(void) 100 { 101 return check_password(bootpasswd); 102 } 103 104 int 105 check_password(const char *password) 106 { 107 int i; 108 char *passwd; 109 MD5_CTX md5ctx; 110 char pwdigest[16]; 111 112 for (i = 0; i < 16; i++) 113 if (password[i]) 114 break; 115 if (i == 16) 116 return (1); /* no password set */ 117 118 for (i = 0; i < 3; i++) { 119 passwd = getpass("Password: "); 120 MD5Init(&md5ctx); 121 MD5Update(&md5ctx, passwd, strlen(passwd)); 122 MD5Final(pwdigest, &md5ctx); 123 if (bcmp(pwdigest, password, 16) == 0) 124 return (1); 125 } 126 127 /* failed */ 128 return (0); 129 } 130 131