1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2004 Sjoerd Hemminga and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* A little bit of encryption for the users' passwords */ |
---|
8 | |
---|
9 | /* |
---|
10 | This program is free software; you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | /* [WvG] This file can also be compiled into a stand-alone program |
---|
27 | which can encode/decode BitlBee account files. The main() will be |
---|
28 | included if CRYPTING_MAIN is defined. Or just do "make decode" and |
---|
29 | the programs will be built. */ |
---|
30 | |
---|
31 | #ifndef CRYPTING_MAIN |
---|
32 | #define BITLBEE_CORE |
---|
33 | #include "bitlbee.h" |
---|
34 | #include "irc.h" |
---|
35 | #include "md5.h" |
---|
36 | #include "crypting.h" |
---|
37 | #include <string.h> |
---|
38 | #include <stdio.h> |
---|
39 | #include <stdlib.h> |
---|
40 | |
---|
41 | #else |
---|
42 | |
---|
43 | typedef struct irc |
---|
44 | { |
---|
45 | char *password; |
---|
46 | } irc_t; |
---|
47 | |
---|
48 | #include "md5.h" |
---|
49 | #include "crypting.h" |
---|
50 | #include <string.h> |
---|
51 | #include <stdio.h> |
---|
52 | #include <stdlib.h> |
---|
53 | |
---|
54 | #endif |
---|
55 | |
---|
56 | /*\ |
---|
57 | * [SH] Do _not_ call this if it's not entirely sure that it will not cause |
---|
58 | * harm to another users file, since this does not check the password for |
---|
59 | * correctness. |
---|
60 | \*/ |
---|
61 | |
---|
62 | int checkpass (const char *pass, const char *md5sum) |
---|
63 | { |
---|
64 | md5_state_t md5state; |
---|
65 | md5_byte_t digest[16]; |
---|
66 | int i, j; |
---|
67 | char digits[3]; |
---|
68 | |
---|
69 | md5_init (&md5state); |
---|
70 | md5_append (&md5state, (unsigned char *)pass, strlen (pass)); |
---|
71 | md5_finish (&md5state, digest); |
---|
72 | |
---|
73 | for (i = 0, j = 0; i < 16; i++, j += 2) { |
---|
74 | /* Check password for correctness */ |
---|
75 | g_snprintf (digits, sizeof (digits), "%02x\n", digest[i]); |
---|
76 | |
---|
77 | if (digits[0] != md5sum[j]) return (-1); |
---|
78 | if (digits[1] != md5sum[j + 1]) return (-1); |
---|
79 | } |
---|
80 | |
---|
81 | return( 0 ); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | char *hashpass (irc_t *irc) { |
---|
86 | md5_state_t md5state; |
---|
87 | md5_byte_t digest[16]; |
---|
88 | int i; |
---|
89 | char digits[3]; |
---|
90 | char *rv; |
---|
91 | |
---|
92 | if (irc->password == NULL) return (NULL); |
---|
93 | |
---|
94 | rv = (char *)g_malloc (33); |
---|
95 | memset (rv, 0, 33); |
---|
96 | |
---|
97 | md5_init (&md5state); |
---|
98 | md5_append (&md5state, (unsigned char *)irc->password, strlen (irc->password)); |
---|
99 | md5_finish (&md5state, digest); |
---|
100 | |
---|
101 | for (i = 0; i < 16; i++) { |
---|
102 | /* Build a hash of the pass */ |
---|
103 | g_snprintf (digits, sizeof (digits), "%02x", digest[i]); |
---|
104 | strcat (rv, digits); |
---|
105 | } |
---|
106 | |
---|
107 | return (rv); |
---|
108 | } |
---|
109 | |
---|
110 | char *obfucrypt (irc_t *irc, char *line) { |
---|
111 | int i, j; |
---|
112 | char *rv; |
---|
113 | |
---|
114 | if (irc->password == NULL) return (NULL); |
---|
115 | |
---|
116 | rv = (char *)g_malloc (strlen (line) + 1); |
---|
117 | memset (rv, '\0', strlen (line) + 1); |
---|
118 | |
---|
119 | i = j = 0; |
---|
120 | while (*line) { |
---|
121 | /* Encrypt/obfuscate the line, using the password */ |
---|
122 | if (*(signed char*)line < 0) *line = - (*line); |
---|
123 | if (((signed char*)irc->password)[i] < 0) irc->password[i] = - irc->password[i]; |
---|
124 | |
---|
125 | rv[j] = *line + irc->password[i]; /* Overflow intended */ |
---|
126 | |
---|
127 | line++; |
---|
128 | if (!irc->password[++i]) i = 0; |
---|
129 | j++; |
---|
130 | } |
---|
131 | |
---|
132 | return (rv); |
---|
133 | } |
---|
134 | |
---|
135 | char *deobfucrypt (irc_t *irc, char *line) { |
---|
136 | int i, j; |
---|
137 | char *rv; |
---|
138 | |
---|
139 | if (irc->password == NULL) return (NULL); |
---|
140 | |
---|
141 | rv = (char *)g_malloc (strlen (line) + 1); |
---|
142 | memset (rv, '\0', strlen (line) + 1); |
---|
143 | |
---|
144 | i = j = 0; |
---|
145 | while (*line) { |
---|
146 | /* Decrypt/deobfuscate the line, using the pass */ |
---|
147 | rv[j] = *line - irc->password[i]; /* Overflow intended */ |
---|
148 | |
---|
149 | line++; |
---|
150 | if (!irc->password[++i]) i = 0; |
---|
151 | j++; |
---|
152 | } |
---|
153 | |
---|
154 | return (rv); |
---|
155 | } |
---|
156 | |
---|
157 | #ifdef CRYPTING_MAIN |
---|
158 | |
---|
159 | /* A little main() function for people who want a stand-alone program to |
---|
160 | encode/decode BitlCrypted files. */ |
---|
161 | |
---|
162 | int main( int argc, char *argv[] ) |
---|
163 | { |
---|
164 | irc_t *irc = g_malloc( sizeof( irc_t ) ); |
---|
165 | char *hash, *action, line[256]; |
---|
166 | char* (*func)( irc_t *, char * ); |
---|
167 | |
---|
168 | if( argc < 2 ) |
---|
169 | { |
---|
170 | fprintf( stderr, "Usage: %s <password>\n\n" |
---|
171 | "Reads from stdin, writes to stdout.\n" |
---|
172 | "Call as \"encode\" to encode, \"decode\" to decode.\n", argv[0] ); |
---|
173 | return( 1 ); |
---|
174 | } |
---|
175 | |
---|
176 | memset( irc, 0, sizeof( irc_t ) ); |
---|
177 | irc->password = g_strdup( argv[1] ); |
---|
178 | |
---|
179 | hash = hashpass( irc ); |
---|
180 | action = argv[0] + strlen( argv[0] ) - strlen( "encode" ); |
---|
181 | |
---|
182 | if( strcmp( action, "encode" ) == 0 ) |
---|
183 | { |
---|
184 | fwrite( hash, 32, 1, stdout ); |
---|
185 | func = obfucrypt; |
---|
186 | } |
---|
187 | else if( strcmp( action, "decode" ) == 0 ) |
---|
188 | { |
---|
189 | char hash2[32]; |
---|
190 | |
---|
191 | fread( hash2, 32, 1, stdin ); |
---|
192 | if( memcmp( hash, hash2, 32 ) != 0 ) |
---|
193 | { |
---|
194 | fprintf( stderr, "Passwords don't match. Can't decode.\n" ); |
---|
195 | return( 1 ); |
---|
196 | } |
---|
197 | func = deobfucrypt; |
---|
198 | } |
---|
199 | else |
---|
200 | { |
---|
201 | return( main( 0, NULL ) ); |
---|
202 | } |
---|
203 | |
---|
204 | while( fscanf( stdin, "%[^\n]255s", line ) > 0 ) |
---|
205 | { |
---|
206 | char *out; |
---|
207 | |
---|
208 | /* Flush the newline */ |
---|
209 | fgetc( stdin ); |
---|
210 | |
---|
211 | out = func( irc, line ); |
---|
212 | printf( "%s\n", out ); |
---|
213 | g_free( out ); |
---|
214 | } |
---|
215 | |
---|
216 | return( 0 ); |
---|
217 | } |
---|
218 | |
---|
219 | #endif |
---|