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