source: crypting.c @ 1ee6c18

Last change on this file since 1ee6c18 was 1ee6c18, checked in by Jelmer Vernooij <jelmer@…>, at 2005-12-08T13:41:53Z

Add abstraction layer for storage

  • Property mode set to 100644
File size: 5.7 KB
Line 
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
43typedef struct irc
44{
45        char *password;
46} irc_t;
47
48#define set_add( a, b, c, d )
49#define set_find( a, b ) NULL
50
51#include "md5.h"
52#include "crypting.h"
53#include <string.h>
54#include <stdio.h>
55#include <stdlib.h>
56
57#define irc_usermsg
58
59#endif
60
61/*\
62 * [SH] Do _not_ call this if it's not entirely sure that it will not cause
63 * harm to another users file, since this does not check the password for
64 * correctness.
65\*/
66
67/* USE WITH CAUTION!
68   Sets pass without checking */
69void setpassnc (irc_t *irc, const char *pass) 
70{
71        if (!set_find (irc, "password"))
72                set_add (irc, "password", NULL, passchange);
73       
74        if (irc->password) g_free (irc->password);
75       
76        if (pass) {
77                irc->password = g_strdup (pass);
78                irc_usermsg (irc, "Password successfully changed");
79        } else {
80                irc->password = NULL;
81        }
82}
83
84char *passchange (irc_t *irc, void *set, char *value) {
85        setpassnc (irc, value);
86        return (NULL);
87}
88
89int setpass (irc_t *irc, const char *pass, char* md5sum) {
90        md5_state_t md5state;
91        md5_byte_t digest[16];
92        int i, j;
93        char digits[3];
94       
95        md5_init (&md5state);
96        md5_append (&md5state, (unsigned char *)pass, strlen (pass));
97        md5_finish (&md5state, digest);
98       
99        for (i = 0, j = 0; i < 16; i++, j += 2) {
100                /* Check password for correctness */
101                g_snprintf (digits, sizeof (digits), "%02x\n", digest[i]);
102               
103                if (digits[0] != md5sum[j]) return (-1);
104                if (digits[1] != md5sum[j + 1]) return (-1);
105        }
106       
107        /* If pass is correct, we end up here and we set the pass */
108        setpassnc (irc, pass);
109       
110        return (0);
111}
112
113char *hashpass (irc_t *irc) {
114        md5_state_t md5state;
115        md5_byte_t digest[16];
116        int i;
117        char digits[3];
118        char *rv;
119       
120        if (irc->password == NULL) return (NULL);
121       
122        rv = (char *)g_malloc (33);
123        memset (rv, 0, 33);
124       
125        md5_init (&md5state);
126        md5_append (&md5state, (unsigned char *)irc->password, strlen (irc->password));
127        md5_finish (&md5state, digest);
128       
129        for (i = 0; i < 16; i++) {
130                /* Build a hash of the pass */
131                g_snprintf (digits, sizeof (digits), "%02x", digest[i]);
132                strcat (rv, digits);
133        }
134       
135        return (rv);
136}
137
138char *obfucrypt (irc_t *irc, char *line) {
139        int i, j;
140        char *rv;
141       
142        if (irc->password == NULL) return (NULL);
143       
144        rv = (char *)g_malloc (strlen (line) + 1);
145        memset (rv, '\0', strlen (line) + 1);
146       
147        i = j = 0;
148        while (*line) {
149                /* Encrypt/obfuscate the line, using the password */
150                if (*(signed char*)line < 0) *line = - (*line);
151                if (((signed char*)irc->password)[i] < 0) irc->password[i] = - irc->password[i];
152               
153                rv[j] = *line + irc->password[i]; /* Overflow intended */
154               
155                line++;
156                if (!irc->password[++i]) i = 0;
157                j++;
158        }
159       
160        return (rv);
161}
162
163char *deobfucrypt (irc_t *irc, char *line) {
164        int i, j;
165        char *rv;
166       
167        if (irc->password == NULL) return (NULL);
168       
169        rv = (char *)g_malloc (strlen (line) + 1);
170        memset (rv, '\0', strlen (line) + 1);
171       
172        i = j = 0;
173        while (*line) {
174                /* Decrypt/deobfuscate the line, using the pass */
175                rv[j] = *line - irc->password[i]; /* Overflow intended */
176               
177                line++;
178                if (!irc->password[++i]) i = 0;
179                j++;
180        }
181       
182        return (rv);
183}
184
185#ifdef CRYPTING_MAIN
186
187/* A little main() function for people who want a stand-alone program to
188   encode/decode BitlCrypted files. */
189
190int main( int argc, char *argv[] )
191{
192        irc_t *irc = g_malloc( sizeof( irc_t ) );
193        char *hash, *action, line[256];
194        char* (*func)( irc_t *, char * );
195       
196        if( argc < 2 )
197        {
198                fprintf( stderr, "Usage: %s <password>\n\n"
199                                 "Reads from stdin, writes to stdout.\n"
200                                 "Call as \"encode\" to encode, \"decode\" to decode.\n", argv[0] );
201                return( 1 );
202        }
203       
204        memset( irc, 0, sizeof( irc_t ) );
205        irc->password = g_strdup( argv[1] );
206       
207        hash = hashpass( irc );
208        action = argv[0] + strlen( argv[0] ) - strlen( "encode" );
209       
210        if( strcmp( action, "encode" ) == 0 )
211        {
212                fwrite( hash, 32, 1, stdout );
213                func = obfucrypt;
214        }
215        else if( strcmp( action, "decode" ) == 0 )
216        {
217                char hash2[32];
218               
219                fread( hash2, 32, 1, stdin );
220                if( memcmp( hash, hash2, 32 ) != 0 )
221                {
222                        fprintf( stderr, "Passwords don't match. Can't decode.\n" );
223                        return( 1 );
224                }
225                func = deobfucrypt;
226        }
227        else
228        {
229                return( main( 0, NULL ) );
230        }
231       
232        while( fscanf( stdin, "%[^\n]255s", line ) > 0 )
233        {
234                char *out;
235               
236                /* Flush the newline */
237                fgetc( stdin );
238               
239                out = func( irc, line );
240                printf( "%s\n", out );
241                g_free( out );
242        }
243       
244        return( 0 );
245}
246
247#endif
Note: See TracBrowser for help on using the repository browser.