source: trunk/minix/lib/other/crypt.c@ 15

Last change on this file since 15 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

File size: 2.3 KB
Line 
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>
23
24/* Set-uid root program to read /etc/shadow or encrypt passwords. */
25static char PWDAUTH[] = "/usr/lib/pwdauth";
26#define LEN 1024
27
28static void tell(const char *s0, ...)
29{
30 va_list ap;
31 const char *s;
32
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 *);
38 }
39 va_end(ap);
40}
41
42char *crypt(const char *key, const char *salt)
43{
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;
52
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;
56
57 if (pipe(pfd) < 0) goto fail;
58
59 /* Prefill the pipe. */
60 (void) write(pfd[1], pwdata, p - pwdata);
61
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]);
72 }
73 if (pfd[1] != 1) {
74 dup2(pfd[1], 1);
75 close(pfd[1]);
76 }
77
78 execl(PWDAUTH, PWDAUTH, (char *) nil);
79
80 tell("crypt(): ", PWDAUTH, ": ", strerror(errno), "\r\n",
81 (char *) nil);
82 /* No pwdauth? Fail! */
83 (void) read(0, pwdata, LEN);
84 _exit(1);
85 }
86 close(pfd[1]);
87
88 status= -1;
89 while (waitpid(pid, &status, 0) == -1 && errno == EINTR) {}
90 if (status != 0) {
91 close(pfd[0]);
92 goto fail;
93 }
94
95 /* Read and return the result. Check if it contains exactly one
96 * string.
97 */
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;
106
107fail:
108 pwdata[0] = salt[0] ^ 1; /* make result != salt */
109 pwdata[1] = 0;
110 return pwdata;
111}
112
113/*
114 * $PchId: crypt.c,v 1.5 1996/04/11 07:46:11 philip Exp $
115 */
Note: See TracBrowser for help on using the repository browser.