3 * Bacula Director -- authorize.c -- handles authorization of
4 * Storage and File daemons.
6 * Kern Sibbald, May MMI
8 * This routine runs as a thread and must be thread reentrant.
14 Copyright (C) 2001-2004 Kern Sibbald and John Walker
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.
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.
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,
36 extern DIRRES *director;
37 extern char my_name[];
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";
43 /* Response from Storage daemon */
44 static char OKhello[] = "3000 OK Hello\n";
45 static char FDOKhello[] = "2000 OK Hello\n";
47 /* Sent to User Agent */
48 static char Dir_sorry[] = "1999 You are not authorized.\n";
50 /* Forward referenced functions */
53 * Authenticate Storage daemon connection
55 bool authenticate_storage_daemon(JCR *jcr, STORE *store)
57 BSOCK *sd = jcr->store_bsock;
58 char dirname[MAX_NAME_LENGTH];
59 int ssl_need = BNET_SSL_NONE;
60 bool get_auth, auth = false;
63 * Send my name to the Storage daemon then do authentication
65 bstrncpy(dirname, director->hdr.name, sizeof(dirname));
67 /* Timeout Hello after 1 min */
68 btimer_t *tid = start_bsock_timer(sd, AUTH_TIMEOUT);
69 if (!bnet_fsend(sd, hello, dirname)) {
70 stop_bsock_timer(tid);
71 Dmsg1(50, _("Error sending Hello to Storage daemon. ERR=%s\n"), bnet_strerror(sd));
72 Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to Storage daemon. ERR=%s\n"), bnet_strerror(sd));
75 get_auth = cram_md5_get_auth(sd, store->password, ssl_need);
77 auth = cram_md5_auth(sd, store->password, ssl_need);
79 Dmsg1(50, "cram_auth failed for %s\n", sd->who);
82 Dmsg1(50, "cram_get_auth failed for %s\n", sd->who);
84 if (!get_auth || !auth) {
85 stop_bsock_timer(tid);
86 Dmsg0(50, _("Director and Storage daemon passwords or names not the same.\n"));
87 Jmsg0(jcr, M_FATAL, 0, _("Director and Storage daemon passwords or names not the same.\n"
88 "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
91 Dmsg1(116, ">stored: %s", sd->msg);
92 if (bnet_recv(sd) <= 0) {
93 stop_bsock_timer(tid);
94 Jmsg1(jcr, M_FATAL, 0, _("bdird<stored: bad response to Hello command: ERR=%s\n"),
98 Dmsg1(110, "<stored: %s", sd->msg);
99 stop_bsock_timer(tid);
100 if (strncmp(sd->msg, OKhello, sizeof(OKhello)) != 0) {
101 Dmsg0(50, _("Storage daemon rejected Hello command\n"));
102 Jmsg0(jcr, M_FATAL, 0, _("Storage daemon rejected Hello command\n"));
109 * Authenticate File daemon connection
111 int authenticate_file_daemon(JCR *jcr)
113 BSOCK *fd = jcr->file_bsock;
114 char dirname[MAX_NAME_LENGTH];
115 int ssl_need = BNET_SSL_NONE;
116 bool get_auth, auth = false;
119 * Send my name to the File daemon then do authentication
121 bstrncpy(dirname, director->hdr.name, sizeof(dirname));
122 bash_spaces(dirname);
123 /* Timeout Hello after 10 mins */
124 btimer_t *tid = start_bsock_timer(fd, AUTH_TIMEOUT);
125 if (!bnet_fsend(fd, hello, dirname)) {
126 stop_bsock_timer(tid);
127 Jmsg(jcr, M_FATAL, 0, _("Error sending Hello to File daemon. ERR=%s\n"), bnet_strerror(fd));
130 get_auth = cram_md5_get_auth(fd, jcr->client->password, ssl_need);
132 auth = cram_md5_auth(fd, jcr->client->password, ssl_need);
134 Dmsg1(50, "cram_auth failed for %s\n", fd->who);
137 Dmsg1(50, "cram_get_auth failed for %s\n", fd->who);
139 if (!get_auth || !auth) {
140 stop_bsock_timer(tid);
141 Dmsg0(50, _("Director and File daemon passwords or names not the same.\n"));
142 Jmsg(jcr, M_FATAL, 0, _("Director and File daemon passwords or names not the same.\n"
143 "Please see http://www.bacula.org/html-manual/faq.html#AuthorizationErrors for help.\n"));
146 Dmsg1(116, ">filed: %s", fd->msg);
147 if (bnet_recv(fd) <= 0) {
148 stop_bsock_timer(tid);
149 Dmsg1(50, _("Bad response from File daemon to Hello command: ERR=%s\n"),
151 Jmsg(jcr, M_FATAL, 0, _("Bad response from File daemon to Hello command: ERR=%s\n"),
155 Dmsg1(110, "<stored: %s", fd->msg);
156 stop_bsock_timer(tid);
157 if (strncmp(fd->msg, FDOKhello, sizeof(FDOKhello)) != 0) {
158 Dmsg0(50, _("File daemon rejected Hello command\n"));
159 Jmsg(jcr, M_FATAL, 0, _("File daemon rejected Hello command\n"));
165 /*********************************************************************
168 int authenticate_user_agent(UAContext *uac)
170 char name[MAX_NAME_LENGTH];
171 int ssl_need = BNET_SSL_NONE;
173 BSOCK *ua = uac->UA_sock;
175 // Emsg4(M_INFO, 0, _("UA Hello from %s:%s:%d is invalid. Len=%d\n"), ua->who,
176 // ua->host, ua->port, ua->msglen);
177 if (ua->msglen < 16 || ua->msglen >= MAX_NAME_LENGTH + 15) {
178 Emsg4(M_ERROR, 0, _("UA Hello from %s:%s:%d is invalid. Len=%d\n"), ua->who,
179 ua->host, ua->port, ua->msglen);
183 if (sscanf(ua->msg, "Hello %127s calling\n", name) != 1) {
184 ua->msg[100] = 0; /* terminate string */
185 Emsg4(M_ERROR, 0, _("UA Hello from %s:%s:%d is invalid. Got: %s\n"), ua->who,
186 ua->host, ua->port, ua->msg);
189 name[sizeof(name)-1] = 0; /* terminate name */
190 if (strcmp(name, "*UserAgent*") == 0) { /* default console */
191 ok = cram_md5_auth(ua, director->password, ssl_need) &&
192 cram_md5_get_auth(ua, director->password, ssl_need);
195 CONRES *cons = (CONRES *)GetResWithName(R_CONSOLE, name);
197 ok = cram_md5_auth(ua, cons->password, ssl_need) &&
198 cram_md5_get_auth(ua, cons->password, ssl_need);
200 uac->cons = cons; /* save console resource pointer */
207 bnet_fsend(ua, "%s", _(Dir_sorry));
208 Emsg4(M_ERROR, 0, _("Unable to authenticate console \"%s\" at %s:%s:%d.\n"),
209 name, ua->who, ua->host, ua->port);
213 bnet_fsend(ua, "1000 OK: %s Version: " VERSION " (" BDATE ")\n", my_name);