[d309eb3] | 1 | /* This file is stolen and slightly modified code */ |
---|
| 2 | |
---|
| 3 | /* zcrypt.c -- Read in a data stream from stdin & dump a decrypted/encrypted * |
---|
| 4 | * datastream. Reads the string to make the key from from the first * |
---|
| 5 | * parameter. Encrypts or decrypts according to -d or -e flag. (-e is * |
---|
| 6 | * default.) Will invoke zwrite if the -c option is provided for * |
---|
| 7 | * encryption. If a zephyr class is specified & the keyfile name omitted * |
---|
| 8 | * the ~/.crypt-table will be checked for "crypt-classname" and then * |
---|
| 9 | * "crypt-default" for the keyfile name. */ |
---|
| 10 | |
---|
| 11 | #include <stdio.h> |
---|
| 12 | #include <stdlib.h> |
---|
| 13 | #include <string.h> |
---|
| 14 | #include <sys/types.h> |
---|
| 15 | #include <sys/wait.h> |
---|
[ac70242] | 16 | #include "owl.h" |
---|
[d309eb3] | 17 | |
---|
[c86a35c] | 18 | #ifdef OWL_ENABLE_ZCRYPT |
---|
[c269e22] | 19 | |
---|
[d309eb3] | 20 | #define BASE_CODE 70 |
---|
| 21 | #define LAST_CODE (BASE_CODE + 15) |
---|
| 22 | #define OUTPUT_BLOCK_SIZE 16 |
---|
| 23 | #include <unistd.h> |
---|
| 24 | #include <sys/types.h> |
---|
[806f769] | 25 | |
---|
| 26 | #ifdef HAVE_KERBEROS_IV |
---|
| 27 | #include <kerberosIV/des.h> |
---|
| 28 | #else |
---|
| 29 | #include <openssl/des.h> |
---|
| 30 | #endif |
---|
[d309eb3] | 31 | |
---|
| 32 | #define MAX_KEY 128 |
---|
| 33 | |
---|
| 34 | #ifndef TRUE |
---|
| 35 | #define TRUE -1 |
---|
| 36 | #endif |
---|
| 37 | #ifndef FALSE |
---|
| 38 | #define FALSE 0 |
---|
| 39 | #endif |
---|
| 40 | |
---|
| 41 | #define ZWRITE_OPT_NOAUTH (1<<0) |
---|
| 42 | #define ZWRITE_OPT_SIGNATURE (1<<1) |
---|
| 43 | #define ZWRITE_OPT_IGNOREVARS (1<<2) |
---|
| 44 | #define ZWRITE_OPT_VERBOSE (1<<3) |
---|
| 45 | #define ZWRITE_OPT_QUIET (1<<4) |
---|
| 46 | #define ZCRYPT_OPT_MESSAGE (1<<5) |
---|
| 47 | #define ZCRYPT_OPT_IGNOREDOT (1<<6) |
---|
| 48 | |
---|
| 49 | typedef struct |
---|
| 50 | { |
---|
| 51 | int flags; |
---|
| 52 | char *signature; |
---|
| 53 | char *message; |
---|
| 54 | } ZWRITEOPTIONS; |
---|
| 55 | |
---|
[e19eb97] | 56 | char *GetZephyrVarKeyFile(const char *whoami, const char *class, const char *instance); |
---|
[d309eb3] | 57 | |
---|
| 58 | #define M_NONE 0 |
---|
| 59 | #define M_ZEPHYR_ENCRYPT 1 |
---|
| 60 | #define M_DECRYPT 2 |
---|
| 61 | #define M_ENCRYPT 3 |
---|
| 62 | #define M_RANDOMIZE 4 |
---|
| 63 | #define M_SETKEY 5 |
---|
| 64 | |
---|
[3daca13] | 65 | static void owl_zcrypt_string_to_schedule(char *keystring, des_key_schedule schedule) { |
---|
| 66 | #ifdef HAVE_KERBEROS_IV |
---|
| 67 | des_cblock key; |
---|
| 68 | #else |
---|
| 69 | des_cblock _key, *key = &_key; |
---|
| 70 | #endif |
---|
| 71 | |
---|
| 72 | des_string_to_key(keystring, key); |
---|
| 73 | des_key_sched(key, schedule); |
---|
| 74 | } |
---|
| 75 | |
---|
[9ceee9d] | 76 | /* The 'owl_zcrypt_decrypt' function was written by kretch for Owl. |
---|
| 77 | * Decrypt the message in 'in' on class 'class' and instance |
---|
| 78 | * 'instance' and leave the result in 'out'. Out must be a buffer |
---|
| 79 | * allocated by the caller. |
---|
| 80 | * |
---|
| 81 | * return 0 on success, otherwise -1 |
---|
| 82 | */ |
---|
[e19eb97] | 83 | int owl_zcrypt_decrypt(char *out, const char *in, const char *class, const char *instance) { |
---|
| 84 | const char *inptr, *endptr; |
---|
[65b2173] | 85 | char *fname, keystring[MAX_KEY]; |
---|
[d309eb3] | 86 | FILE *fkey; |
---|
| 87 | des_key_schedule schedule; |
---|
[3daca13] | 88 | unsigned char input[8], output[8]; |
---|
[d309eb3] | 89 | int i, c1, c2; |
---|
| 90 | |
---|
| 91 | fname=GetZephyrVarKeyFile("zcrypt", class, instance); |
---|
[9ceee9d] | 92 | if (!fname) return(-1); |
---|
[d309eb3] | 93 | fkey=fopen(fname, "r"); |
---|
| 94 | if (!fkey) return(-1); |
---|
[3daca13] | 95 | if (!fgets(keystring, MAX_KEY-1, fkey)) { |
---|
| 96 | fclose(fkey); |
---|
| 97 | return -1; |
---|
| 98 | } |
---|
[d309eb3] | 99 | fclose(fkey); |
---|
| 100 | |
---|
| 101 | strcpy(out, ""); |
---|
| 102 | |
---|
| 103 | output[0] = '\0'; /* In case no message at all */ |
---|
| 104 | |
---|
[3daca13] | 105 | owl_zcrypt_string_to_schedule(keystring, schedule); |
---|
[d309eb3] | 106 | |
---|
| 107 | inptr=in; |
---|
| 108 | endptr=in+strlen(in)-1; |
---|
| 109 | while (inptr<endptr) { |
---|
| 110 | for (i=0; i<8; i++) { |
---|
| 111 | c1=(inptr[0])-BASE_CODE; |
---|
| 112 | c2=(inptr[1])-BASE_CODE; |
---|
| 113 | input[i]=c1 * 0x10 + c2; |
---|
| 114 | inptr+=2; |
---|
| 115 | } |
---|
[3daca13] | 116 | des_ecb_encrypt(&input, &output, schedule, FALSE); |
---|
| 117 | strncat(out, (const char *)output, 8); |
---|
[d309eb3] | 118 | } |
---|
| 119 | |
---|
[3daca13] | 120 | if (out[0] && out[strlen(out) - 1] != '\n') |
---|
[d309eb3] | 121 | strcat(out, "\n"); |
---|
| 122 | return(0); |
---|
| 123 | } |
---|
| 124 | |
---|
[e19eb97] | 125 | int owl_zcrypt_encrypt(char *out, const char *in, const char *class, const char *instance) { |
---|
[9ceee9d] | 126 | char *fname, keystring[MAX_KEY]; |
---|
| 127 | FILE *fkey; |
---|
| 128 | des_key_schedule schedule; |
---|
[3daca13] | 129 | unsigned char input[8], output[8]; |
---|
[9ceee9d] | 130 | int size, length, i; |
---|
[e19eb97] | 131 | const char *inbuff = NULL, *inptr; |
---|
[9ceee9d] | 132 | int num_blocks=0, last_block_size=0; |
---|
| 133 | |
---|
| 134 | fname=GetZephyrVarKeyFile("zcrypt", class, instance); |
---|
| 135 | if (!fname) return(-1); |
---|
| 136 | fkey=fopen(fname, "r"); |
---|
| 137 | if (!fkey) return(-1); |
---|
[3daca13] | 138 | if (!fgets(keystring, MAX_KEY-1, fkey)) { |
---|
| 139 | fclose(fkey); |
---|
| 140 | return -1; |
---|
| 141 | } |
---|
[9ceee9d] | 142 | fclose(fkey); |
---|
| 143 | |
---|
[3daca13] | 144 | owl_zcrypt_string_to_schedule(keystring, schedule); |
---|
[9ceee9d] | 145 | |
---|
| 146 | inbuff=in; |
---|
| 147 | length=strlen(inbuff); |
---|
| 148 | num_blocks=(length+7)/8; |
---|
| 149 | last_block_size=((length+7)%8)+1; |
---|
| 150 | |
---|
| 151 | strcpy(out, ""); |
---|
| 152 | |
---|
| 153 | inptr=inbuff; |
---|
| 154 | while (TRUE) { |
---|
| 155 | /* Get 8 bytes from buffer */ |
---|
| 156 | if (num_blocks > 1) { |
---|
| 157 | size = 8; |
---|
| 158 | memcpy(input, inptr, size); |
---|
| 159 | inptr+=8; |
---|
| 160 | num_blocks--; |
---|
| 161 | } else if (num_blocks == 1) { |
---|
| 162 | size=last_block_size; |
---|
| 163 | memcpy(input, inptr, size); |
---|
| 164 | num_blocks--; |
---|
| 165 | } else { |
---|
| 166 | size=0; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /* Check for EOF and pad the string to 8 chars, if needed */ |
---|
| 170 | if (size == 0) break; /* END OF INPUT: BREAK FROM while LOOP! */ |
---|
| 171 | |
---|
| 172 | if (size<8) memset(input + size, 0, 8 - size); |
---|
| 173 | |
---|
| 174 | /* Encrypt and output the block */ |
---|
[3daca13] | 175 | des_ecb_encrypt(&input, &output, schedule, TRUE); |
---|
[9ceee9d] | 176 | |
---|
| 177 | for (i = 0; i < 8; i++) { |
---|
[b9cb41b] | 178 | sprintf(out + strlen(out), "%c", ((output[i] & 0xf0) >> 4) + BASE_CODE); |
---|
| 179 | sprintf(out + strlen(out), "%c", (output[i] & 0x0f) + BASE_CODE); |
---|
[9ceee9d] | 180 | } |
---|
| 181 | |
---|
| 182 | if (size < 8) break; |
---|
| 183 | } |
---|
[e3869df] | 184 | return(0); |
---|
[d309eb3] | 185 | } |
---|
| 186 | |
---|
| 187 | |
---|
| 188 | #define MAX_BUFF 258 |
---|
| 189 | #define MAX_SEARCH 3 |
---|
| 190 | /* Find the class/instance in the .crypt-table */ |
---|
[e19eb97] | 191 | char *GetZephyrVarKeyFile(const char *whoami, const char *class, const char *instance) { |
---|
[e3869df] | 192 | char *keyfile = NULL; |
---|
[8412869] | 193 | char *varname[MAX_SEARCH]; |
---|
[d309eb3] | 194 | int length[MAX_SEARCH], i; |
---|
| 195 | char buffer[MAX_BUFF]; |
---|
[8412869] | 196 | char *filename; |
---|
[d309eb3] | 197 | char result[MAX_SEARCH][MAX_BUFF]; |
---|
| 198 | int numsearch = 0; |
---|
| 199 | FILE *fsearch; |
---|
| 200 | |
---|
[8412869] | 201 | memset(varname, 0, sizeof(varname)); |
---|
| 202 | |
---|
[d309eb3] | 203 | /* Determine names to look for in .crypt-table */ |
---|
| 204 | if (instance) { |
---|
[8412869] | 205 | varname[numsearch++] = owl_sprintf("crypt-%s-%s:", (class?class:"message"), instance); |
---|
[d309eb3] | 206 | } |
---|
| 207 | if (class) { |
---|
[8412869] | 208 | varname[numsearch++] = owl_sprintf("crypt-%s:", class); |
---|
[d309eb3] | 209 | } |
---|
[8412869] | 210 | varname[numsearch++] = owl_strdup("crypt-default:"); |
---|
[d309eb3] | 211 | |
---|
| 212 | /* Setup the result array, and determine string lengths */ |
---|
| 213 | for (i = 0; i < numsearch; i++) { |
---|
| 214 | result[i][0] = '\0'; |
---|
| 215 | length[i] = strlen(varname[i]); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /* Open~/.crypt-table */ |
---|
[8412869] | 219 | filename = owl_sprintf("%s/.crypt-table", getenv("HOME")); |
---|
[d309eb3] | 220 | fsearch = fopen(filename, "r"); |
---|
| 221 | if (fsearch) { |
---|
| 222 | /* Scan file for a match */ |
---|
| 223 | while (!feof(fsearch)) { |
---|
[3daca13] | 224 | if (!fgets(buffer, MAX_BUFF - 3, fsearch)) break; |
---|
[d309eb3] | 225 | for (i = 0; i < numsearch; i++) { |
---|
| 226 | if (strncasecmp(varname[i], buffer, length[i]) == 0) { |
---|
| 227 | int j; |
---|
| 228 | for (j = length[i]; buffer[j] == ' '; j++) |
---|
| 229 | ; |
---|
| 230 | strcpy(result[i], &buffer[j]); |
---|
| 231 | if (*result[i]) { |
---|
| 232 | if (result[i][strlen(result[i])-1] == '\n') { |
---|
| 233 | result[i][strlen(result[i])-1] = '\0'; |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | /* Pick the "best" match found */ |
---|
| 241 | keyfile = NULL; |
---|
| 242 | for (i = 0; i < numsearch; i++) { |
---|
| 243 | if (*result[i]) { |
---|
| 244 | keyfile = result[i]; |
---|
| 245 | break; |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | if (keyfile == NULL) { |
---|
[9ceee9d] | 250 | /* printf("Could not find key table entry.\n"); */ |
---|
[d309eb3] | 251 | } else { |
---|
| 252 | /* Prepare result to be returned */ |
---|
[36486be] | 253 | keyfile = owl_strdup(keyfile); |
---|
[d309eb3] | 254 | } |
---|
| 255 | |
---|
| 256 | fclose(fsearch); |
---|
| 257 | } else { |
---|
[9ceee9d] | 258 | /* printf("Could not open key table file: %s\n", filename); */ |
---|
[d309eb3] | 259 | } |
---|
| 260 | |
---|
[8412869] | 261 | for(i = 0; i < MAX_SEARCH; i++) { |
---|
| 262 | owl_free(varname[i]); |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | owl_free(filename); |
---|
| 266 | |
---|
[d309eb3] | 267 | return(keyfile); |
---|
| 268 | } |
---|
| 269 | |
---|
[c269e22] | 270 | #endif |
---|