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