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