]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/console/authenticate.c
5f5be7bfca5bdc704081e23a64132afb8176be5d
[bacula/bacula] / bacula / src / console / authenticate.c
1 /*
2  *
3  *   Bacula UA authentication. Provides authentication with
4  *     the Director.
5  *
6  *     Kern Sibbald, June MMI
7  *
8  *    This routine runs as a thread and must be thread reentrant.
9  *
10  *  Basic tasks done here:
11  *
12  */
13 /*
14    Copyright (C) 2000, 2001 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
18    as published by the Free Software Foundation; either version 2
19    of 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
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program; if not, write to the Free Software
28    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
29  */
30
31 #include "bacula.h"
32 #include "console_conf.h"
33 #include "jcr.h"
34
35
36 void senditf(char *fmt, ...);
37 void sendit(char *buf); 
38
39 /* Commands sent to Director */
40 static char hello[]    = "Hello %s calling\n";
41
42 /* Response from Director */
43 static char OKhello[]   = "1000 OK:";
44
45 /* Forward referenced functions */
46
47 /*
48  * Authenticate Director
49  */
50 int authenticate_director(JCR *jcr, DIRRES *director, CONRES *cons)
51 {
52    BSOCK *dir = jcr->dir_bsock;
53    int ssl_need = BNET_SSL_NONE;
54    char bashed_name[MAX_NAME_LENGTH];
55    char *password;
56
57    /* 
58     * Send my name to the Director then do authentication
59     */
60    if (cons) {
61       bstrncpy(bashed_name, cons->hdr.name, sizeof(bashed_name));
62       bash_spaces(bashed_name);
63       password = cons->password;
64    } else {
65       bstrncpy(bashed_name, "*UserAgent*", sizeof(bashed_name));
66       password = director->password;
67    }
68    /* Timeout Hello after 5 mins */
69    btimer_t *tid = start_bsock_timer(dir, 60 * 5);
70    bnet_fsend(dir, hello, bashed_name);
71
72    if (!cram_md5_get_auth(dir, password, ssl_need) || 
73        !cram_md5_auth(dir, password, ssl_need)) {
74       stop_bsock_timer(tid);
75       sendit( _("Director authorization problem.\n"
76             "Most likely the passwords do not agree.\n"));  
77       return 0;
78    }
79
80    Dmsg1(6, ">dird: %s", dir->msg);
81    if (bnet_recv(dir) <= 0) {
82       stop_bsock_timer(tid);
83       senditf(_("Bad response to Hello command: ERR=%s\n"),
84          bnet_strerror(dir));
85       return 0;
86    }
87    Dmsg1(10, "<dird: %s", dir->msg);
88    stop_bsock_timer(tid);
89    if (strncmp(dir->msg, OKhello, sizeof(OKhello)-1) != 0) {
90       sendit(_("Director rejected Hello command\n"));
91       return 0;
92    } else {
93       sendit(dir->msg);
94    }
95    return 1;
96 }