2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
7 The original author of Bacula is Kern Sibbald, with contributions
8 from many others, a complete list can be found in the file AUTHORS.
10 You may use this file and others of this release according to the
11 license defined in the LICENSE file, which includes the Affero General
12 Public License, v3.0 ("AGPLv3") and some additional permissions and
13 terms pursuant to its AGPLv3 Section 7.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
22 * Bacula Director -- User Agent Server
24 * Kern Sibbald, September MM
31 /* Imported variables */
34 /* Forward referenced functions */
35 extern "C" void *connect_thread(void *arg);
36 static void *handle_UA_client_request(void *arg);
39 /* Global variables */
40 static int started = FALSE;
41 static workq_t ua_workq;
48 /* Called here by Director daemon to start UA (user agent)
49 * command thread. This routine creates the thread and then
52 void start_UA_server(dlist *addrs)
56 static dlist *myaddrs = addrs;
58 if ((status=pthread_create(&thid, NULL, connect_thread, (void *)myaddrs)) != 0) {
60 Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), be.bstrerror(status));
67 void *connect_thread(void *arg)
69 pthread_detach(pthread_self());
70 set_jcr_in_tsd(INVALID_JCR);
72 /* Permit MaxConsoleConnect console connections */
73 bnet_thread_server((dlist*)arg, director->MaxConsoleConnect, &ua_workq, handle_UA_client_request);
78 * Create a Job Control Record for a control "job",
79 * filling in all the appropriate fields.
81 JCR *new_control_jcr(const char *base_name, int job_type)
84 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
86 * The job and defaults are not really used, but
87 * we set them up to ensure that everything is correctly
91 jcr->job = (JOB *)GetNextRes(R_JOB, NULL);
92 set_jcr_defaults(jcr, jcr->job);
94 jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
95 create_unique_job_name(jcr, base_name);
96 jcr->sched_time = jcr->start_time;
97 jcr->setJobType(job_type);
98 jcr->setJobLevel(L_NONE);
99 jcr->setJobStatus(JS_Running);
105 * Handle Director User Agent commands
108 static void *handle_UA_client_request(void *arg)
113 BSOCK *user = (BSOCK *)arg;
115 pthread_detach(pthread_self());
117 jcr = new_control_jcr("-Console-", JT_CONSOLE);
119 ua = new_ua_context(jcr);
121 set_jcr_in_tsd(INVALID_JCR);
123 user->recv(); /* Get first message */
124 if (!authenticate_user_agent(ua)) {
129 if (ua->api) user->signal(BNET_MAIN_PROMPT);
132 pm_strcpy(ua->cmd, ua->UA_sock->msg);
134 if (ua->argc > 0 && ua->argk[0][0] == '.') {
135 do_a_dot_command(ua);
139 dequeue_messages(ua->jcr);
141 if (console_msg_pending && acl_access_ok(ua, Command_ACL, "messages", 8)) {
142 if (ua->auto_display_messages) {
143 pm_strcpy(ua->cmd, "messages");
144 qmessagescmd(ua, ua->cmd);
145 ua->user_notified_msg_pending = false;
146 } else if (!ua->gui && !ua->user_notified_msg_pending && console_msg_pending) {
148 user->signal(BNET_MSGS_PENDING);
150 bsendmsg(ua, _("You have messages.\n"));
152 ua->user_notified_msg_pending = true;
155 if (!ua->api) user->signal(BNET_EOD); /* send end of command */
157 } else if (user->is_stop()) {
159 } else { /* signal */
160 user->signal(BNET_POLL);
163 /* At the end of each command, revert to the main shared SQL link */
164 ua->db = ua->shared_db;
176 * Create a UAContext for a Job that is running so that
177 * it can the User Agent routines and
178 * to ensure that the Job gets the proper output.
179 * This is a sort of mini-kludge, and should be
180 * unified at some point.
182 UAContext *new_ua_context(JCR *jcr)
186 ua = (UAContext *)malloc(sizeof(UAContext));
187 memset(ua, 0, sizeof(UAContext));
189 ua->shared_db = ua->db = jcr->db;
190 ua->cmd = get_pool_memory(PM_FNAME);
191 ua->args = get_pool_memory(PM_FNAME);
192 ua->errmsg = get_pool_memory(PM_FNAME);
194 ua->automount = true;
198 void free_ua_context(UAContext *ua)
201 free_pool_memory(ua->cmd);
204 free_pool_memory(ua->args);
207 free_pool_memory(ua->errmsg);
215 free_bsock(ua->UA_sock);
221 * Called from main Bacula thread
223 void term_ua_server()