2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from
7 many others, a complete list can be found in the file AUTHORS.
8 This program is Free Software; you can redistribute it and/or
9 modify it under the terms of version three of the GNU Affero General Public
10 License as published by the Free Software Foundation and included
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Affero General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 Bacula® is a registered trademark of Kern Sibbald.
24 The licensor of Bacula is the Free Software Foundation Europe
25 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26 Switzerland, email:ftf@fsfeurope.org.
30 * Bacula Director -- User Agent Server
32 * Kern Sibbald, September MM
40 /* Imported variables */
43 /* Forward referenced functions */
44 extern "C" void *connect_thread(void *arg);
45 static void *handle_UA_client_request(void *arg);
48 /* Global variables */
49 static int started = FALSE;
50 static workq_t ua_workq;
57 /* Called here by Director daemon to start UA (user agent)
58 * command thread. This routine creates the thread and then
61 void start_UA_server(dlist *addrs)
65 static dlist *myaddrs = addrs;
67 if ((status=pthread_create(&thid, NULL, connect_thread, (void *)myaddrs)) != 0) {
69 Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), be.bstrerror(status));
76 void *connect_thread(void *arg)
78 pthread_detach(pthread_self());
79 set_jcr_in_tsd(INVALID_JCR);
81 /* Permit MaxConsoleConnect console connections */
82 bnet_thread_server((dlist*)arg, director->MaxConsoleConnect, &ua_workq, handle_UA_client_request);
87 * Create a Job Control Record for a control "job",
88 * filling in all the appropriate fields.
90 JCR *new_control_jcr(const char *base_name, int job_type)
93 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
95 * The job and defaults are not really used, but
96 * we set them up to ensure that everything is correctly
100 jcr->job = (JOB *)GetNextRes(R_JOB, NULL);
101 set_jcr_defaults(jcr, jcr->job);
103 jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
104 create_unique_job_name(jcr, base_name);
105 jcr->sched_time = jcr->start_time;
106 jcr->set_JobType(job_type);
107 jcr->set_JobLevel(L_NONE);
108 set_jcr_job_status(jcr, JS_Running);
114 * Handle Director User Agent commands
117 static void *handle_UA_client_request(void *arg)
122 BSOCK *user = (BSOCK *)arg;
124 pthread_detach(pthread_self());
126 jcr = new_control_jcr("-Console-", JT_CONSOLE);
128 ua = new_ua_context(jcr);
130 set_jcr_in_tsd(INVALID_JCR);
132 user->recv(); /* Get first message */
133 if (!authenticate_user_agent(ua)) {
138 if (ua->api) user->signal(BNET_MAIN_PROMPT);
141 pm_strcpy(ua->cmd, ua->UA_sock->msg);
143 if (ua->argc > 0 && ua->argk[0][0] == '.') {
144 do_a_dot_command(ua);
148 dequeue_messages(ua->jcr);
150 if (console_msg_pending && acl_access_ok(ua, Command_ACL, "messages", 8)) {
151 if (ua->auto_display_messages) {
152 pm_strcpy(ua->cmd, "messages");
153 qmessagescmd(ua, ua->cmd);
154 ua->user_notified_msg_pending = false;
155 } else if (!ua->gui && !ua->user_notified_msg_pending && console_msg_pending) {
157 user->signal(BNET_MSGS_PENDING);
159 bsendmsg(ua, _("You have messages.\n"));
161 ua->user_notified_msg_pending = true;
164 if (!ua->api) user->signal(BNET_EOD); /* send end of command */
166 } else if (is_bnet_stop(user)) {
168 } else { /* signal */
169 user->signal(BNET_POLL);
182 * Create a UAContext for a Job that is running so that
183 * it can the User Agent routines and
184 * to ensure that the Job gets the proper output.
185 * This is a sort of mini-kludge, and should be
186 * unified at some point.
188 UAContext *new_ua_context(JCR *jcr)
192 ua = (UAContext *)malloc(sizeof(UAContext));
193 memset(ua, 0, sizeof(UAContext));
196 ua->cmd = get_pool_memory(PM_FNAME);
197 ua->args = get_pool_memory(PM_FNAME);
198 ua->err = get_pool_memory(PM_FNAME);
200 ua->automount = true;
204 void free_ua_context(UAContext *ua)
207 free_pool_memory(ua->cmd);
210 free_pool_memory(ua->args);
213 free_pool_memory(ua->err);
219 bnet_close(ua->UA_sock);
227 * Called from main Bacula thread
229 void term_ua_server()