summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-06-02 18:56:03 -0700
committerJohn Denker <jsd@av8n.com>2012-06-02 18:56:03 -0700
commit88771b9a2f6013d87fdf49ad5101c46491b33db7 (patch)
tree4f6cf7b8f0fce14211ffbb940010917ccfc43c26
parente2463d90bca8ca4227a604ab0f80037fe1166448 (diff)
make checkpassword easier to test ... and slightly more efficient to use
-rw-r--r--checkpasswd/checkpassword.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/checkpasswd/checkpassword.c b/checkpasswd/checkpassword.c
index afca798..7e5ee18 100644
--- a/checkpasswd/checkpassword.c
+++ b/checkpasswd/checkpassword.c
@@ -4,6 +4,7 @@
extern char *crypt();
#include <pwd.h>
+#include <stdlib.h> /* for getenv */
static struct passwd *pw;
#include "hasspnam.h"
@@ -30,19 +31,23 @@ main(int argc,char **argv)
int r;
int i;
+ int unit = 3;
+ char* unit_str = getenv("CHECKPASSWORD_UNIT");
+ if (unit_str) unit = atoi(unit_str);
+
if (!argv[1]) _exit(2);
uplen = 0;
for (;;) {
do
- r = read(3,up + uplen,sizeof(up) - uplen);
+ r = read(unit, up + uplen,sizeof(up) - uplen);
while ((r == -1) && (errno == error_intr));
if (r == -1) _exit(111);
if (r == 0) break;
uplen += r;
if (uplen >= sizeof(up)) _exit(1);
}
- close(3);
+ close(unit);
i = 0;
if (i >= uplen) _exit(2);
@@ -76,10 +81,13 @@ main(int argc,char **argv)
if (!stored) _exit(1);
encrypted = crypt(password,stored);
- for (i = 0;i < sizeof(up);++i) up[i] = 0;
+ for (i = 0;i < sizeof(up);++i) up[i] = 0; // don't leave it lying around
if (!*stored || strcmp(encrypted,stored)) _exit(1);
+// OK, the password checks out:
+ if (argv[1][0] == '-' && argv[1][1] == 0) _exit(0);
+
if (prot_gid((int) pw->pw_gid) == -1) _exit(1);
if (prot_uid((int) pw->pw_uid) == -1) _exit(1);
if (chdir(pw->pw_dir) == -1) _exit(111);