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 | #include <string.h> |
---|
32 | #include <stdio.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <glib.h> |
---|
35 | #include "md5.h" |
---|
36 | #include "crypting.h" |
---|
37 | |
---|
38 | /*\ |
---|
39 | * [SH] Do _not_ call this if it's not entirely sure that it will not cause |
---|
40 | * harm to another users file, since this does not check the password for |
---|
41 | * correctness. |
---|
42 | \*/ |
---|
43 | |
---|
44 | int checkpass (const char *pass, const char *md5sum) |
---|
45 | { |
---|
46 | md5_state_t md5state; |
---|
47 | md5_byte_t digest[16]; |
---|
48 | int i, j; |
---|
49 | char digits[3]; |
---|
50 | |
---|
51 | md5_init (&md5state); |
---|
52 | md5_append (&md5state, (unsigned char *)pass, strlen (pass)); |
---|
53 | md5_finish (&md5state, digest); |
---|
54 | |
---|
55 | for (i = 0, j = 0; i < 16; i++, j += 2) { |
---|
56 | /* Check password for correctness */ |
---|
57 | g_snprintf (digits, sizeof (digits), "%02x\n", digest[i]); |
---|
58 | |
---|
59 | if (digits[0] != md5sum[j]) return (-1); |
---|
60 | if (digits[1] != md5sum[j + 1]) return (-1); |
---|
61 | } |
---|
62 | |
---|
63 | return( 0 ); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | char *hashpass (const char *password) |
---|
68 | { |
---|
69 | md5_state_t md5state; |
---|
70 | md5_byte_t digest[16]; |
---|
71 | int i; |
---|
72 | char digits[3]; |
---|
73 | char *rv; |
---|
74 | |
---|
75 | if (password == NULL) return (NULL); |
---|
76 | |
---|
77 | rv = g_new0 (char, 33); |
---|
78 | |
---|
79 | md5_init (&md5state); |
---|
80 | md5_append (&md5state, (const unsigned char *)password, strlen (password)); |
---|
81 | md5_finish (&md5state, digest); |
---|
82 | |
---|
83 | for (i = 0; i < 16; i++) { |
---|
84 | /* Build a hash of the pass */ |
---|
85 | g_snprintf (digits, sizeof (digits), "%02x", digest[i]); |
---|
86 | strcat (rv, digits); |
---|
87 | } |
---|
88 | |
---|
89 | return (rv); |
---|
90 | } |
---|
91 | |
---|
92 | char *obfucrypt (char *line, const char *password) |
---|
93 | { |
---|
94 | int i, j; |
---|
95 | char *rv; |
---|
96 | |
---|
97 | if (password == NULL) return (NULL); |
---|
98 | |
---|
99 | rv = g_new0 (char, strlen (line) + 1); |
---|
100 | |
---|
101 | i = j = 0; |
---|
102 | while (*line) { |
---|
103 | /* Encrypt/obfuscate the line, using the password */ |
---|
104 | if (*(signed char*)line < 0) *line = - (*line); |
---|
105 | |
---|
106 | rv[j] = *line + password[i]; /* Overflow intended */ |
---|
107 | |
---|
108 | line++; |
---|
109 | if (!password[++i]) i = 0; |
---|
110 | j++; |
---|
111 | } |
---|
112 | |
---|
113 | return (rv); |
---|
114 | } |
---|
115 | |
---|
116 | char *deobfucrypt (char *line, const char *password) |
---|
117 | { |
---|
118 | int i, j; |
---|
119 | char *rv; |
---|
120 | |
---|
121 | if (password == NULL) return (NULL); |
---|
122 | |
---|
123 | rv = g_new0 (char, strlen (line) + 1); |
---|
124 | |
---|
125 | i = j = 0; |
---|
126 | while (*line) { |
---|
127 | /* Decrypt/deobfuscate the line, using the pass */ |
---|
128 | rv[j] = *line - password[i]; /* Overflow intended */ |
---|
129 | |
---|
130 | line++; |
---|
131 | if (!password[++i]) i = 0; |
---|
132 | j++; |
---|
133 | } |
---|
134 | |
---|
135 | return (rv); |
---|
136 | } |
---|
137 | |
---|
138 | #ifdef CRYPTING_MAIN |
---|
139 | |
---|
140 | /* A little main() function for people who want a stand-alone program to |
---|
141 | encode/decode BitlCrypted files. */ |
---|
142 | |
---|
143 | int main( int argc, char *argv[] ) |
---|
144 | { |
---|
145 | char *hash, *action, line[256]; |
---|
146 | char* (*func)( char *, const char * ); |
---|
147 | |
---|
148 | if( argc < 2 ) |
---|
149 | { |
---|
150 | fprintf( stderr, "Usage: %s <password>\n\n" |
---|
151 | "Reads from stdin, writes to stdout.\n" |
---|
152 | "Call as \"encode\" to encode, \"decode\" to decode.\n", argv[0] ); |
---|
153 | return( 1 ); |
---|
154 | } |
---|
155 | |
---|
156 | hash = hashpass( argv[1] ); |
---|
157 | action = argv[0] + strlen( argv[0] ) - strlen( "encode" ); |
---|
158 | |
---|
159 | if( strcmp( action, "encode" ) == 0 ) |
---|
160 | { |
---|
161 | fwrite( hash, 32, 1, stdout ); |
---|
162 | func = obfucrypt; |
---|
163 | } |
---|
164 | else if( strcmp( action, "decode" ) == 0 ) |
---|
165 | { |
---|
166 | char hash2[32]; |
---|
167 | |
---|
168 | fread( hash2, 32, 1, stdin ); |
---|
169 | if( memcmp( hash, hash2, 32 ) != 0 ) |
---|
170 | { |
---|
171 | fprintf( stderr, "Passwords don't match. Can't decode.\n" ); |
---|
172 | return( 1 ); |
---|
173 | } |
---|
174 | func = deobfucrypt; |
---|
175 | } |
---|
176 | else |
---|
177 | { |
---|
178 | return( main( 0, NULL ) ); |
---|
179 | } |
---|
180 | |
---|
181 | while( fscanf( stdin, "%[^\n]255s", line ) > 0 ) |
---|
182 | { |
---|
183 | char *out; |
---|
184 | |
---|
185 | /* Flush the newline */ |
---|
186 | fgetc( stdin ); |
---|
187 | |
---|
188 | out = func( line, argv[1] ); |
---|
189 | printf( "%s\n", out ); |
---|
190 | g_free( out ); |
---|
191 | } |
---|
192 | |
---|
193 | return( 0 ); |
---|
194 | } |
---|
195 | |
---|
196 | #endif |
---|