ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/ermyth/modules/crypto/ircservices.C
Revision: 1.5
Committed: Tue Aug 28 17:08:09 2007 UTC (19 years, 2 months ago) by pippijn
Content type: text/plain
Branch: MAIN
Changes since 1.4: +99 -113 lines
Log Message:
- changed name
- updated the example config to the new system
- added more documentation
- enhanced documentation generators
- added a link to the pdf to the website
- added an RSS feed generator
- transitioned hooks to c++ callbacks
- did various merges with upstream along the way
- added const where appropriate
- removed the old block allocator
- fixed most memory leaks
- transitioned some dictionaries to std::map
- transitioned some lists to std::vector
- made some free functions members where appropriate
- renamed string to dynstr and added a static string ststr
- use NOW instead of time (NULL) if possible
- completely reworked database backends, crypto handlers and protocol handlers
  to use an object factory
- removed the old module system. ermyth does not do any dynamic loading anymore
- fixed most of the build system
- reworked how protocol commands work

File Contents

# User Rev Content
1 pippijn 1.1 /*
2 pippijn 1.5 * Copyright © 2005 Atheme Development Group
3 pippijn 1.2 * Rights to this code are as documented in doc/pod/license.pod.
4 pippijn 1.1 *
5     * IRCServices's weird password encryption thingy, taken from Anope 1.6.3.
6     *
7 pippijn 1.5 * $Id: ircservices.C,v 1.4 2007-07-25 01:33:46 pippijn Exp $
8 pippijn 1.1 */
9     /* Include file for high-level encryption routines.
10     *
11     * (C) 2003 Anope Team
12     * Contact us at info@anope.org
13     *
14 pippijn 1.4 * Please read doc/poddoc/license.pod and README for further details.
15 pippijn 1.1 *
16     * Based on the original code of Epona by Lara.
17     * Based on the original code of Services by Andy Church.
18     */
19    
20     #include "atheme.h"
21 pippijn 1.5 #include <ermyth/crypto.h>
22    
23     static char const rcsid[] = "$Id: ircservices.C,v 1.4 2007-07-25 01:33:46 pippijn Exp $";
24 pippijn 1.1
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 pippijn 1.5 namespace crypto
41 pippijn 1.1 {
42 pippijn 1.5 namespace impl
43     {
44     /**
45     * Encrypt `src' of length `len' and store the result in `dest'. If the
46     * resulting string would be longer than `size', return -1 and leave `dest'
47     * unchanged; else return 0.
48     */
49     static int
50     encrypt (char const * const src, int len, char *dest, int size)
51     {
52 pippijn 1.1 #if ENCRYPT_MD5
53 pippijn 1.5 MD5Context context;
54     char digest[33];
55     char dest2[16];
56     int i;
57    
58     if (size < 32)
59     return -1;
60    
61     memset (&context, 0, sizeof (context));
62     memset (&digest, 0, sizeof (digest));
63    
64     MD5Init (&context);
65     MD5Update (&context, (unsigned char const *) src, (size_t) len);
66     MD5Final ((unsigned char *) digest, &context);
67    
68     for (i = 0; i < 32; i += 2)
69     dest2[i / 2] = XTOI (digest[i]) << 4 | XTOI (digest[i + 1]);
70    
71     /* convert to hex, skipping last 8 bytes (constant) -- jilles */
72     strcpy (dest, "$ircservices$");
73     for (i = 0; i <= 7; i++)
74     sprintf (dest + 13 + 2 * i, "%02x", 255 & dest2[i]);
75     return 0;
76    
77 pippijn 1.1 #endif
78 pippijn 1.5
79     return -1; /* unknown encryption algorithm */
80     }
81    
82 pippijn 1.1 #if 0 /* unused */
83 pippijn 1.5 /* Shortcut for encrypting a null-terminated string in place. */
84     static int
85     encrypt_in_place (char *buf, int size)
86     {
87     return encrypt (buf, strlen (buf), buf, size);
88     }
89 pippijn 1.1 #endif
90 pippijn 1.5
91     /**
92     * Compare a plaintext string against an encrypted password. Return 1 if
93     * they match, 0 if not, and -1 if something went wrong.
94     */
95     static int
96     check_password (char const * const plaintext, char const * const password)
97     {
98     char buf[BUFSIZE];
99    
100     if (encrypt (plaintext, strlen (plaintext), buf, sizeof (buf)) < 0)
101     return -1;
102 pippijn 1.1 #ifdef ENCRYPT_MD5
103 pippijn 1.5 if (strcmp (buf, password) == 0)
104 pippijn 1.1 #else
105 pippijn 1.5 if (0)
106 pippijn 1.1 #endif
107 pippijn 1.5 return 1;
108     else
109     return 0;
110 pippijn 1.1 }
111 pippijn 1.5 } // namespace impl
112    
113     /*************************************************************************/
114    
115     struct ircservices : handler
116     {
117     virtual char const *crypt (char const * const key, char const * const salt);
118     };
119    
120     char const *
121     ircservices::crypt (char const * const key, char const * const salt)
122     {
123     static char output[PASSMAX];
124     if (salt[0] == '$' && salt[1] == '1') /* this is a new pw XXX */
125     {
126     impl::encrypt (key, strlen (key), output, PASSMAX);
127     return output;
128     }
129     else
130     {
131     if (impl::check_password (key, salt))
132     return salt;
133     else
134     return "";
135     }
136     }
137     } // namespace crypto
138    
139     #define FACREG_TYPE crypto::ircservices
140     #define FACREG_TYPE_NAME "ircservices"
141     #define FACREG_INTERFACE_TYPE crypto::handler
142     #include <ermyth/factory_reg.h>