source: crypting.c @ 34759e6

Last change on this file since 34759e6 was 34759e6, checked in by Jelmer Vernooij <jelmer@…>, at 2005-12-10T15:28:41Z

Use more GLib functions

  • Property mode set to 100644
File size: 5.0 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#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
62int 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
85char *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
110char *obfucrypt (irc_t *irc, char *line) 
111{
112        int i, j;
113        char *rv;
114       
115        if (irc->password == NULL) return (NULL);
116       
117        rv = g_new0(char, 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
135char *deobfucrypt (irc_t *irc, char *line) 
136{
137        int i, j;
138        char *rv;
139       
140        if (irc->password == NULL) return (NULL);
141       
142        rv = g_new0(char, 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
162int main( int argc, char *argv[] )
163{
164        irc_t *irc = g_new0( irc_t, 1 );
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        irc->password = g_strdup( argv[1] );
177       
178        hash = hashpass( irc );
179        action = argv[0] + strlen( argv[0] ) - strlen( "encode" );
180       
181        if( strcmp( action, "encode" ) == 0 )
182        {
183                fwrite( hash, 32, 1, stdout );
184                func = obfucrypt;
185        }
186        else if( strcmp( action, "decode" ) == 0 )
187        {
188                char hash2[32];
189               
190                fread( hash2, 32, 1, stdin );
191                if( memcmp( hash, hash2, 32 ) != 0 )
192                {
193                        fprintf( stderr, "Passwords don't match. Can't decode.\n" );
194                        return( 1 );
195                }
196                func = deobfucrypt;
197        }
198        else
199        {
200                return( main( 0, NULL ) );
201        }
202       
203        while( fscanf( stdin, "%[^\n]255s", line ) > 0 )
204        {
205                char *out;
206               
207                /* Flush the newline */
208                fgetc( stdin );
209               
210                out = func( irc, line );
211                printf( "%s\n", out );
212                g_free( out );
213        }
214       
215        return( 0 );
216}
217
218#endif
Note: See TracBrowser for help on using the repository browser.