1 /*
2 ** Copyright 2001 Double Precision, Inc.
3 ** See COPYING for distribution information.
4 */
5 
6 #if	HAVE_CONFIG_H
7 #include	"config.h"
8 #endif
9 #include	<sys/types.h>
10 #if	HAVE_SYS_STAT_H
11 #include	<sys/stat.h>
12 #endif
13 #if	HAVE_FCNTL_H
14 #include	<fcntl.h>
15 #endif
16 #if	HAVE_UNISTD_H
17 #include	<unistd.h>
18 #endif
19 #if TIME_WITH_SYS_TIME
20 #include <sys/time.h>
21 #include <time.h>
22 #else
23 #if HAVE_SYS_TIME_H
24 #include <sys/time.h>
25 #else
26 #include <time.h>
27 #endif
28 #endif
29 #if	HAVE_MD5
30 #include	"md5/md5.h"
31 #endif
32 
33 #include	<string.h>
34 #include	<stdio.h>
35 #include	<signal.h>
36 #include	<stdlib.h>
37 #if	HAVE_TERMIOS_H
38 #include	<termios.h>
39 #endif
40 #if	HAVE_CRYPT_H
41 #include	<crypt.h>
42 #endif
43 
44 #if HAVE_CRYPT
45 #if NEED_CRYPT_PROTOTYPE
46 extern char *crypt(const char *, const char *);
47 #endif
48 #endif
49 
50 char userdb_hex64[]="./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
51 
52 #ifdef	RANDOM
userdb_get_random(char * buf,unsigned n)53 void userdb_get_random(char *buf, unsigned n)
54 {
55 int	f=open(RANDOM, O_RDONLY);
56 int	l;
57 
58 	if (f < 0)
59 	{
60 		perror(RANDOM);
61 		exit(1);
62 	}
63 	while (n)
64 	{
65 		l=read(f, buf, n);
66 		if (l < 0)
67 		{
68 			perror("read");
69 			exit(1);
70 		}
71 		n -= l;
72 		buf += l;
73 	}
74 	close(f);
75 }
76 #endif
77 
78 #if	HAVE_MD5
userdb_mkmd5pw(const char * buf)79 char *userdb_mkmd5pw(const char *buf)
80 {
81 	int	i;
82 	char salt[9];
83 
84 	salt[8]=0;
85 #ifdef	RANDOM
86 	userdb_get_random(salt, 8);
87 	for (i=0; i<8; i++)
88 		salt[i] = userdb_hex64[salt[i] & 63 ];
89 
90 #else
91 	{
92 
93 		struct {
94 #if HAVE_GETTIMEOFDAY
95 			struct timeval tv;
96 #else
97 			time_t	tv;
98 #endif
99 			pid_t	p;
100 		} s;
101 
102 		MD5_DIGEST	d;
103 #if HAVE_GETTIMEOFDAY
104 		struct timezone tz;
105 
106 		gettimeofday(&s.tv, &tz);
107 #else
108 		time(&s.tv);
109 #endif
110 		s.p=getpid();
111 
112 		md5_digest(&s, sizeof(s), d);
113 		for (i=0; i<8; i++)
114 			salt[i]=userdb_hex64[ ((unsigned char *)d)[i] ];
115 	}
116 #endif
117 	return (md5_crypt(buf, salt));
118 }
119 #endif
120