1 /* $NetBSD: checkpasswd.c,v 1.9 2011/01/06 02:45:13 jakllsch 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(const char *prompt) 41 { 42 int c; 43 char *lp; 44 static char buf[128]; /* == _PASSWORD_LEN */ 45 46 printf("%s", prompt); 47 48 for (lp = buf;;) { 49 switch (c = getchar() & 0177) { 50 case '\n': 51 case '\r': 52 *lp = '\0'; 53 putchar('\n'); 54 return buf; 55 case '\b': 56 case '\177': 57 if (lp > buf) { 58 lp--; 59 putchar('\b'); 60 putchar(' '); 61 putchar('\b'); 62 } 63 break; 64 #if HASH_ERASE 65 case '#': 66 if (lp > buf) 67 --lp; 68 break; 69 #endif 70 case 'r'&037: { 71 char *p; 72 73 putchar('\n'); 74 for (p = buf; p < lp; ++p) 75 putchar('*'); 76 break; 77 } 78 #if AT_ERASE 79 case '@': 80 #endif 81 case 'u'&037: 82 case 'w'&037: 83 lp = buf; 84 putchar('\n'); 85 break; 86 default: 87 *lp++ = c; 88 putchar('*'); 89 break; 90 } 91 } 92 /*NOTREACHED*/ 93 } 94 95 #include <sys/md5.h> 96 97 char bootpasswd[16] = {'\0'}; /* into data segment! */ 98 99 int 100 checkpasswd(void) 101 { 102 103 return check_password(bootpasswd); 104 } 105 106 int 107 check_password(const char *password) 108 { 109 int i; 110 char *passwd; 111 MD5_CTX md5ctx; 112 char pwdigest[16]; 113 114 for (i = 0; i < 16; i++) 115 if (password[i]) 116 break; 117 if (i == 16) 118 return 1; /* no password set */ 119 120 for (i = 0; i < 3; i++) { 121 passwd = getpass("Password: "); 122 MD5Init(&md5ctx); 123 MD5Update(&md5ctx, passwd, strlen(passwd)); 124 MD5Final(pwdigest, &md5ctx); 125 if (memcmp(pwdigest, password, 16) == 0) 126 return 1; 127 } 128 129 /* failed */ 130 return 0; 131 } 132