]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/authenticate.c
Fix bscan walk DCRs + add some bools to dev.c
[bacula/bacula] / bacula / src / stored / authenticate.c
1 /*
2  * Authenticate caller
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 "stored.h"
31
32 static char Dir_sorry[] = "3999 No go\n";
33 static char OK_hello[]  = "3000 OK Hello\n";
34
35
36 /********************************************************************* 
37  *
38  *
39  */
40 static int authenticate(int rcode, BSOCK *bs)
41 {
42    POOLMEM *dirname;
43    DIRRES *director = NULL;
44    int ssl_need = BNET_SSL_NONE;
45
46    if (rcode != R_DIRECTOR) {
47       Emsg1(M_FATAL, 0, _("I only authenticate Directors, not %d\n"), rcode);
48       return 0;
49    }
50    if (bs->msglen < 25 || bs->msglen > 200) {
51       Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s. Len=%d.\n"), 
52             bs->who, bs->msglen);
53       return 0;
54    }
55    dirname = get_pool_memory(PM_MESSAGE);
56    dirname = check_pool_memory_size(dirname, bs->msglen);
57
58    if (sscanf(bs->msg, "Hello Director %127s calling\n", dirname) != 1) {
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    director = NULL;
65    unbash_spaces(dirname);
66    LockRes();
67    while ((director=(DIRRES *)GetNextRes(rcode, (RES *)director))) {
68       if (strcmp(director->hdr.name, dirname) == 0)
69          break;
70    }
71    UnlockRes();
72    if (!director) {
73       Emsg2(M_FATAL, 0, _("Connection from unknown Director %s at %s rejected.\n"), 
74             dirname, bs->who);
75       free_pool_memory(dirname);
76       return 0;
77    }
78
79    /* Timeout Hello after 5 mins */
80    btimer_t *tid = start_bsock_timer(bs, 60 * 5);
81    if (!cram_md5_auth(bs, director->password, ssl_need) ||
82        !cram_md5_get_auth(bs, director->password, ssl_need)) {
83       stop_bsock_timer(tid);
84       Emsg0(M_FATAL, 0, _("Incorrect password given by Director.\n"));
85       free_pool_memory(dirname);
86       return 0;
87    }
88    stop_bsock_timer(tid);
89    free_pool_memory(dirname);
90    return 1;
91 }
92
93 /*
94  * Inititiate the message channel with the Director.
95  * He has made a connection to our server.
96  * 
97  * Basic tasks done here:
98  *   Assume the Hello message is already in the input
99  *     buffer.  We then authenticate him.
100  *   Get device, media, and pool information from Director
101  *
102  *   This is the channel across which we will send error
103  *     messages and job status information.
104  */
105 int authenticate_director(JCR *jcr)
106 {
107    BSOCK *dir = jcr->dir_bsock;
108
109    if (!authenticate(R_DIRECTOR, dir)) {
110       bnet_fsend(dir, "%s", Dir_sorry);
111       Emsg1(M_ERROR, 0, _("Unable to authenticate Director at %s.\n"), dir->who);
112       bmicrosleep(5, 0);
113       return 0;
114    }
115    return bnet_fsend(dir, "%s", OK_hello);
116 }
117
118 int authenticate_filed(JCR *jcr)
119 {
120    BSOCK *fd = jcr->file_bsock;
121    int ssl_need = BNET_SSL_NONE;
122
123    /* Timeout Hello after 5 mins */
124    btimer_t *tid = start_bsock_timer(fd, 60 * 5);
125    if (cram_md5_auth(fd, jcr->sd_auth_key, ssl_need) &&
126        cram_md5_get_auth(fd, jcr->sd_auth_key, ssl_need)) {
127       jcr->authenticated = true;
128    }
129    stop_bsock_timer(tid);
130    if (!jcr->authenticated) {
131       Jmsg(jcr, M_FATAL, 0, _("Incorrect authorization key from File daemon at %s rejected.\n"), 
132            fd->who);
133    }
134    return jcr->authenticated;
135 }