summaryrefslogtreecommitdiffstats
path: root/userdb/userdbmkpw.c
blob: 3786a83710d665140d403ad34802c1a760b9d3fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
** Copyright 2001 Double Precision, Inc.
** See COPYING for distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	<sys/types.h>
#if	HAVE_SYS_STAT_H
#include	<sys/stat.h>
#endif
#if	HAVE_FCNTL_H
#include	<fcntl.h>
#endif
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#if TIME_WITH_SYS_TIME
#include <sys/time.h>
#include <time.h>
#else
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#else
#include <time.h>
#endif
#endif
#if	HAVE_MD5
#include	"md5/md5.h"
#endif

#include	<string.h>
#include	<stdio.h>
#include	<signal.h>
#include	<stdlib.h>
#if	HAVE_TERMIOS_H
#include	<termios.h>
#endif
#if	HAVE_CRYPT_H
#include	<crypt.h>
#endif

#if HAVE_CRYPT
#if NEED_CRYPT_PROTOTYPE
extern char *crypt(const char *, const char *);
#endif
#endif

char userdb_hex64[]="./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

#ifdef	RANDOM
void userdb_get_random(char *buf, unsigned n)
{
int	f=open(RANDOM, O_RDONLY);
int	l;

	if (f < 0)
	{
		perror(RANDOM);
		exit(1);
	}
	while (n)
	{
		l=read(f, buf, n);
		if (l < 0)
		{
			perror("read");
			exit(1);
		}
		n -= l;
		buf += l;
	}
	close(f);
}
#endif

#if	HAVE_MD5
char *userdb_mkmd5pw(const char *buf)
{
	int	i;
	char salt[9];

	salt[8]=0;
#ifdef	RANDOM
	userdb_get_random(salt, 8);
	for (i=0; i<8; i++)
		salt[i] = userdb_hex64[salt[i] & 63 ];

#else
	{

		struct {
#if HAVE_GETTIMEOFDAY
			struct timeval tv;
#else
			time_t	tv;
#endif
			pid_t	p;
		} s;

		MD5_DIGEST	d;
#if HAVE_GETTIMEOFDAY
		struct timezone tz;

		gettimeofday(&s.tv, &tz);
#else
		time(&s.tv);
#endif
		s.p=getpid();

		md5_digest(&s, sizeof(s), d);
		for (i=0; i<8; i++)
			salt[i]=userdb_hex64[ ((unsigned char *)d)[i] ];
	}
#endif
	return (md5_crypt(buf, salt));
}
#endif