]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/authenticate.c
Fix new FileSet exclusion
[bacula/bacula] / bacula / src / filed / authenticate.c
1 /*
2  * Authenticate Director who is attempting to connect.
3  *
4  *   Kern Sibbald, October 2000
5  *
6  *   Version $Id$
7  * 
8  */
9 /*
10    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
11
12    This program is free software; you can redistribute it and/or
13    modify it under the terms of the GNU General Public License as
14    published by the Free Software Foundation; either version 2 of
15    the License, or (at your option) any later version.
16
17    This program is distributed in the hope that it will be useful,
18    but WITHOUT ANY WARRANTY; without even the implied warranty of
19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20    General Public License for more details.
21
22    You should have received a copy of the GNU General Public
23    License along with this program; if not, write to the Free
24    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25    MA 02111-1307, USA.
26
27  */
28   
29 #include "bacula.h"
30 #include "filed.h"
31
32 static char OK_hello[]  = "2000 OK Hello\n";
33 static char Dir_sorry[] = "2999 No go\n";
34
35
36 /********************************************************************* 
37  *
38  */
39 static int authenticate(int rcode, BSOCK *bs)
40 {
41    POOLMEM *dirname;
42    DIRRES *director;
43    int ssl_need = BNET_SSL_NONE;
44
45    if (rcode != R_DIRECTOR) {
46       Emsg1(M_FATAL, 0, _("I only authenticate directors, not %d\n"), rcode);
47       return 0;
48    }
49    if (bs->msglen < 25 || bs->msglen > 200) {
50       Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s. Len=%d.\n"), 
51             bs->who, bs->msglen);
52       return 0;
53    }
54    dirname = get_pool_memory(PM_MESSAGE);
55    dirname = check_pool_memory_size(dirname, bs->msglen);
56
57    if (sscanf(bs->msg, "Hello Director %s calling\n", dirname) != 1) {
58       free_pool_memory(dirname);
59       bs->msg[100] = 0;
60       Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s: %s\n"), 
61             bs->who, bs->msg);
62       return 0;
63    }
64    unbash_spaces(dirname);
65    LockRes();
66    foreach_res(director, R_DIRECTOR) {
67       if (strcmp(director->hdr.name, dirname) == 0)
68          break;
69    }
70    UnlockRes();
71    if (!director) {
72       Emsg2(M_FATAL, 0, _("Connection from unknown Director %s at %s rejected.\n"   
73        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"), 
74             dirname, bs->who);
75       free_pool_memory(dirname);
76       return 0;
77    }
78    btimer_t *tid = start_bsock_timer(bs, 60 * 5);
79    if (!cram_md5_auth(bs, director->password, ssl_need) ||
80        !cram_md5_get_auth(bs, director->password, ssl_need)) {
81       Emsg1(M_FATAL, 0, _("Incorrect password given by Director at %s.\n"  
82        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"), 
83             bs->who);
84       director = NULL;
85    }
86    stop_bsock_timer(tid);
87    free_pool_memory(dirname);
88    return (director != NULL);
89 }
90
91 /*
92  * Inititiate the communications with the Director.
93  * He has made a connection to our server.
94  * 
95  * Basic tasks done here:
96  *   We read Director's initial message and authorize him.
97  *
98  */
99 int authenticate_director(JCR *jcr)
100 {
101    BSOCK *dir = jcr->dir_bsock;
102
103    if (!authenticate(R_DIRECTOR, dir)) {
104       bnet_fsend(dir, "%s", Dir_sorry);
105       Emsg0(M_FATAL, 0, _("Unable to authenticate Director\n"));
106       bmicrosleep(5, 0);
107       return 0;
108    }
109    return bnet_fsend(dir, "%s", OK_hello);
110 }
111
112 /*
113  * First prove our identity to the Storage daemon, then
114  * make him prove his identity.
115  */
116 int authenticate_storagedaemon(JCR *jcr)
117 {
118    BSOCK *sd = jcr->store_bsock;
119    int stat;
120    int ssl_need = BNET_SSL_NONE;
121
122    btimer_t *tid = start_bsock_timer(sd, 60 * 5);
123    stat = cram_md5_get_auth(sd, jcr->sd_auth_key, ssl_need) &&
124           cram_md5_auth(sd, jcr->sd_auth_key, ssl_need);
125    stop_bsock_timer(tid);
126    memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
127    if (!stat) {
128       Jmsg(jcr, M_FATAL, 0, _("Authorization key rejected by Storage daemon.\n"   
129        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
130    }
131    return stat;
132 }