1 /* crypt() - one-way password encryption function Author: Kees J. Bot
3 * This routine does not encrypt anything, it uses the pwdauth
4 * program to do the hard work.
15 #define waitpid _waitpid
16 #include <sys/types.h>
24 /* Set-uid root program to read /etc/shadow or encrypt passwords. */
25 static char PWDAUTH
[] = "/usr/lib/pwdauth";
28 static void tell(const char *s0
, ...)
36 (void) write(2, s
, strlen(s
));
37 s
= va_arg(ap
, const char *);
42 char *crypt(const char *key
, const char *salt
)
47 static char pwdata
[LEN
];
53 /* Fill pwdata[] with the key and salt. */
54 while ((*p
++ = *k
++) != 0) if (p
== pwdata
+LEN
-1) goto fail
;
55 while ((*p
++ = *s
++) != 0) if (p
== pwdata
+LEN
-0) goto fail
;
57 if (pipe(pfd
) < 0) goto fail
;
59 /* Prefill the pipe. */
60 (void) write(pfd
[1], pwdata
, p
- pwdata
);
62 switch ((pid
= fork())) {
68 /* Connect both input and output to the pipe. */
78 execl(PWDAUTH
, PWDAUTH
, (char *) nil
);
80 tell("crypt(): ", PWDAUTH
, ": ", strerror(errno
), "\r\n",
82 /* No pwdauth? Fail! */
83 (void) read(0, pwdata
, LEN
);
89 while (waitpid(pid
, &status
, 0) == -1 && errno
== EINTR
) {}
95 /* Read and return the result. Check if it contains exactly one
98 n
= read(pfd
[0], pwdata
, LEN
);
100 if (n
< 0) goto fail
;
103 while (p
> pwdata
) if (*--p
== 0) n
++;
104 if (n
!= 1) goto fail
;
108 pwdata
[0] = salt
[0] ^ 1; /* make result != salt */
114 * $PchId: crypt.c,v 1.5 1996/04/11 07:46:11 philip Exp $