Uninitialized vector entry?
[minix3.git] / lib / other / crypt.c
bloba82b3b0c2f27d706fbb01c269e68c0dbfcf2a30d
1 /* crypt() - one-way password encryption function Author: Kees J. Bot
2 * 7 Feb 1994
3 * This routine does not encrypt anything, it uses the pwdauth
4 * program to do the hard work.
5 */
6 #define nil ((void*)0)
7 #define pipe _pipe
8 #define fork _fork
9 #define close _close
10 #define dup2 _dup2
11 #define execl _execl
12 #define read _read
13 #define _exit __exit
14 #define write _write
15 #define waitpid _waitpid
16 #include <sys/types.h>
17 #include <unistd.h>
18 #include <string.h>
19 #include <stdio.h>
20 #include <errno.h>
21 #include <stdarg.h>
22 #include <sys/wait.h>
24 /* Set-uid root program to read /etc/shadow or encrypt passwords. */
25 static char PWDAUTH[] = "/usr/lib/pwdauth";
26 #define LEN 1024
28 static void tell(const char *s0, ...)
30 va_list ap;
31 const char *s;
33 va_start(ap, s0);
34 s= s0;
35 while (s != nil) {
36 (void) write(2, s, strlen(s));
37 s= va_arg(ap, const char *);
39 va_end(ap);
42 char *crypt(const char *key, const char *salt)
44 pid_t pid;
45 int status;
46 int pfd[2];
47 static char pwdata[LEN];
48 char *p= pwdata;
49 const char *k= key;
50 const char *s= salt;
51 int n;
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())) {
63 case -1:
64 close(pfd[0]);
65 close(pfd[1]);
66 goto fail;
67 case 0:
68 /* Connect both input and output to the pipe. */
69 if (pfd[0] != 0) {
70 dup2(pfd[0], 0);
71 close(pfd[0]);
73 if (pfd[1] != 1) {
74 dup2(pfd[1], 1);
75 close(pfd[1]);
78 execl(PWDAUTH, PWDAUTH, (char *) nil);
80 tell("crypt(): ", PWDAUTH, ": ", strerror(errno), "\r\n",
81 (char *) nil);
82 /* No pwdauth? Fail! */
83 (void) read(0, pwdata, LEN);
84 _exit(1);
86 close(pfd[1]);
88 status= -1;
89 while (waitpid(pid, &status, 0) == -1 && errno == EINTR) {}
90 if (status != 0) {
91 close(pfd[0]);
92 goto fail;
95 /* Read and return the result. Check if it contains exactly one
96 * string.
98 n= read(pfd[0], pwdata, LEN);
99 close(pfd[0]);
100 if (n < 0) goto fail;
101 p = pwdata + n;
102 n = 0;
103 while (p > pwdata) if (*--p == 0) n++;
104 if (n != 1) goto fail;
105 return pwdata;
107 fail:
108 pwdata[0] = salt[0] ^ 1; /* make result != salt */
109 pwdata[1] = 0;
110 return pwdata;
114 * $PchId: crypt.c,v 1.5 1996/04/11 07:46:11 philip Exp $