]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/authenticate.c
This commit was manufactured by cvs2svn to create tag
[bacula/bacula] / bacula / src / dird / authenticate.c
1 /*
2  *
3  *   Bacula Director -- authorize.c -- handles authorization of
4  *     Storage and File daemons.
5  *
6  *     Kern Sibbald, May MMI
7  *
8  *    This routine runs as a thread and must be thread reentrant.
9  *
10  *   Version $Id$
11  *
12  */
13 /*
14    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
15
16    This program is free software; you can redistribute it and/or
17    modify it under the terms of the GNU General Public License as
18    published by the Free Software Foundation; either version 2 of
19    the License, or (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU General Public
27    License along with this program; if not, write to the Free
28    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
29    MA 02111-1307, USA.
30
31  */
32
33 #include "bacula.h"
34 #include "dird.h"
35
36 extern DIRRES *director;
37 extern char my_name[];
38
39 /* Commands sent to Storage daemon and File daemon and received
40  *  from the User Agent */
41 static char hello[]    = "Hello Director %s calling\n";
42
43 /* Response from Storage daemon */
44 static char OKhello[]   = "3000 OK Hello\n";
45 static char FDOKhello[] = "2000 OK Hello\n";
46
47 /* Sent to User Agent */
48 static char Dir_sorry[]  = N_("1999 You are not authorized.\n");
49
50 /* Forward referenced functions */
51
52 /*
53  * Authenticate Storage daemon connection
54  */
55 int authenticate_storage_daemon(JCR *jcr)
56 {
57    BSOCK *sd = jcr->store_bsock;
58    char dirname[MAX_NAME_LENGTH];
59    int ssl_need = BNET_SSL_NONE;
60
61    /* 
62     * Send my name to the Storage daemon then do authentication
63     */
64    bstrncpy(dirname, director->hdr.name, sizeof(dirname));
65    bash_spaces(dirname);
66    if (!bnet_fsend(sd, hello, dirname)) {
67       Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to Storage daemon. ERR=%s\n"), bnet_strerror(sd));
68       return 0;
69    }
70    if (!cram_md5_get_auth(sd, jcr->store->password, ssl_need) || 
71        !cram_md5_auth(sd, jcr->store->password, ssl_need)) {
72       Jmsg0(jcr, M_FATAL, 0, _("Director and Storage daemon passwords or names not the same.\n"));
73       return 0;
74    }
75    Dmsg1(116, ">stored: %s", sd->msg);
76    if (bnet_recv(sd) <= 0) {
77       Jmsg1(jcr, M_FATAL, 0, _("bdird<stored: bad response to Hello command: ERR=%s\n"),
78          bnet_strerror(sd));
79       return 0;
80    }
81    Dmsg1(110, "<stored: %s", sd->msg);
82    if (strncmp(sd->msg, OKhello, sizeof(OKhello)) != 0) {
83       Jmsg0(jcr, M_FATAL, 0, _("Storage daemon rejected Hello command\n"));
84       return 0;
85    }
86    return 1;
87 }
88
89 /*
90  * Authenticate File daemon connection
91  */
92 int authenticate_file_daemon(JCR *jcr)
93 {
94    BSOCK *fd = jcr->file_bsock;
95    char dirname[MAX_NAME_LENGTH];
96    int ssl_need = BNET_SSL_NONE;
97
98    /* 
99     * Send my name to the File daemon then do authentication
100     */
101    bstrncpy(dirname, director->hdr.name, sizeof(dirname));
102    bash_spaces(dirname);
103    if (!bnet_fsend(fd, hello, dirname)) {
104       Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to File daemon. ERR=%s\n"), bnet_strerror(fd));
105       return 0;
106    }
107    if (!cram_md5_get_auth(fd, jcr->client->password, ssl_need) || 
108        !cram_md5_auth(fd, jcr->client->password, ssl_need)) {
109       Jmsg(jcr, M_FATAL, 0, _("Director and File daemon passwords or names not the same.\n"));
110       return 0;
111    }
112    Dmsg1(116, ">filed: %s", fd->msg);
113    if (bnet_recv(fd) <= 0) {
114       Jmsg(jcr, M_FATAL, 0, _("Bad response from File daemon to Hello command: ERR=%s\n"),
115          bnet_strerror(fd));
116       return 0;
117    }
118    Dmsg1(110, "<stored: %s", fd->msg);
119    if (strncmp(fd->msg, FDOKhello, sizeof(FDOKhello)) != 0) {
120       Jmsg(jcr, M_FATAL, 0, _("File daemon rejected Hello command\n"));
121       return 0;
122    }
123    return 1;
124 }
125
126 /********************************************************************* 
127  *
128  */
129 int authenticate_user_agent(BSOCK *ua)
130 {
131    char name[MAXSTRING];
132    int ssl_need = BNET_SSL_NONE;
133    int ok;    
134
135    if (ua->msglen < 16 || ua->msglen >= MAXSTRING-1) {
136       Emsg2(M_ERROR, 0, _("UA Hello from %s is invalid. Len=%d\n"), ua->who, 
137             ua->msglen);
138       return 0;
139    }
140
141    if (sscanf(ua->msg, "Hello %127s calling\n", name) != 1) {
142       ua->msg[100] = 0;               /* terminate string */
143       Emsg2(M_ERROR, 0, _("UA Hello from %s is invalid. Got: %s\n"), ua->who,
144             ua->msg);
145       return 0;
146    }
147
148    ok = cram_md5_auth(ua, director->password, ssl_need) &&
149         cram_md5_get_auth(ua, director->password, ssl_need);
150
151    if (!ok) {
152       bnet_fsend(ua, "%s", _(Dir_sorry));
153       Emsg1(M_WARNING, 0, _("Unable to authenticate User Agent at %s.\n"),
154             ua->who);
155       sleep(5);
156       return 0;
157    }
158    bnet_fsend(ua, "1000 OK: %s Version: " VERSION " (" BDATE ")\n", my_name);
159    return 1;
160 }