From 88771b9a2f6013d87fdf49ad5101c46491b33db7 Mon Sep 17 00:00:00 2001
From: John Denker <jsd@av8n.com>
Date: Sat, 2 Jun 2012 18:56:03 -0700
Subject: make checkpassword easier to test ... and slightly more efficient to
 use

---
 checkpasswd/checkpassword.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

(limited to 'checkpasswd')

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);
-- 
cgit v1.2.3