1 |
lefcha |
1.1 |
#include <stdio.h> |
2 |
|
|
#include <string.h> |
3 |
|
|
|
4 |
|
|
#include "imapfilter.h" |
5 |
|
|
#include "data.h" |
6 |
|
|
|
7 |
|
|
|
8 |
|
|
static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
9 |
|
|
|
10 |
|
|
|
11 |
|
|
/* |
12 |
|
|
* Get password from user interactively. |
13 |
|
|
*/ |
14 |
|
|
void get_password(char *user, char *serv, char *passwd) |
15 |
|
|
{ |
16 |
|
|
char *c; |
17 |
|
|
|
18 |
|
|
tty_store(); |
19 |
|
|
|
20 |
|
|
printf("Enter password for %s@%s: ", user, serv); |
21 |
|
|
|
22 |
|
|
tty_disable_echo(); |
23 |
|
|
|
24 |
|
|
if (fgets(passwd, PASSWORD_LEN, stdin)) |
25 |
|
|
if ((c = strchr(passwd, '\n'))) |
26 |
|
|
*c = 0; |
27 |
|
|
|
28 |
|
|
tty_restore(); |
29 |
|
|
|
30 |
|
|
putchar('\n'); |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
/* |
35 |
|
|
* Encode in base64/radix64 a sequence of three characters. |
36 |
|
|
*/ |
37 |
|
|
char *base64_encode(char *dest, char *src, int n) |
38 |
|
|
{ |
39 |
|
|
if (n == 2) |
40 |
|
|
src[2] = 0; |
41 |
|
|
if (n == 1) |
42 |
|
|
src[1] = 0; |
43 |
|
|
|
44 |
|
|
dest[0] = base64[src[0] >> 2]; |
45 |
|
|
dest[1] = base64[((src[0] & 0x03) << 4) | (src[1] >> 4)]; |
46 |
|
|
dest[2] = base64[((src[1] & 0x0f) << 2) | (src[2] >> 6)]; |
47 |
|
|
dest[3] = base64[src[2] & 0x3f]; |
48 |
|
|
|
49 |
|
|
if (n <= 2) |
50 |
|
|
dest[3] = '='; |
51 |
|
|
if (n <= 1) |
52 |
|
|
dest[2] = '='; |
53 |
|
|
|
54 |
|
|
return dest; |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
|
58 |
|
|
/* |
59 |
|
|
* Decode from base64/radix64 a sequence of four characters. |
60 |
|
|
*/ |
61 |
|
|
int base64_decode(char *dest, char *src) |
62 |
|
|
{ |
63 |
|
|
int n, i, t; |
64 |
|
|
char tmp[4]; |
65 |
|
|
|
66 |
|
|
if (src[2] == '=') { |
67 |
|
|
n = 1; |
68 |
|
|
src[2] = src[3] = 0; |
69 |
|
|
} else if (src[3] == '=') { |
70 |
|
|
n = 2; |
71 |
|
|
src[3] = 0; |
72 |
|
|
} else |
73 |
|
|
n = 3; |
74 |
|
|
|
75 |
|
|
for (i = 0; i < 4; i++) { |
76 |
|
|
t = strchr(base64, src[i]) - base64; |
77 |
|
|
if (t == 64) |
78 |
|
|
tmp[i] = 0; |
79 |
|
|
else |
80 |
|
|
tmp[i] = t; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
dest[0] = (tmp[0] << 2) | (tmp[1] >> 4); |
84 |
|
|
dest[1] = (tmp[1] << 4) | (tmp[2] >> 2); |
85 |
|
|
dest[2] = (tmp[2] << 6) | tmp[3]; |
86 |
|
|
|
87 |
|
|
return n; |
88 |
|
|
} |