]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/cram-md5.c
a063ad1bac55e315a31ab4d73b495f6c7af336d8
[bacula/bacula] / bacula / src / lib / cram-md5.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2001-2007 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  Challenge Response Authentication Method using MD5 (CRAM-MD5)
30  *
31  * cram-md5 is based on RFC2104.
32  *
33  * Written for Bacula by Kern E. Sibbald, May MMI.
34  *
35  *   Version $Id$
36  */
37
38 #include "bacula.h"
39
40 const int dbglvl = 50;
41
42 /* Authorize other end
43  * Codes that tls_local_need and tls_remote_need can take:
44  *   BNET_TLS_NONE     I cannot do tls
45  *   BNET_TLS_OK       I can do tls, but it is not required on my end
46  *   BNET_TLS_REQUIRED  tls is required on my end
47  *
48  *   Returns: false if authentication failed
49  *            true if OK
50  */
51 bool cram_md5_challenge(BSOCK *bs, const char *password, int tls_local_need, int compatible)
52 {
53    struct timeval t1;
54    struct timeval t2;
55    struct timezone tz;
56    int i;
57    bool ok;
58    char chal[MAXSTRING];
59    char host[MAXSTRING];
60    uint8_t hmac[20];
61
62    gettimeofday(&t1, &tz);
63    for (i=0; i<4; i++) {
64       gettimeofday(&t2, &tz);
65    }
66    srandom((t1.tv_sec&0xffff) * (t2.tv_usec&0xff));
67    if (!gethostname(host, sizeof(host))) {
68       bstrncpy(host, my_name, sizeof(host));
69    }
70    /* Send challenge -- no hashing yet */
71    bsnprintf(chal, sizeof(chal), "<%u.%u@%s>", (uint32_t)random(), (uint32_t)time(NULL), host);
72    if (compatible) {
73       Dmsg2(dbglvl, "send: auth cram-md5 %s ssl=%d\n", chal, tls_local_need);
74       if (!bs->fsend("auth cram-md5 %s ssl=%d\n", chal, tls_local_need)) {
75          Dmsg1(dbglvl, "Bnet send challenge error.\n", bs->bstrerror());
76          return false;
77       }
78    } else {
79       /* Old non-compatible system */
80       Dmsg2(dbglvl, "send: auth cram-md5 %s ssl=%d\n", chal, tls_local_need);
81       if (!bs->fsend("auth cram-md5 %s ssl=%d\n", chal, tls_local_need)) {
82          Dmsg1(dbglvl, "Bnet send challenge error.\n", bs->bstrerror());
83          return false;
84       }
85    }
86
87    /* Read hashed response to challenge */
88    if (bs->wait_data(180) <= 0 || bs->recv() <= 0) {
89       Dmsg1(dbglvl, "Bnet receive challenge response error.\n", bs->bstrerror());
90       bmicrosleep(5, 0);
91       return false;
92    }
93
94    /* Attempt to duplicate hash with our password */
95    hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
96    bin_to_base64(host, sizeof(host), (char *)hmac, 16, compatible);
97    ok = strcmp(bs->msg, host) == 0;
98    if (ok) {
99       Dmsg1(dbglvl, "Authenticate OK %s\n", host);
100    } else {
101       bin_to_base64(host, sizeof(host), (char *)hmac, 16, false);     
102       ok = strcmp(bs->msg, host) == 0;
103       if (!ok) {
104          Dmsg2(dbglvl, "Authenticate NOT OK: wanted %s, got %s\n", host, bs->msg);
105       }
106    }
107    if (ok) {
108       bs->fsend("1000 OK auth\n");
109    } else {
110       Dmsg1(dbglvl, "Auth failed PW: %s\n", password);
111       bs->fsend(_("1999 Authorization failed.\n"));
112       bmicrosleep(5, 0);
113    }
114    return ok;
115 }
116
117 /* Respond to challenge from other end */
118 bool cram_md5_respond(BSOCK *bs, const char *password, int *tls_remote_need, int *compatible)
119 {
120    char chal[MAXSTRING];
121    uint8_t hmac[20];
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    if (sscanf(bs->msg, "auth cram-md5c %s ssl=%d", chal, tls_remote_need) == 2) {
135       *compatible = true;
136    } else if (sscanf(bs->msg, "auth cram-md5 %s ssl=%d", chal, tls_remote_need) != 2) {
137       if (sscanf(bs->msg, "auth cram-md5 %s\n", chal) != 1) {
138          Dmsg1(dbglvl, "Cannot scan challenge: %s", bs->msg);
139          bs->fsend(_("1999 Authorization failed.\n"));
140          bmicrosleep(5, 0);
141          return false;
142       }
143    }
144
145    hmac_md5((uint8_t *)chal, strlen(chal), (uint8_t *)password, strlen(password), hmac);
146    bs->msglen = bin_to_base64(bs->msg, 50, (char *)hmac, 16, *compatible) + 1;
147 // Dmsg3(100, "get_auth: chal=%s pw=%s hmac=%s\n", chal, password, bs->msg);
148    if (!bs->send()) {
149       Dmsg1(dbglvl, "Send challenge failed. ERR=%s\n", bs->bstrerror());
150       return false;
151    }
152    Dmsg1(99, "sending resp to challenge: %s\n", bs->msg);
153    if (bs->wait_data(180) <= 0 || bs->recv() <= 0) {
154       Dmsg1(dbglvl, "Receive chanllenge response failed. ERR=%s\n", bs->bstrerror());
155       bmicrosleep(5, 0);
156       return false;
157    }
158    if (strcmp(bs->msg, "1000 OK auth\n") == 0) {
159       return true;
160    }
161    Dmsg1(dbglvl, "Received bad response: %s\n", bs->msg);
162    bmicrosleep(5, 0);
163    return false;
164 }