ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/crypto/ircservices.C
Revision: 1.1
Committed: Thu Jul 19 08:24:53 2007 UTC (19 years, 2 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Log Message:
initial import. the most important changes since Atheme are:
- fixed many memory leaks
- fixed many bugs
- converted to C++ and use more STL containers
- added a (not very enhanced yet) perl module
- greatly improved XML-RPC speed
- added a JSON-RPC module with code from json-cpp
- added a valgrind memcheck module to operserv
- added a more object oriented base64 implementation
- added a specialised unit test framework
- improved stability
- use gettimeofday() if available
- reworked adding/removing commands
- MemoServ IGNORE DEL can now remove indices

File Contents

# User Rev Content
1 pippijn 1.1 /*
2     * Copyright © 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     * $Id: ircservices.c 7877 2007-03-06 01:43:05Z pippijn $
8     */
9     /* Include file for high-level encryption routines.
10     *
11     * (C) 2003 Anope Team
12     * Contact us at info@anope.org
13     *
14     * Please read COPYING and README for furhter details.
15     *
16     * Based on the original code of Epona by Lara.
17     * Based on the original code of Services by Andy Church.
18     *
19     * $\Id: myencrypt.c 5 2004-03-29 01:29:50Z dane $
20     *
21     */
22    
23     #include "atheme.h"
24    
25     /* necessary anope defines */
26     #define PASSMAX 32
27     #define ENCRYPT_MD5 1
28    
29     /*************************************************************************/
30    
31     /******** Our own high-level routines. ********/
32    
33    
34     #define XTOI(c) ((c)>9 ? (c)-'A'+10 : (c)-'0')
35     /* Result of this:
36     * c in [-128,9] => [-183,-46]
37     * c in [10,127] => [-38,79]
38     */
39    
40     /* Encrypt `src' of length `len' and store the result in `dest'. If the
41     * resulting string would be longer than `size', return -1 and leave `dest'
42     * unchanged; else return 0.
43     */
44     static int
45     myencrypt (const char *src, int len, char *dest, int size)
46     {
47     #if ENCRYPT_MD5
48     MD5Context 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     MD5Init (&context);
60     MD5Update (&context, (unsigned char const *) src, (size_t) len);
61     MD5Final ((unsigned char *) digest, &context);
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     #endif
73    
74     return -1; /* unknown encryption algorithm */
75     }
76    
77     #if 0 /* unused */
78     /* Shortcut for encrypting a null-terminated string in place. */
79     static int
80     encrypt_in_place (char *buf, int size)
81     {
82     return myencrypt (buf, strlen (buf), buf, size);
83     }
84     #endif
85    
86     /* Compare a plaintext string against an encrypted password. Return 1 if
87     * they match, 0 if not, and -1 if something went wrong. */
88    
89     static int
90     check_password (const char *plaintext, const char *password)
91     {
92     char buf[BUFSIZE];
93    
94     if (myencrypt (plaintext, strlen (plaintext), buf, sizeof (buf)) < 0)
95     return -1;
96     #ifdef ENCRYPT_MD5
97     if (strcmp (buf, password) == 0)
98     #else
99     if (0)
100     #endif
101     return 1;
102     else
103     return 0;
104     }
105    
106     /*************************************************************************/
107    
108     static char const rcsid[] = "$Id";
109    
110     struct crypto_ircservices_listeners : callback::has_listeners
111     {
112     };
113    
114     crypto_ircservices_listeners listeners;
115    
116     DECLARE_MODULE_V1 ("crypto/ircservices", false, _modinit, _moddeinit, "$Id: ircservices.c 7877 2007-03-06 01:43:05Z pippijn $", "Jilles Tjoelker <jilles@stack.nl>");
117    
118     static char *
119     ircservices_crypt_string (char *key, char *salt)
120     {
121     static char output[PASSMAX];
122     if (salt[0] == '$' && salt[1] == '1') /* this is a new pw XXX */
123     {
124     myencrypt (key, strlen (key), output, PASSMAX);
125     return output;
126     }
127     else
128     {
129     if (check_password (key, salt))
130     return salt;
131     else
132     return "";
133     }
134     }
135    
136     void
137     _modinit (module_t *m)
138     {
139     crypt_string = &ircservices_crypt_string;
140    
141     crypto_module_loaded = true;
142     }
143    
144     void
145     _moddeinit (void)
146     {
147     crypt_string = &generic_crypt_string;
148    
149     crypto_module_loaded = false;
150     }
151    
152     /* vim:cinoptions=>s,e0,n0,f0,{0,}0,^0,=s,ps,t0,c3,+s,(2s,us,)20,*30,gs,hs
153     * vim:ts=8
154     * vim:sw=8
155     * vim:noexpandtab
156     */