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