2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
21 * Bacula Director -- User Agent Server
23 * Kern Sibbald, September MM
30 /* Imported variables */
33 /* Forward referenced functions */
34 extern "C" void *connect_thread(void *arg);
35 static void *handle_UA_client_request(void *arg);
38 /* Global variables */
39 static int started = FALSE;
40 static workq_t ua_workq;
47 /* Called here by Director daemon to start UA (user agent)
48 * command thread. This routine creates the thread and then
51 void start_UA_server(dlist *addrs)
55 static dlist *myaddrs = addrs;
57 if ((status=pthread_create(&thid, NULL, connect_thread, (void *)myaddrs)) != 0) {
59 Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), be.bstrerror(status));
66 void *connect_thread(void *arg)
68 pthread_detach(pthread_self());
69 set_jcr_in_tsd(INVALID_JCR);
71 /* Permit MaxConsoleConnect console connections */
72 bnet_thread_server((dlist*)arg, director->MaxConsoleConnect, &ua_workq, handle_UA_client_request);
77 * Create a Job Control Record for a control "job",
78 * filling in all the appropriate fields.
80 JCR *new_control_jcr(const char *base_name, int job_type)
83 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
85 * The job and defaults are not really used, but
86 * we set them up to ensure that everything is correctly
90 jcr->job = (JOB *)GetNextRes(R_JOB, NULL);
91 set_jcr_defaults(jcr, jcr->job);
92 /* We use a resource, so we should count in the reload */
93 jcr->setJobType(job_type);
96 jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
97 create_unique_job_name(jcr, base_name);
98 jcr->sched_time = jcr->start_time;
99 jcr->setJobLevel(L_NONE);
100 jcr->setJobStatus(JS_Running);
106 * Handle Director User Agent commands
109 static void *handle_UA_client_request(void *arg)
114 BSOCK *user = (BSOCK *)arg;
116 pthread_detach(pthread_self());
118 jcr = new_control_jcr("-Console-", JT_CONSOLE);
120 ua = new_ua_context(jcr);
122 set_jcr_in_tsd(INVALID_JCR);
124 user->recv(); /* Get first message */
125 if (!authenticate_user_agent(ua)) {
130 if (ua->api) user->signal(BNET_MAIN_PROMPT);
133 pm_strcpy(ua->cmd, ua->UA_sock->msg);
135 if (ua->argc > 0 && ua->argk[0][0] == '.') {
136 do_a_dot_command(ua);
140 dequeue_messages(ua->jcr);
142 if (console_msg_pending && acl_access_ok(ua, Command_ACL, "messages", 8)) {
143 if (ua->auto_display_messages) {
144 pm_strcpy(ua->cmd, "messages");
145 qmessagescmd(ua, ua->cmd);
146 ua->user_notified_msg_pending = false;
147 } else if (!ua->gui && !ua->user_notified_msg_pending && console_msg_pending) {
149 user->signal(BNET_MSGS_PENDING);
151 bsendmsg(ua, _("You have messages.\n"));
153 ua->user_notified_msg_pending = true;
156 if (!ua->api) user->signal(BNET_EOD); /* send end of command */
158 } else if (user->is_stop()) {
160 } else { /* signal */
161 user->signal(BNET_POLL);
164 /* At the end of each command, revert to the main shared SQL link */
165 ua->db = ua->shared_db;
177 * Create a UAContext for a Job that is running so that
178 * it can the User Agent routines and
179 * to ensure that the Job gets the proper output.
180 * This is a sort of mini-kludge, and should be
181 * unified at some point.
183 UAContext *new_ua_context(JCR *jcr)
187 ua = (UAContext *)malloc(sizeof(UAContext));
188 memset(ua, 0, sizeof(UAContext));
190 ua->shared_db = ua->db = jcr->db;
191 ua->cmd = get_pool_memory(PM_FNAME);
192 ua->args = get_pool_memory(PM_FNAME);
193 ua->errmsg = get_pool_memory(PM_FNAME);
195 ua->automount = true;
199 void free_ua_context(UAContext *ua)
202 free_pool_memory(ua->cmd);
205 free_pool_memory(ua->args);
208 free_pool_memory(ua->errmsg);
216 free_bsock(ua->UA_sock);
222 * Called from main Bacula thread
224 void term_ua_server()