]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/authenticate.c
- Make authentication timeout compile time configurable.
[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-2004 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, JCR* jcr)
40 {
41    POOLMEM *dirname;
42    DIRRES *director;
43    int ssl_need = BNET_SSL_NONE;
44    bool auth, get_auth = false;
45
46    if (rcode != R_DIRECTOR) {
47       Dmsg1(50, _("I only authenticate directors, not %d\n"), rcode);
48       Emsg1(M_FATAL, 0, _("I only authenticate directors, not %d\n"), rcode);
49       return 0;
50    }
51    if (bs->msglen < 25 || bs->msglen > 200) {
52       Dmsg2(50, _("Bad Hello command from Director at %s. Len=%d.\n"), 
53             bs->who, bs->msglen);
54       Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s. Len=%d.\n"), 
55             bs->who, bs->msglen);
56       return 0;
57    }
58    dirname = get_pool_memory(PM_MESSAGE);
59    dirname = check_pool_memory_size(dirname, bs->msglen);
60
61    if (sscanf(bs->msg, "Hello Director %s calling\n", dirname) != 1) {
62       free_pool_memory(dirname);
63       bs->msg[100] = 0;
64       Dmsg2(50, _("Bad Hello command from Director at %s: %s\n"), 
65             bs->who, bs->msg);
66       Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s: %s\n"), 
67             bs->who, bs->msg);
68       return 0;
69    }
70    unbash_spaces(dirname);
71    LockRes();
72    foreach_res(director, R_DIRECTOR) {
73       if (strcmp(director->hdr.name, dirname) == 0)
74          break;
75    }
76    UnlockRes();
77    if (!director) {
78       Dmsg2(50, _("Connection from unknown Director %s at %s rejected.\n"),
79             dirname, bs->who);
80       Emsg2(M_FATAL, 0, _("Connection from unknown Director %s at %s rejected.\n"   
81        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"), 
82             dirname, bs->who);
83       free_pool_memory(dirname);
84       return 0;
85    }
86    btimer_t *tid = start_bsock_timer(bs, AUTH_TIMEOUT);
87    auth = cram_md5_auth(bs, director->password, ssl_need);  
88    if (auth) {
89       get_auth = cram_md5_get_auth(bs, director->password, ssl_need);  
90       if (!get_auth) {
91          Dmsg1(50, "cram_get_auth failed for %s\n", bs->who);
92       }
93    } else {
94       Dmsg1(50, "cram_auth failed for %s\n", bs->who);
95    }
96    if (!auth || !get_auth) {
97       Emsg1(M_FATAL, 0, _("Incorrect password given by Director at %s.\n"  
98        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"), 
99             bs->who);
100       director = NULL;
101    }
102    stop_bsock_timer(tid);
103    free_pool_memory(dirname);
104    jcr->director = director;
105    return (director != NULL);
106 }
107
108 /*
109  * Inititiate the communications with the Director.
110  * He has made a connection to our server.
111  * 
112  * Basic tasks done here:
113  *   We read Director's initial message and authorize him.
114  *
115  */
116 int authenticate_director(JCR *jcr)
117 {
118    BSOCK *dir = jcr->dir_bsock;
119
120    if (!authenticate(R_DIRECTOR, dir, jcr)) {
121       bnet_fsend(dir, "%s", Dir_sorry);
122       Emsg0(M_FATAL, 0, _("Unable to authenticate Director\n"));
123       bmicrosleep(5, 0);
124       return 0;
125    }
126    return bnet_fsend(dir, "%s", OK_hello);
127 }
128
129 /*
130  * First prove our identity to the Storage daemon, then
131  * make him prove his identity.
132  */
133 int authenticate_storagedaemon(JCR *jcr)
134 {
135    BSOCK *sd = jcr->store_bsock;
136    int ssl_need = BNET_SSL_NONE;
137    bool get_auth, auth = false;
138
139    btimer_t *tid = start_bsock_timer(sd, AUTH_TIMEOUT);
140    get_auth = cram_md5_get_auth(sd, jcr->sd_auth_key, ssl_need);  
141    if (!get_auth) {
142       Dmsg1(50, "cram_get_auth failed for %s\n", sd->who);
143    } else {
144       auth = cram_md5_auth(sd, jcr->sd_auth_key, ssl_need);
145       if (!auth) {
146          Dmsg1(50, "cram_auth failed for %s\n", sd->who);
147       }
148    }
149    stop_bsock_timer(tid);
150    memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
151    if (!get_auth || !auth) {
152       Jmsg(jcr, M_FATAL, 0, _("Authorization key rejected by Storage daemon.\n"   
153        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
154    }
155    return get_auth && auth;
156 }