source: crypting.c @ 1eddf6b

Last change on this file since 1eddf6b was 1eddf6b, checked in by Jelmer Vernooij <jelmer@…>, at 2005-12-08T12:30:43Z

Add some const

  • Property mode set to 100644
File size: 5.8 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, const char *value) {
85        setpassnc (irc, value);
86        return (NULL);
87}
88
89int setpass (irc_t *irc, const char *pass, const char* md5sum) 
90{
91        md5_state_t md5state;
92        md5_byte_t digest[16];
93        int i, j;
94        char digits[3];
95       
96        md5_init (&md5state);
97        md5_append (&md5state, (unsigned char *)pass, strlen (pass));
98        md5_finish (&md5state, digest);
99       
100        for (i = 0, j = 0; i < 16; i++, j += 2) {
101                /* Check password for correctness */
102                g_snprintf (digits, sizeof (digits), "%02x\n", digest[i]);
103               
104                if (digits[0] != md5sum[j]) return (-1);
105                if (digits[1] != md5sum[j + 1]) return (-1);
106        }
107       
108        /* If pass is correct, we end up here and we set the pass */
109        setpassnc (irc, pass);
110       
111        return (0);
112}
113
114char *hashpass (irc_t *irc) {
115        md5_state_t md5state;
116        md5_byte_t digest[16];
117        int i;
118        char digits[3];
119        char *rv;
120       
121        if (irc->password == NULL) return (NULL);
122       
123        rv = (char *)g_malloc (33);
124        memset (rv, 0, 33);
125       
126        md5_init (&md5state);
127        md5_append (&md5state, (unsigned char *)irc->password, strlen (irc->password));
128        md5_finish (&md5state, digest);
129       
130        for (i = 0; i < 16; i++) {
131                /* Build a hash of the pass */
132                g_snprintf (digits, sizeof (digits), "%02x", digest[i]);
133                strcat (rv, digits);
134        }
135       
136        return (rv);
137}
138
139char *obfucrypt (irc_t *irc, char *line) {
140        int i, j;
141        char *rv;
142       
143        if (irc->password == NULL) return (NULL);
144       
145        rv = (char *)g_malloc (strlen (line) + 1);
146        memset (rv, '\0', strlen (line) + 1);
147       
148        i = j = 0;
149        while (*line) {
150                /* Encrypt/obfuscate the line, using the password */
151                if (*(signed char*)line < 0) *line = - (*line);
152                if (((signed char*)irc->password)[i] < 0) irc->password[i] = - irc->password[i];
153               
154                rv[j] = *line + irc->password[i]; /* Overflow intended */
155               
156                line++;
157                if (!irc->password[++i]) i = 0;
158                j++;
159        }
160       
161        return (rv);
162}
163
164char *deobfucrypt (irc_t *irc, char *line) {
165        int i, j;
166        char *rv;
167       
168        if (irc->password == NULL) return (NULL);
169       
170        rv = (char *)g_malloc (strlen (line) + 1);
171        memset (rv, '\0', strlen (line) + 1);
172       
173        i = j = 0;
174        while (*line) {
175                /* Decrypt/deobfuscate the line, using the pass */
176                rv[j] = *line - irc->password[i]; /* Overflow intended */
177               
178                line++;
179                if (!irc->password[++i]) i = 0;
180                j++;
181        }
182       
183        return (rv);
184}
185
186#ifdef CRYPTING_MAIN
187
188/* A little main() function for people who want a stand-alone program to
189   encode/decode BitlCrypted files. */
190
191int main( int argc, char *argv[] )
192{
193        irc_t *irc = g_malloc( sizeof( irc_t ) );
194        char *hash, *action, line[256];
195        char* (*func)( irc_t *, char * );
196       
197        if( argc < 2 )
198        {
199                fprintf( stderr, "Usage: %s <password>\n\n"
200                                 "Reads from stdin, writes to stdout.\n"
201                                 "Call as \"encode\" to encode, \"decode\" to decode.\n", argv[0] );
202                return( 1 );
203        }
204       
205        memset( irc, 0, sizeof( irc_t ) );
206        irc->password = g_strdup( argv[1] );
207       
208        hash = hashpass( irc );
209        action = argv[0] + strlen( argv[0] ) - strlen( "encode" );
210       
211        if( strcmp( action, "encode" ) == 0 )
212        {
213                fwrite( hash, 32, 1, stdout );
214                func = obfucrypt;
215        }
216        else if( strcmp( action, "decode" ) == 0 )
217        {
218                char hash2[32];
219               
220                fread( hash2, 32, 1, stdin );
221                if( memcmp( hash, hash2, 32 ) != 0 )
222                {
223                        fprintf( stderr, "Passwords don't match. Can't decode.\n" );
224                        return( 1 );
225                }
226                func = deobfucrypt;
227        }
228        else
229        {
230                return( main( 0, NULL ) );
231        }
232       
233        while( fscanf( stdin, "%[^\n]255s", line ) > 0 )
234        {
235                char *out;
236               
237                /* Flush the newline */
238                fgetc( stdin );
239               
240                out = func( irc, line );
241                printf( "%s\n", out );
242                g_free( out );
243        }
244       
245        return( 0 );
246}
247
248#endif
Note: See TracBrowser for help on using the repository browser.