]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/authenticate.c
Update doc and authentication error message
[bacula/bacula] / bacula / src / dird / authenticate.c
1 /*
2  *
3  *   Bacula Director -- authorize.c -- handles authorization of
4  *     Storage and File daemons.
5  *
6  *     Kern Sibbald, May MMI
7  *
8  *    This routine runs as a thread and must be thread reentrant.
9  *
10  *   Version $Id$
11  *
12  */
13 /*
14    Copyright (C) 2001-2004 Kern Sibbald and John Walker
15
16    This program is free software; you can redistribute it and/or
17    modify it under the terms of the GNU General Public License as
18    published by the Free Software Foundation; either version 2 of
19    the License, or (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU General Public
27    License along with this program; if not, write to the Free
28    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29    MA 02111-1307, USA.
30
31  */
32
33 #include "bacula.h"
34 #include "dird.h"
35
36 extern DIRRES *director;
37 extern char my_name[];
38
39 /* Commands sent to Storage daemon and File daemon and received
40  *  from the User Agent */
41 static char hello[]    = "Hello Director %s calling\n";
42
43 /* Response from Storage daemon */
44 static char OKhello[]   = "3000 OK Hello\n";
45 static char FDOKhello[] = "2000 OK Hello\n";
46
47 /* Sent to User Agent */
48 static char Dir_sorry[]  = "1999 You are not authorized.\n";
49
50 /* Forward referenced functions */
51
52 /*
53  * Authenticate Storage daemon connection
54  */
55 bool authenticate_storage_daemon(JCR *jcr, STORE *store) 
56 {
57    BSOCK *sd = jcr->store_bsock;
58    char dirname[MAX_NAME_LENGTH];
59    int ssl_need = BNET_SSL_NONE;
60    bool get_auth, auth = false;
61
62    /* 
63     * Send my name to the Storage daemon then do authentication
64     */
65    bstrncpy(dirname, director->hdr.name, sizeof(dirname));
66    bash_spaces(dirname);
67    /* Timeout Hello after 1 min */
68    btimer_t *tid = start_bsock_timer(sd, AUTH_TIMEOUT);
69    if (!bnet_fsend(sd, hello, dirname)) {
70       stop_bsock_timer(tid);
71       Dmsg1(50, _("Error sending Hello to Storage daemon. ERR=%s\n"), bnet_strerror(sd));
72       Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to Storage daemon. ERR=%s\n"), bnet_strerror(sd));
73       return 0;
74    }
75    get_auth = cram_md5_get_auth(sd, store->password, ssl_need);   
76    if (get_auth) {
77       auth = cram_md5_auth(sd, store->password, ssl_need);  
78       if (!auth) {
79          Dmsg1(50, "cram_auth failed for %s\n", sd->who);
80       }
81    } else {
82       Dmsg1(50, "cram_get_auth failed for %s\n", sd->who);
83    }
84    if (!get_auth || !auth) {
85       stop_bsock_timer(tid);
86       Dmsg0(50, _("Director and Storage daemon passwords or names not the same.\n"));
87       Jmsg0(jcr, M_FATAL, 0,
88             _("Director and Storage daemon passwords or names not the same or\n"
89             "you have exceeded the Maximum Concurrent Jobs on the SD.\n"
90             "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
91       return 0;
92    }
93    Dmsg1(116, ">stored: %s", sd->msg);
94    if (bnet_recv(sd) <= 0) {
95       stop_bsock_timer(tid);
96       Jmsg1(jcr, M_FATAL, 0, _("bdird<stored: bad response to Hello command: ERR=%s\n"),
97          bnet_strerror(sd));
98       return 0;
99    }
100    Dmsg1(110, "<stored: %s", sd->msg);
101    stop_bsock_timer(tid);
102    if (strncmp(sd->msg, OKhello, sizeof(OKhello)) != 0) {
103       Dmsg0(50, _("Storage daemon rejected Hello command\n"));
104       Jmsg0(jcr, M_FATAL, 0, _("Storage daemon rejected Hello command\n"));
105       return 0;
106    }
107    return 1;
108 }
109
110 /*
111  * Authenticate File daemon connection
112  */
113 int authenticate_file_daemon(JCR *jcr)
114 {
115    BSOCK *fd = jcr->file_bsock;
116    char dirname[MAX_NAME_LENGTH];
117    int ssl_need = BNET_SSL_NONE;
118    bool get_auth, auth = false;
119
120    /* 
121     * Send my name to the File daemon then do authentication
122     */
123    bstrncpy(dirname, director->hdr.name, sizeof(dirname));
124    bash_spaces(dirname);
125    /* Timeout Hello after 10 mins */
126    btimer_t *tid = start_bsock_timer(fd, AUTH_TIMEOUT);
127    if (!bnet_fsend(fd, hello, dirname)) {
128       stop_bsock_timer(tid);
129       Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to File daemon. ERR=%s\n"), bnet_strerror(fd));
130       return 0;
131    }
132    get_auth = cram_md5_get_auth(fd, jcr->client->password, ssl_need);   
133    if (get_auth) {
134       auth = cram_md5_auth(fd, jcr->client->password, ssl_need);  
135       if (!auth) {
136          Dmsg1(50, "cram_auth failed for %s\n", fd->who);
137       }
138    } else {
139       Dmsg1(50, "cram_get_auth failed for %s\n", fd->who);
140    }
141    if (!get_auth || !auth) {
142       stop_bsock_timer(tid);
143       Dmsg0(50, _("Director and File daemon passwords or names not the same.\n"));
144       Jmsg(jcr, M_FATAL, 0,
145            _("Director and File daemon passwords or names not the same or\n"   
146             "you have exceeded the Maximum Concurrent Jobs on the FD.\n"
147             "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
148       return 0;
149    }
150    Dmsg1(116, ">filed: %s", fd->msg);
151    if (bnet_recv(fd) <= 0) {
152       stop_bsock_timer(tid);
153       Dmsg1(50, _("Bad response from File daemon to Hello command: ERR=%s\n"),
154          bnet_strerror(fd));
155       Jmsg(jcr, M_FATAL, 0, _("Bad response from File daemon to Hello command: ERR=%s\n"),
156          bnet_strerror(fd));
157       return 0;
158    }
159    Dmsg1(110, "<stored: %s", fd->msg);
160    stop_bsock_timer(tid);
161    if (strncmp(fd->msg, FDOKhello, sizeof(FDOKhello)) != 0) {
162       Dmsg0(50, _("File daemon rejected Hello command\n"));
163       Jmsg(jcr, M_FATAL, 0, _("File daemon rejected Hello command\n"));
164       return 0;
165    }
166    return 1;
167 }
168
169 /********************************************************************* 
170  *
171  */
172 int authenticate_user_agent(UAContext *uac) 
173 {
174    char name[MAX_NAME_LENGTH];
175    int ssl_need = BNET_SSL_NONE;
176    bool ok;    
177    BSOCK *ua = uac->UA_sock;
178
179 //  Emsg4(M_INFO, 0, _("UA Hello from %s:%s:%d is invalid. Len=%d\n"), ua->who,
180 //          ua->host, ua->port, ua->msglen);
181    if (ua->msglen < 16 || ua->msglen >= MAX_NAME_LENGTH + 15) {
182       Emsg4(M_ERROR, 0, _("UA Hello from %s:%s:%d is invalid. Len=%d\n"), ua->who,
183             ua->host, ua->port, ua->msglen);
184       return 0;
185    }
186
187    if (sscanf(ua->msg, "Hello %127s calling\n", name) != 1) {
188       ua->msg[100] = 0;               /* terminate string */
189       Emsg4(M_ERROR, 0, _("UA Hello from %s:%s:%d is invalid. Got: %s\n"), ua->who,
190             ua->host, ua->port, ua->msg);
191       return 0;
192    }
193    name[sizeof(name)-1] = 0;             /* terminate name */
194    if (strcmp(name, "*UserAgent*") == 0) {  /* default console */
195       ok = cram_md5_auth(ua, director->password, ssl_need) &&
196            cram_md5_get_auth(ua, director->password, ssl_need);
197    } else {
198       unbash_spaces(name); 
199       CONRES *cons = (CONRES *)GetResWithName(R_CONSOLE, name);
200       if (cons) {
201          ok = cram_md5_auth(ua, cons->password, ssl_need) &&
202               cram_md5_get_auth(ua, cons->password, ssl_need);
203          if (ok) {
204             uac->cons = cons;         /* save console resource pointer */
205          }
206       } else {
207          ok = false;
208       }
209    }
210    if (!ok) {
211       bnet_fsend(ua, "%s", _(Dir_sorry));
212       Emsg4(M_ERROR, 0, _("Unable to authenticate console \"%s\" at %s:%s:%d.\n"),
213             name, ua->who, ua->host, ua->port);
214       sleep(5);
215       return 0;
216    }
217    bnet_fsend(ua, "1000 OK: %s Version: " VERSION " (" BDATE ")\n", my_name);
218    return 1;
219 }