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