diff options
author | H. Peter Anvin <hpa@zytor.com> | 2008-02-10 22:32:46 -0800 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2008-02-10 22:32:46 -0800 |
commit | 4d4bf8c86bc16a9b75449054ce78c0ac097c86d5 (patch) | |
tree | 4bd346449b1c4e116fac21c165fa06f42f31767e | |
parent | c416cce81f71b56c41086e767bd3d96fcc81c002 (diff) | |
download | syslinux-4d4bf8c86bc16a9b75449054ce78c0ac097c86d5.tar.gz |
simple menu: break password comparisons out into a separate file
Move passwd_compare() into a separate source file, for cleanliness.
-rw-r--r-- | com32/menu/Makefile | 2 | ||||
-rw-r--r-- | com32/menu/menu.h | 3 | ||||
-rw-r--r-- | com32/menu/menumain.c | 55 | ||||
-rw-r--r-- | com32/menu/passwd.c | 64 |
4 files changed, 71 insertions, 53 deletions
diff --git a/com32/menu/Makefile b/com32/menu/Makefile index 118e196c..8d615ce2 100644 --- a/com32/menu/Makefile +++ b/com32/menu/Makefile @@ -50,7 +50,7 @@ COM32DIR = $(AUXDIR)/com32 MODULES = menu.c32 vesamenu.c32 TESTFILES = -COMMONOBJS = menumain.o readconfig.o printmsg.o +COMMONOBJS = menumain.o readconfig.o passwd.o printmsg.o all: $(MODULES) $(TESTFILES) diff --git a/com32/menu/menu.h b/com32/menu/menu.h index 1c516ef2..bf376aae 100644 --- a/com32/menu/menu.h +++ b/com32/menu/menu.h @@ -140,4 +140,7 @@ int show_message_file(const char *filename, const char *background); void set_msg_colors_global(unsigned int fg, unsigned int bg, enum color_table_shadow shadow); +/* passwd.c */ +int passwd_compare(const char *passwd, const char *entry); + #endif /* MENU_H */ diff --git a/com32/menu/menumain.c b/com32/menu/menumain.c index fad49a28..2296e7ad 100644 --- a/com32/menu/menumain.c +++ b/com32/menu/menumain.c @@ -1,11 +1,11 @@ /* ----------------------------------------------------------------------- * - * + * * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, Inc., 53 Temple Place Ste 330, - * Boston MA 02111-1307, USA; either version 2 of the License, or + * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston MA 02110-1301, USA; either version 2 of the License, or * (at your option) any later version; incorporated herein by reference. * * ----------------------------------------------------------------------- */ @@ -27,9 +27,6 @@ #include <minmax.h> #include <setjmp.h> #include <limits.h> -#include <sha1.h> -#include <md5.h> -#include <base64.h> #include <colortbl.h> #include <com32.h> @@ -293,52 +290,6 @@ draw_row(int y, int sel, int top, int sbtop, int sbbot) } } -static int passwd_compare_sha1(const char *passwd, const char *entry) -{ - const char *p; - SHA1_CTX ctx; - unsigned char sha1[20], pwdsha1[20]; - - SHA1Init(&ctx); - - if ( (p = strchr(passwd+3, '$')) ) { - SHA1Update(&ctx, (void *)passwd+3, p-(passwd+3)); - p++; - } else { - p = passwd+3; /* Assume no salt */ - } - - SHA1Update(&ctx, (void *)entry, strlen(entry)); - SHA1Final(sha1, &ctx); - - memset(pwdsha1, 0, 20); - unbase64(pwdsha1, 20, p); - - return !memcmp(sha1, pwdsha1, 20); -} - -static int passwd_compare_md5(const char *passwd, const char *entry) -{ - const char *crypted = crypt_md5(entry, passwd+3); - int len = strlen(crypted); - - return !strncmp(crypted, passwd, len) && - (passwd[len] == '\0' || passwd[len] == '$'); -} - -static int -passwd_compare(const char *passwd, const char *entry) -{ - if ( passwd[0] != '$' ) /* Plaintext passwd, yuck! */ - return !strcmp(entry, passwd); - else if ( !strncmp(passwd, "$4$", 3) ) - return passwd_compare_sha1(passwd, entry); - else if ( !strncmp(passwd, "$1$", 3) ) - return passwd_compare_md5(passwd, entry); - else - return 0; /* Invalid encryption algorithm */ -} - static jmp_buf timeout_jump; int mygetkey(clock_t timeout) diff --git a/com32/menu/passwd.c b/com32/menu/passwd.c new file mode 100644 index 00000000..4a420246 --- /dev/null +++ b/com32/menu/passwd.c @@ -0,0 +1,64 @@ +/* ----------------------------------------------------------------------- * + * + * Copyright 2004-2008 H. Peter Anvin - All Rights Reserved + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston MA 02110-1301, USA; either version 2 of the License, or + * (at your option) any later version; incorporated herein by reference. + * + * ----------------------------------------------------------------------- */ + +#include <string.h> +#include <sha1.h> +#include <md5.h> +#include <base64.h> + +#include "menu.h" + +static int passwd_compare_sha1(const char *passwd, const char *entry) +{ + const char *p; + SHA1_CTX ctx; + unsigned char sha1[20], pwdsha1[20]; + + SHA1Init(&ctx); + + if ( (p = strchr(passwd+3, '$')) ) { + SHA1Update(&ctx, (void *)passwd+3, p-(passwd+3)); + p++; + } else { + p = passwd+3; /* Assume no salt */ + } + + SHA1Update(&ctx, (void *)entry, strlen(entry)); + SHA1Final(sha1, &ctx); + + memset(pwdsha1, 0, 20); + unbase64(pwdsha1, 20, p); + + return !memcmp(sha1, pwdsha1, 20); +} + +static int passwd_compare_md5(const char *passwd, const char *entry) +{ + const char *crypted = crypt_md5(entry, passwd+3); + int len = strlen(crypted); + + return !strncmp(crypted, passwd, len) && + (passwd[len] == '\0' || passwd[len] == '$'); +} + +int passwd_compare(const char *passwd, const char *entry) +{ + if ( passwd[0] != '$' ) /* Plaintext passwd, yuck! */ + return !strcmp(entry, passwd); + else if ( !strncmp(passwd, "$4$", 3) ) + return passwd_compare_sha1(passwd, entry); + else if ( !strncmp(passwd, "$1$", 3) ) + return passwd_compare_md5(passwd, entry); + else + return 0; /* Invalid encryption algorithm */ +} + |