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