]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/cram-md5.c
Fix #2085 About director segfault in cram-md5 function
[bacula/bacula] / bacula / src / lib / cram-md5.c
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2016 Kern Sibbald
5
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *  Challenge Response Authentication Method using MD5 (CRAM-MD5)
21  *
22  * cram-md5 is based on RFC2104.
23  *
24  * Written for Bacula by Kern E. Sibbald, May MMI.
25  *
26  */
27
28 #include "bacula.h"
29
30 const int dbglvl = 50;
31
32
33 /* Authorize other end
34  * Codes that tls_local_need and tls_remote_need can take:
35  *   BNET_TLS_NONE     I cannot do tls
36  *   BNET_TLS_OK       I can do tls, but it is not required on my end
37  *   BNET_TLS_REQUIRED  tls is required on my end
38  *
39  *   Returns: false if authentication failed
40  *            true if OK
41  */
42 bool cram_md5_challenge(BSOCK *bs, const char *password, int tls_local_need, int compatible)
43 {
44    struct timeval t1;
45    struct timeval t2;
46    struct timezone tz;
47    int i;
48    bool ok;
49    char chal[MAXSTRING];
50    char host[MAXSTRING];
51    uint8_t hmac[20];
52
53    if (!bs) {
54       Dmsg0(dbglvl, "Invalid bsock\n");
55       return false;
56    }
57
58    gettimeofday(&t1, &tz);
59    for (i=0; i<4; i++) {
60       gettimeofday(&t2, &tz);
61    }
62    srandom((t1.tv_sec&0xffff) * (t2.tv_usec&0xff));
63    if (!gethostname(host, sizeof(host))) {
64       bstrncpy(host, my_name, sizeof(host));
65    }
66    /* Send challenge -- no hashing yet */
67    bsnprintf(chal, sizeof(chal), "<%u.%u@%s>", (uint32_t)random(), (uint32_t)time(NULL), host);
68    if (compatible) {
69       Dmsg2(dbglvl, "send: auth cram-md5 challenge %s ssl=%d\n", chal, tls_local_need);
70       if (!bs->fsend("auth cram-md5 %s ssl=%d\n", chal, tls_local_need)) {
71          Dmsg1(dbglvl, "Send challenge comm error. ERR=%s\n", bs->bstrerror());
72          return false;
73       }
74    } else {
75       /* Old non-compatible system */
76       Dmsg2(dbglvl, "send: auth cram-md5 challenge %s ssl=%d\n", chal, tls_local_need);
77       if (!bs->fsend("auth cram-md5 %s ssl=%d\n", chal, tls_local_need)) {
78          Dmsg1(dbglvl, "Send challenge comm error. ERR=%s\n", bs->bstrerror());
79          return false;
80       }
81    }
82
83    /* Read hashed response to challenge */
84    if (bs->wait_data(180) <= 0 || bs->recv() <= 0) {
85       Dmsg1(dbglvl, "Receive cram-md5 response comm error. ERR=%s\n", bs->bstrerror());
86       bmicrosleep(5, 0);
87       return false;
88    }
89
90    /* Attempt to duplicate hash with our password */
91    hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
92    bin_to_base64(host, sizeof(host), (char *)hmac, 16, compatible);
93    ok = strcmp(bs->msg, host) == 0;
94    if (ok) {
95       Dmsg1(dbglvl, "Authenticate OK %s\n", host);
96    } else {
97       bin_to_base64(host, sizeof(host), (char *)hmac, 16, false);
98       ok = strcmp(bs->msg, host) == 0;
99       if (!ok) {
100          Dmsg2(dbglvl, "Authenticate NOT OK: wanted %s, got %s\n", host, bs->msg);
101       }
102    }
103    if (ok) {
104       bs->fsend("1000 OK auth\n");
105    } else {
106       bs->fsend(_("1999 Authorization failed.\n"));
107       bmicrosleep(5, 0);
108    }
109    return ok;
110 }
111
112 /* Respond to challenge from other end */
113 bool cram_md5_respond(BSOCK *bs, const char *password, int *tls_remote_need, int *compatible)
114 {
115    char chal[MAXSTRING];
116    uint8_t hmac[20];
117
118    if (!bs) {
119       Dmsg0(dbglvl, "Invalid bsock\n");
120       return false;
121    }
122
123    *compatible = false;
124    if (bs->recv() <= 0) {
125       bmicrosleep(5, 0);
126       return false;
127    }
128    if (bs->msglen >= MAXSTRING) {
129       Dmsg1(dbglvl, "Msg too long wanted auth cram... Got: %s", bs->msg);
130       bmicrosleep(5, 0);
131       return false;
132    }
133    Dmsg1(100, "cram-get received: %s", bs->msg);
134    /*
135     * Note that the next call is only to keep compatibility with very
136     *  old versions of Bacula that used a non-compatible base64 algorithm.
137     */
138    if (sscanf(bs->msg, "auth cram-md5c %s ssl=%d", chal, tls_remote_need) == 2) {
139       *compatible = true;
140    } else if (sscanf(bs->msg, "auth cram-md5 %s ssl=%d", chal, tls_remote_need) != 2) {
141       if (sscanf(bs->msg, "auth cram-md5 %s\n", chal) != 1) {
142          Dmsg1(dbglvl, "Cannot scan received response to challenge: %s", bs->msg);
143          bs->fsend(_("1999 Authorization failed.\n"));
144          bmicrosleep(5, 0);
145          return false;
146       }
147    }
148
149    hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
150    bs->msglen = bin_to_base64(bs->msg, 50, (char *)hmac, 16, *compatible) + 1;
151 // Don't turn the following on except for local debugging -- security
152 // Dmsg3(100, "get_auth: chal=%s pw=%s hmac=%s\n", chal, password, bs->msg);
153    if (!bs->send()) {
154       Dmsg1(dbglvl, "Send challenge failed. ERR=%s\n", bs->bstrerror());
155       return false;
156    }
157    Dmsg1(99, "sending resp to challenge: %s\n", bs->msg);
158    if (bs->wait_data(180) <= 0 || bs->recv() <= 0) {
159       Dmsg1(dbglvl, "Receive cram-md5 response failed. ERR=%s\n", bs->bstrerror());
160       bmicrosleep(5, 0);
161       return false;
162    }
163    if (strcmp(bs->msg, "1000 OK auth\n") == 0) {
164       return true;
165    }
166    Dmsg1(dbglvl, "Received bad response: %s\n", bs->msg);
167    bmicrosleep(5, 0);
168    return false;
169 }