]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/authenticate.c
dd76c767b50651e6612580021b61008df3f2b75e
[bacula/bacula] / bacula / src / stored / authenticate.c
1 /*
2  * Authenticate caller
3  *
4  *   Kern Sibbald, October 2000
5  * 
6  */
7 /*
8    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26   
27 #include "bacula.h"
28 #include "stored.h"
29
30 static char Dir_sorry[] = "3999 No go\n";
31 static char OK_hello[]  = "3000 OK Hello\n";
32
33
34 /********************************************************************* 
35  *
36  *
37  */
38 static int authenticate(int rcode, BSOCK *bs)
39 {
40    char *name;
41    DIRRES *director;
42
43    if (rcode != R_DIRECTOR) {
44       Emsg1(M_FATAL, 0, _("I only authenticate Directors, not %d\n"), rcode);
45       return 0;
46    }
47    name = (char *) get_pool_memory(PM_MESSAGE);
48    name = (char *) check_pool_memory_size(name, bs->msglen);
49
50    if (sscanf(bs->msg, "Hello Director %127s calling\n", name) != 1) {
51       free_pool_memory(name);
52       Emsg1(M_FATAL, 0, _("Authentication failure: %s\n"), bs->msg);
53       return 0;
54    }
55    director = NULL;
56    LockRes();
57    while ((director=(DIRRES *)GetNextRes(rcode, (RES *)director))) {
58       if (strcmp(director->hdr.name, name) == 0)
59          break;
60    }
61    UnlockRes();
62    if (director && (!cram_md5_auth(bs, director->password) ||
63        !cram_md5_get_auth(bs, director->password))) {
64       director = NULL;
65    }
66    free_pool_memory(name);
67    return (director != NULL);
68 }
69
70 /*
71  * Inititiate the message channel with the Director.
72  * He has made a connection to our server.
73  * 
74  * Basic tasks done here:
75  *   Assume the Hello message is already in the input
76  *     buffer.  We then authenticate him.
77  *   Get device, media, and pool information from Director
78  *
79  *   This is the channel across which we will send error
80  *     messages and job status information.
81  */
82 int authenticate_director(JCR *jcr)
83 {
84    BSOCK *dir = jcr->dir_bsock;
85
86    if (!authenticate(R_DIRECTOR, dir)) {
87       bnet_fsend(dir, "%s", Dir_sorry);
88       Emsg0(M_ERROR, 0, _("Unable to authenticate Director\n"));
89       return 0;
90    }
91    return bnet_fsend(dir, "%s", OK_hello);
92 }
93
94 int authenticate_filed(JCR *jcr)
95 {
96    BSOCK *fd = jcr->file_bsock;
97
98    if (cram_md5_auth(fd, jcr->sd_auth_key) &&
99        cram_md5_get_auth(fd, jcr->sd_auth_key)) {
100       jcr->authenticated = TRUE;
101    }
102    return jcr->authenticated;
103 }