]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/cram-md5.c
Win32 tweaks
[bacula/bacula] / bacula / src / lib / cram-md5.c
1 /*
2  *  Challenge Response Authentication Method using MD5 (CRAM-MD5)
3  *
4  * cram-md5 is based on RFC2104.
5  *
6  * Written for Bacula by Kern E. Sibbald, May MMI.
7  *
8  *   Version $Id$
9  */
10 /*
11    Copyright (C) 2000-2005 Kern Sibbald
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License
15    version 2 as amended with additional clauses defined in the
16    file LICENSE in the main source directory.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
21    the file LICENSE for additional details.
22
23  */
24
25 #include "bacula.h"
26
27 /* Authorize other end
28  * Codes that tls_local_need and tls_remote_need can take:
29  *   BNET_TLS_NONE     I cannot do tls
30  *   BNET_TLS_OK       I can do tls, but it is not required on my end
31  *   BNET_TLS_REQUIRED  tls is required on my end
32  */
33 int cram_md5_auth(BSOCK *bs, char *password, int tls_local_need)
34 {
35    struct timeval t1;
36    struct timeval t2;
37    struct timezone tz;
38    int i, ok;
39    char chal[MAXSTRING];
40    char host[MAXSTRING];
41    uint8_t hmac[20];
42
43    gettimeofday(&t1, &tz);
44    for (i=0; i<4; i++) {
45       gettimeofday(&t2, &tz);
46    }
47    srandom((t1.tv_sec&0xffff) * (t2.tv_usec&0xff));
48    if (!gethostname(host, sizeof(host))) {
49       bstrncpy(host, my_name, sizeof(host));
50    }
51    bsnprintf(chal, sizeof(chal), "<%u.%u@%s>", (uint32_t)random(), (uint32_t)time(NULL), host);
52    Dmsg2(50, "send: auth cram-md5 %s ssl=%d\n", chal, tls_local_need);
53    if (!bnet_fsend(bs, "auth cram-md5 %s ssl=%d\n", chal, tls_local_need)) {
54       Dmsg1(50, "Bnet send challenge error.\n", bnet_strerror(bs));
55       return 0;
56    }
57
58    if (bnet_wait_data(bs, 180) <= 0 || bnet_recv(bs) <= 0) {
59       Dmsg1(50, "Bnet receive challenge response error.\n", bnet_strerror(bs));
60       bmicrosleep(5, 0);
61       return 0;
62    }
63    hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
64    bin_to_base64(host, (char *)hmac, 16);
65 // Dmsg3(100, "auth: chal=%s pw=%s hmac=%s\n", chal, password, host);
66    ok = strcmp(bs->msg, host) == 0;
67    if (ok) {
68       Dmsg1(50, "Authenticate OK %s\n", host);
69    } else {
70       Dmsg2(50, "Authenticate NOT OK: wanted %s, got %s\n", host, bs->msg);
71    }
72    if (ok) {
73       bnet_fsend(bs, "1000 OK auth\n");
74    } else {
75       Dmsg1(50, "Auth failed PW: %s\n", password);
76       bnet_fsend(bs, _("1999 Authorization failed.\n"));
77       bmicrosleep(5, 0);
78    }
79    return ok;
80 }
81
82 /* Get authorization from other end */
83 int cram_md5_get_auth(BSOCK *bs, char *password, int *tls_remote_need)
84 {
85    char chal[MAXSTRING];
86    uint8_t hmac[20];
87
88    if (bnet_recv(bs) <= 0) {
89       bmicrosleep(5, 0);
90       return 0;
91    }
92    if (bs->msglen >= MAXSTRING) {
93       Dmsg1(50, "Msg too long wanted auth cram... Got: %s", bs->msg);
94       bmicrosleep(5, 0);
95       return 0;
96    }
97    Dmsg1(100, "cram-get: %s", bs->msg);
98    if (sscanf(bs->msg, "auth cram-md5 %s ssl=%d\n", chal, tls_remote_need) != 2) {
99       if (sscanf(bs->msg, "auth cram-md5 %s\n", chal) != 1) {
100          Dmsg1(50, "Cannot scan challenge: %s", bs->msg);
101          bnet_fsend(bs, _("1999 Authorization failed.\n"));
102          bmicrosleep(5, 0);
103          return 0;
104       }
105    }
106
107    hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
108    bs->msglen = bin_to_base64(bs->msg, (char *)hmac, 16) + 1;
109 // Dmsg3(100, "get_auth: chal=%s pw=%s hmac=%s\n", chal, password, bs->msg);
110    if (!bnet_send(bs)) {
111       Dmsg1(50, "Send challenge failed. ERR=%s\n", bnet_strerror(bs));
112       return 0;
113    }
114    Dmsg1(99, "sending resp to challenge: %s\n", bs->msg);
115    if (bnet_wait_data(bs, 180) <= 0 || bnet_recv(bs) <= 0) {
116       Dmsg1(50, "Receive chanllenge response failed. ERR=%s\n", bnet_strerror(bs));
117       bmicrosleep(5, 0);
118       return 0;
119    }
120    if (strcmp(bs->msg, "1000 OK auth\n") == 0) {
121       return 1;
122    }
123    Dmsg1(50, "Bad auth response: %s\n", bs->msg);
124    bmicrosleep(5, 0);
125    return 0;
126 }