Debian Codegraph @

modules/crypto/ircservices.c

raw
1 /*
2 * Copyright (c) 2005 Atheme Development Group
3 * Rights to this code are as documented in doc/LICENSE.
4 *
5 * IRCServices's weird password encryption thingy, taken from Anope 1.6.3.
6 *
7 */
8 /* Include file for high-level encryption routines.
9 *
10 * (C) 2003 Anope Team
11 * Contact us at [email protected]
12 *
13 * Please read COPYING and README for furhter details.
14 *
15 * Based on the original code of Epona by Lara.
16 * Based on the original code of Services by Andy Church.
17 *
18 * $\Id: myencrypt.c 5 2004-03-29 01:29:50Z dane $
19 *
20 */
21
22 #include "atheme.h"
23
24 /* necessary anope defines */
25 #define PASSMAX 32
26 #define ENCRYPT_MD5
27
28 /*************************************************************************/
29
30 /******** Our own high-level routines. ********/
31
32
33 #define XTOI(c) ((c)>9 ? (c)-'A'+10 : (c)-'0')
34 /* Result of this:
35 * c in [-128,9] => [-183,-46]
36 * c in [10,127] => [-38,79]
37 */
38
39 /* Encrypt `src' of length `len' and store the result in `dest'. If the
40 * resulting string would be longer than `size', return -1 and leave `dest'
41 * unchanged; else return 0.
42 */
43 static int myencrypt(const char *src, int len, char *dest, int size)
44 {
45
46 #ifdef ENCRYPT_MD5
47
48 md5_state_t context;
49 char digest[33];
50 char dest2[16];
51 int i;
52
53 if (size < 32)
54 return -1;
55
56 memset(&context, 0, sizeof(context));
57 memset(&digest, 0, sizeof(digest));
58
59 md5_init(&context);
60 md5_append(&context, (unsigned const char *) src, (size_t) len);
61 md5_finish(&context, (unsigned char *) digest);
62
63 for (i = 0; i < 32; i += 2)
64 dest2[i / 2] = XTOI(digest[i]) << 4 | XTOI(digest[i + 1]);
65
66 /* convert to hex, skipping last 8 bytes (constant) -- jilles */
67 strcpy(dest, "$ircservices$");
68 for (i = 0; i <= 7; i++)
69 sprintf(dest + 13 + 2 * i, "%02x", 255 & dest2[i]);
70 return 0;
71
72 #else
73
74 return -1; /* unknown encryption algorithm */
75
76 #endif
77
78 }
79
80 #if 0 /* unused */
81 /* Shortcut for encrypting a null-terminated string in place. */
82 static int encrypt_in_place(char *buf, int size)
83 {
84 return myencrypt(buf, strlen(buf), buf, size);
85 }
86 #endif
87
88 /* Compare a plaintext string against an encrypted password. Return 1 if
89 * they match, 0 if not, and -1 if something went wrong. */
90
91 static int check_password(const char *plaintext, const char *password)
92 {
93 char buf[BUFSIZE];
94
95 if (myencrypt(plaintext, strlen(plaintext), buf, sizeof(buf)) < 0)
96 return -1;
97 #ifdef ENCRYPT_MD5
98 if (strcmp(buf, password) == 0)
99 #else
100 if (0)
101 #endif
102 return 1;
103 else
104 return 0;
105 }
106
107 /*************************************************************************/
108
109 DECLARE_MODULE_V1
110 (
111 "crypto/ircservices", false, _modinit, _moddeinit,
112 PACKAGE_STRING,
113 "Jilles Tjoelker <[email protected]>"
114 );
115
116 static const char *ircservices_crypt_string(const char *key, const char *salt)
117 {
118 static char output[PASSMAX];
119 if (salt[0] == '$' && salt[1] == '1') /* this is a new pw XXX */
120 {
121 myencrypt(key, strlen(key), output, PASSMAX);
122 return output;
123 }
124 else
125 {
126 if (check_password(key, salt))
127 return salt;
128 else
129 {
130 output[0] = '\0';
131 return output;
132 }
133 }
134 }
135
136 static crypt_impl_t ircservices_crypt_impl = {
137 .id = "ircservices",
138 .crypt = &ircservices_crypt_string,
139 };
140
141 void _modinit(module_t *m)
142 {
143 crypt_register(&ircservices_crypt_impl);
144 }
145
146 void _moddeinit(module_unload_intent_t intent)
147 {
148 crypt_unregister(&ircservices_crypt_impl);
149 }
150
151 /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
152 * vim:ts=8
153 * vim:sw=8
154 * vim:noexpandtab
155 */