]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/authenticate.c
46cc07818b7a59c4425f6f6b2f5b0f0b04434cd6
[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
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       Emsg1(M_FATAL, 0, _("Bad Hello command from Director. Len=%d.\n"), bs->msglen);
51       return 0;
52    }
53    dirname = get_pool_memory(PM_MESSAGE);
54    dirname = check_pool_memory_size(dirname, bs->msglen);
55
56    if (sscanf(bs->msg, "Hello Director %127s calling\n", dirname) != 1) {
57       bs->msg[100] = 0;
58       Emsg1(M_FATAL, 0, _("Bad Hello command from Director: %s\n"), bs->msg);
59       return 0;
60    }
61    director = NULL;
62    unbash_spaces(dirname);
63    LockRes();
64    while ((director=(DIRRES *)GetNextRes(rcode, (RES *)director))) {
65       if (strcmp(director->hdr.name, dirname) == 0)
66          break;
67    }
68    UnlockRes();
69    if (!director) {
70       Emsg1(M_FATAL, 0, _("Connection from unknown Director %s rejected.\n"), dirname);
71       goto bail_out;
72    }
73    if (!cram_md5_auth(bs, director->password) ||
74        !cram_md5_get_auth(bs, director->password)) {
75       Emsg0(M_FATAL, 0, _("Incorrect password given by Director.\n"));
76       goto bail_out;
77    }
78    free_pool_memory(dirname);
79    return 1;
80
81 bail_out:
82    free_pool_memory(dirname);
83    return 0;
84 }
85
86 /*
87  * Inititiate the message channel with the Director.
88  * He has made a connection to our server.
89  * 
90  * Basic tasks done here:
91  *   Assume the Hello message is already in the input
92  *     buffer.  We then authenticate him.
93  *   Get device, media, and pool information from Director
94  *
95  *   This is the channel across which we will send error
96  *     messages and job status information.
97  */
98 int authenticate_director(JCR *jcr)
99 {
100    BSOCK *dir = jcr->dir_bsock;
101
102    if (!authenticate(R_DIRECTOR, dir)) {
103       bnet_fsend(dir, "%s", Dir_sorry);
104       Emsg0(M_ERROR, 0, _("Unable to authenticate Director\n"));
105       sleep(5);
106       return 0;
107    }
108    return bnet_fsend(dir, "%s", OK_hello);
109 }
110
111 int authenticate_filed(JCR *jcr)
112 {
113    BSOCK *fd = jcr->file_bsock;
114
115    if (cram_md5_auth(fd, jcr->sd_auth_key) &&
116        cram_md5_get_auth(fd, jcr->sd_auth_key)) {
117       jcr->authenticated = TRUE;
118    }
119    if (!jcr->authenticated) {
120       Jmsg(jcr, M_FATAL, 0, _("Incorrect authorization key from File daemon rejected.\n"));
121    }
122    return jcr->authenticated;
123 }