File: cryptpassword.c

package info (click to toggle)
courier 0.47-4sarge5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 32,680 kB
  • ctags: 12,265
  • sloc: ansic: 164,045; cpp: 23,863; sh: 19,569; perl: 7,225; makefile: 4,192; yacc: 316; sed: 16
file content (94 lines) | stat: -rw-r--r-- 1,740 bytes parent folder | download
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
/*
** Copyright 2001-2002 Double Precision, Inc.  See COPYING for
** distribution information.
*/

#if	HAVE_CONFIG_H
#include	"config.h"
#endif
#include	<string.h>
#if	HAVE_UNISTD_H
#include	<unistd.h>
#endif
#include	<stdlib.h>
#if	HAVE_CRYPT_H
#include	<crypt.h>
#endif
#include	"auth.h"
#include	<sys/time.h>

static const char rcsid[]="$Id: cryptpassword.c,v 1.4 2002/12/12 04:23:58 mrsam Exp $";

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

#if	HAVE_MD5LIB
#include        "md5/md5.h"
#endif

#if	HAVE_SHA1LIB
#include	"sha1/sha1.h"
#endif

static const char crypt_salt[65]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./";

static const char *crypt_hash(const char *pw)
{
	struct timeval tv;
	char salt[3];

	gettimeofday(&tv, NULL);

	tv.tv_sec |= tv.tv_usec;
	tv.tv_sec ^= getpid();

	salt[0]=crypt_salt[ tv.tv_sec % 64 ];

	tv.tv_sec /= 64;

	salt[1]=crypt_salt[ tv.tv_sec % 64 ];
	salt[2]=0;

	return (crypt(pw, salt));
}

char *authcryptpasswd(const char *password, const char *encryption_hint)
{
	const char *(*hash_func)(const char *)=0;
	const char *pfix=0;
	const char *p;
	char *pp;

#if	HAVE_MD5LIB

	if (!encryption_hint || strncmp(encryption_hint, "$1$", 3) == 0
	    || strncasecmp(encryption_hint, "{MD5}", 5) == 0)
	{
               hash_func= &md5_hash_courier;
		pfix="{MD5}";
	}
#endif

#if	HAVE_SHA1LIB
	if (!encryption_hint || strncasecmp(encryption_hint, "{SHA}", 5) == 0)
	{
		hash_func= &sha1_hash;
		pfix="{SHA}";
	}
#endif

	if (!hash_func)
	{
		hash_func= &crypt_hash;
		pfix="{CRYPT}";
	}

	p= (*hash_func)(password);
	if (!p || (pp=malloc(strlen(pfix)+strlen(p)+1)) == 0)
		return (0);

	return (strcat(strcpy(pp, pfix), p));
}