2 Bacula® - The Network Backup Solution
4 Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from many
7 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 Bacula® is a registered trademark of Kern Sibbald.
18 * Bacula Director -- User Agent Server
20 * Kern Sibbald, September MM
27 /* Imported variables */
30 /* Forward referenced functions */
31 extern "C" void *connect_thread(void *arg);
32 static void *handle_UA_client_request(void *arg);
35 /* Global variables */
36 static int started = FALSE;
37 static workq_t ua_workq;
44 /* Called here by Director daemon to start UA (user agent)
45 * command thread. This routine creates the thread and then
48 void start_UA_server(dlist *addrs)
52 static dlist *myaddrs = addrs;
54 if ((status=pthread_create(&thid, NULL, connect_thread, (void *)myaddrs)) != 0) {
56 Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), be.bstrerror(status));
63 void *connect_thread(void *arg)
65 pthread_detach(pthread_self());
66 set_jcr_in_tsd(INVALID_JCR);
68 /* Permit MaxConsoleConnect console connections */
69 bnet_thread_server((dlist*)arg, director->MaxConsoleConnect, &ua_workq, handle_UA_client_request);
74 * Create a Job Control Record for a control "job",
75 * filling in all the appropriate fields.
77 JCR *new_control_jcr(const char *base_name, int job_type)
80 jcr = new_jcr(sizeof(JCR), dird_free_jcr);
82 * The job and defaults are not really used, but
83 * we set them up to ensure that everything is correctly
87 jcr->job = (JOB *)GetNextRes(R_JOB, NULL);
88 set_jcr_defaults(jcr, jcr->job);
90 jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
91 create_unique_job_name(jcr, base_name);
92 jcr->sched_time = jcr->start_time;
93 jcr->setJobType(job_type);
94 jcr->setJobLevel(L_NONE);
95 jcr->setJobStatus(JS_Running);
101 * Handle Director User Agent commands
104 static void *handle_UA_client_request(void *arg)
109 BSOCK *user = (BSOCK *)arg;
111 pthread_detach(pthread_self());
113 jcr = new_control_jcr("-Console-", JT_CONSOLE);
115 ua = new_ua_context(jcr);
117 set_jcr_in_tsd(INVALID_JCR);
119 user->recv(); /* Get first message */
120 if (!authenticate_user_agent(ua)) {
125 if (ua->api) user->signal(BNET_MAIN_PROMPT);
128 pm_strcpy(ua->cmd, ua->UA_sock->msg);
130 if (ua->argc > 0 && ua->argk[0][0] == '.') {
131 do_a_dot_command(ua);
135 dequeue_messages(ua->jcr);
137 if (console_msg_pending && acl_access_ok(ua, Command_ACL, "messages", 8)) {
138 if (ua->auto_display_messages) {
139 pm_strcpy(ua->cmd, "messages");
140 qmessagescmd(ua, ua->cmd);
141 ua->user_notified_msg_pending = false;
142 } else if (!ua->gui && !ua->user_notified_msg_pending && console_msg_pending) {
144 user->signal(BNET_MSGS_PENDING);
146 bsendmsg(ua, _("You have messages.\n"));
148 ua->user_notified_msg_pending = true;
151 if (!ua->api) user->signal(BNET_EOD); /* send end of command */
153 } else if (user->is_stop()) {
155 } else { /* signal */
156 user->signal(BNET_POLL);
159 /* At the end of each command, revert to the main shared SQL link */
160 ua->db = ua->shared_db;
172 * Create a UAContext for a Job that is running so that
173 * it can the User Agent routines and
174 * to ensure that the Job gets the proper output.
175 * This is a sort of mini-kludge, and should be
176 * unified at some point.
178 UAContext *new_ua_context(JCR *jcr)
182 ua = (UAContext *)malloc(sizeof(UAContext));
183 memset(ua, 0, sizeof(UAContext));
185 ua->shared_db = ua->db = jcr->db;
186 ua->cmd = get_pool_memory(PM_FNAME);
187 ua->args = get_pool_memory(PM_FNAME);
188 ua->errmsg = get_pool_memory(PM_FNAME);
190 ua->automount = true;
194 void free_ua_context(UAContext *ua)
197 free_pool_memory(ua->cmd);
200 free_pool_memory(ua->args);
203 free_pool_memory(ua->errmsg);
211 free_bsock(ua->UA_sock);
217 * Called from main Bacula thread
219 void term_ua_server()