]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/authenticate.c
9c4440c1ca9f0b06fcdc36b7a50dce1ea1c1ca5f
[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
44    if (rcode != R_DIRECTOR) {
45       Emsg1(M_FATAL, 0, _("I only authenticate directors, not %d\n"), rcode);
46       return 0;
47    }
48    if (bs->msglen < 25 || bs->msglen > 200) {
49       Emsg0(M_FATAL, 0, _("Bad Hello command from Director.\n"));
50       return 0;
51    }
52    dirname = get_pool_memory(PM_MESSAGE);
53    dirname = check_pool_memory_size(dirname, bs->msglen);
54
55    if (sscanf(bs->msg, "Hello Director %s calling\n", dirname) != 1) {
56       free_pool_memory(dirname);
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       free_pool_memory(dirname);
72       return 0;
73    }
74    if (!cram_md5_auth(bs, director->password) ||
75        !cram_md5_get_auth(bs, director->password)) {
76       Emsg0(M_FATAL, 0, _("Incorrect password given by Director.\n"));
77       director = NULL;
78    }
79    free_pool_memory(dirname);
80    return (director != NULL);
81 }
82
83 /*
84  * Inititiate the communications with the Director.
85  * He has made a connection to our server.
86  * 
87  * Basic tasks done here:
88  *   We read Director's initial message and authorize him.
89  *
90  */
91 int authenticate_director(JCR *jcr)
92 {
93    BSOCK *dir = jcr->dir_bsock;
94
95    if (!authenticate(R_DIRECTOR, dir)) {
96       bnet_fsend(dir, "%s", Dir_sorry);
97       Emsg0(M_FATAL, 0, _("Unable to authenticate Director\n"));
98       sleep(5);
99       return 0;
100    }
101    return bnet_fsend(dir, "%s", OK_hello);
102 }
103
104 /*
105  * First prove our identity to the Storage daemon, then
106  * make him prove his identity.
107  */
108 int authenticate_storagedaemon(JCR *jcr)
109 {
110    BSOCK *sd = jcr->store_bsock;
111    int stat;
112
113    stat = cram_md5_get_auth(sd, jcr->sd_auth_key) &&
114           cram_md5_auth(sd, jcr->sd_auth_key);
115    memset(jcr->sd_auth_key, 0, strlen(jcr->sd_auth_key));
116    if (!stat) {
117       Jmsg(jcr, M_FATAL, 0, _("Authorization key rejected by Storage daemon.\n"));
118    }
119    return stat;
120 }