]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_server.c
kes Change Bacula trademark owner from John Walker to Kern Sibbald
[bacula/bacula] / bacula / src / dird / ua_server.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
5
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 two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
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.
17
18    You should have received a copy of the GNU 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
21    02110-1301, USA.
22
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.
27 */
28 /*
29  *
30  *   Bacula Director -- User Agent Server
31  *
32  *     Kern Sibbald, September MM
33  *
34  *    Version $Id$
35  */
36
37 #include "bacula.h"
38 #include "dird.h"
39
40 /* Imported variables */
41
42
43 /* Forward referenced functions */
44 extern "C" void *connect_thread(void *arg);
45 static void *handle_UA_client_request(void *arg);
46
47
48 /* Global variables */
49 static int started = FALSE;
50 static workq_t ua_workq;
51
52 struct s_addr_port {
53    char *addr;
54    char *port;
55 };
56
57 /* Called here by Director daemon to start UA (user agent)
58  * command thread. This routine creates the thread and then
59  * returns.
60  */
61 void start_UA_server(dlist *addrs)
62 {
63    pthread_t thid;
64    int status;
65    static dlist *myaddrs = addrs;
66
67    if ((status=pthread_create(&thid, NULL, connect_thread, (void *)myaddrs)) != 0) {
68       berrno be;
69       Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), be.bstrerror(status));
70    }
71    started = TRUE;
72    return;
73 }
74
75 extern "C"
76 void *connect_thread(void *arg)
77 {
78    pthread_detach(pthread_self());
79
80    /* Permit 20 console connections */
81    bnet_thread_server((dlist*)arg, 20, &ua_workq, handle_UA_client_request);
82    return NULL;
83 }
84
85 /*
86  * Create a Job Control Record for a control "job",
87  *   filling in all the appropriate fields.
88  */
89 JCR *new_control_jcr(const char *base_name, int job_type)
90 {
91    JCR *jcr;
92    jcr = new_jcr(sizeof(JCR), dird_free_jcr);
93    /*
94     * The job and defaults are not really used, but
95     *  we set them up to ensure that everything is correctly
96     *  initialized.
97     */
98    LockRes();
99    jcr->job = (JOB *)GetNextRes(R_JOB, NULL);
100    set_jcr_defaults(jcr, jcr->job);
101    UnlockRes();
102    jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
103    create_unique_job_name(jcr, base_name);
104    jcr->sched_time = jcr->start_time;
105    jcr->JobType = job_type;
106    jcr->JobLevel = L_NONE;
107    set_jcr_job_status(jcr, JS_Running);
108    jcr->JobId = 0;
109    return jcr;
110 }
111
112 /*
113  * Handle Director User Agent commands
114  *
115  */
116 static void *handle_UA_client_request(void *arg)
117 {
118    int stat;
119    UAContext *ua;
120    JCR *jcr;
121    BSOCK *user = (BSOCK *)arg;
122
123    pthread_detach(pthread_self());
124
125    jcr = new_control_jcr("-Console-", JT_CONSOLE);
126
127    ua = new_ua_context(jcr);
128    ua->UA_sock = user;
129
130    user->recv();             /* Get first message */
131    if (!authenticate_user_agent(ua)) {
132       goto getout;
133    }
134
135    while (!ua->quit) {
136       if (ua->api) user->signal(BNET_MAIN_PROMPT);
137       stat = user->recv();
138       if (stat >= 0) {
139          pm_strcpy(ua->cmd, ua->UA_sock->msg);
140          parse_ua_args(ua);
141          if (ua->argc > 0 && ua->argk[0][0] == '.') {
142             do_a_dot_command(ua);
143          } else {
144             do_a_command(ua);
145          }
146          dequeue_messages(ua->jcr);
147          if (!ua->quit) {
148             if (console_msg_pending && acl_access_ok(ua, Command_ACL, "messages", 8)) {
149                if (ua->auto_display_messages) {
150                   pm_strcpy(ua->cmd, "messages");
151                   qmessagescmd(ua, ua->cmd);
152                   ua->user_notified_msg_pending = false;
153                } else if (!ua->gui && !ua->user_notified_msg_pending && console_msg_pending) {
154                   if (ua->api) {
155                      user->signal(BNET_MSGS_PENDING);
156                   } else {
157                      bsendmsg(ua, _("You have messages.\n"));
158                   }
159                   ua->user_notified_msg_pending = true;
160                }
161             }
162             if (!ua->api) user->signal(BNET_EOD);     /* send end of command */
163          }
164       } else if (is_bnet_stop(user)) {
165          ua->quit = true;
166       } else { /* signal */
167          user->signal(BNET_POLL);
168       }
169    }
170
171 getout:
172    close_db(ua);
173    free_ua_context(ua);
174    free_jcr(jcr);
175
176    return NULL;
177 }
178
179 /*
180  * Create a UAContext for a Job that is running so that
181  *   it can the User Agent routines and
182  *   to ensure that the Job gets the proper output.
183  *   This is a sort of mini-kludge, and should be
184  *   unified at some point.
185  */
186 UAContext *new_ua_context(JCR *jcr)
187 {
188    UAContext *ua;
189
190    ua = (UAContext *)malloc(sizeof(UAContext));
191    memset(ua, 0, sizeof(UAContext));
192    ua->jcr = jcr;
193    ua->db = jcr->db;
194    ua->cmd = get_pool_memory(PM_FNAME);
195    ua->args = get_pool_memory(PM_FNAME);
196    ua->verbose = true;
197    ua->automount = true;
198    return ua;
199 }
200
201 void free_ua_context(UAContext *ua)
202 {
203    if (ua->cmd) {
204       free_pool_memory(ua->cmd);
205    }
206    if (ua->args) {
207       free_pool_memory(ua->args);
208    }
209    if (ua->prompt) {
210       free(ua->prompt);
211    }
212
213    if (ua->UA_sock) {
214       bnet_close(ua->UA_sock);
215       ua->UA_sock = NULL;
216    }
217    free(ua);
218 }
219
220
221 /*
222  * Called from main Bacula thread
223  */
224 void term_ua_server()
225 {
226    if (!started) {
227       return;
228    }
229 }