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