]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/stored/authenticate.c
- Make authentication timeout compile time configurable.
[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, JCR* jcr)
41 {
42    POOLMEM *dirname;
43    DIRRES *director = NULL;
44    int ssl_need = BNET_SSL_NONE;
45    bool auth, get_auth = false;
46
47    if (rcode != R_DIRECTOR) {
48       Dmsg1(50, _("I only authenticate Directors, not %d\n"), rcode);
49       Emsg1(M_FATAL, 0, _("I only authenticate Directors, not %d\n"), rcode);
50       return 0;
51    }
52    if (bs->msglen < 25 || bs->msglen > 200) {
53       Dmsg2(50, _("Bad Hello command from Director at %s. Len=%d.\n"), 
54             bs->who, bs->msglen);
55       Emsg2(M_FATAL, 0, _("Bad Hello command from Director at %s. Len=%d.\n"), 
56             bs->who, bs->msglen);
57       return 0;
58    }
59    dirname = get_pool_memory(PM_MESSAGE);
60    dirname = check_pool_memory_size(dirname, bs->msglen);
61
62    if (sscanf(bs->msg, "Hello Director %127s calling\n", dirname) != 1) {
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    director = NULL;
71    unbash_spaces(dirname);
72    LockRes();
73    while ((director=(DIRRES *)GetNextRes(rcode, (RES *)director))) {
74       if (strcmp(director->hdr.name, dirname) == 0)
75          break;
76    }
77    UnlockRes();
78    if (!director) {
79       Dmsg2(50, _("Connection from unknown Director %s at %s rejected.\n"), 
80             dirname, bs->who);
81       Emsg2(M_FATAL, 0, _("Connection from unknown Director %s at %s rejected.\n"   
82        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"), 
83             dirname, bs->who);
84       free_pool_memory(dirname);
85       return 0;
86    }
87
88    /* Timeout Hello after 10 mins */
89    btimer_t *tid = start_bsock_timer(bs, AUTH_TIMEOUT);
90    auth = cram_md5_auth(bs, director->password, ssl_need);
91    if (auth) {
92       get_auth = cram_md5_get_auth(bs, director->password, ssl_need);
93       if (!get_auth) {
94          Dmsg1(50, "cram_get_auth failed with %s\n", bs->who);
95       }
96    } else {
97       Dmsg1(50, "cram_auth failed with %s\n", bs->who);
98    }
99    if (!auth || !get_auth) {
100       stop_bsock_timer(tid);
101       Emsg0(M_FATAL, 0, _("Incorrect password given by Director.\n"   
102        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
103       free_pool_memory(dirname);
104       return 0;
105    }
106    stop_bsock_timer(tid);
107    free_pool_memory(dirname);
108    jcr->director = director;
109    return 1;
110 }
111
112 /*
113  * Inititiate the message channel with the Director.
114  * He has made a connection to our server.
115  * 
116  * Basic tasks done here:
117  *   Assume the Hello message is already in the input
118  *     buffer.  We then authenticate him.
119  *   Get device, media, and pool information from Director
120  *
121  *   This is the channel across which we will send error
122  *     messages and job status information.
123  */
124 int authenticate_director(JCR *jcr)
125 {
126    BSOCK *dir = jcr->dir_bsock;
127
128    if (!authenticate(R_DIRECTOR, dir, jcr)) {
129       bnet_fsend(dir, "%s", Dir_sorry);
130       Dmsg1(50, _("Unable to authenticate Director at %s.\n"), dir->who);
131       Emsg1(M_ERROR, 0, _("Unable to authenticate Director at %s.\n"), dir->who);
132       bmicrosleep(5, 0);
133       return 0;
134    }
135    return bnet_fsend(dir, "%s", OK_hello);
136 }
137
138 int authenticate_filed(JCR *jcr)
139 {
140    BSOCK *fd = jcr->file_bsock;
141    int ssl_need = BNET_SSL_NONE;
142    bool auth, get_auth = false;
143
144    /* Timeout Hello after 5 mins */
145    btimer_t *tid = start_bsock_timer(fd, AUTH_TIMEOUT);
146    auth = cram_md5_auth(fd, jcr->sd_auth_key, ssl_need);
147    if (auth) {
148        get_auth = cram_md5_get_auth(fd, jcr->sd_auth_key, ssl_need);
149        if (!get_auth) {
150           Dmsg1(50, "cram-get-auth failed with %s\n", fd->who);
151        }
152    } else {
153       Dmsg1(50, "cram-auth failed with %s\n", fd->who);
154    }
155    if (auth && get_auth) {
156       jcr->authenticated = true;
157    }
158    stop_bsock_timer(tid);
159    if (!jcr->authenticated) {
160       Jmsg(jcr, M_FATAL, 0, _("Incorrect authorization key from File daemon at %s rejected.\n"
161        "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"),
162            fd->who);
163    }
164    return jcr->authenticated;
165 }