]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_server.c
Fix line I accidently truncated
[bacula/bacula] / bacula / src / dird / ua_server.c
1 /*
2  *
3  *   Bacula Director -- User Agent Server
4  *
5  *     Kern Sibbald, September MM
6  *
7  *    Version $Id$
8  */
9 /*
10    Bacula® - The Network Backup Solution
11
12    Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
13
14    The main author of Bacula is Kern Sibbald, with contributions from
15    many others, a complete list can be found in the file AUTHORS.
16    This program is Free Software; you can redistribute it and/or
17    modify it under the terms of version two of the GNU General Public
18    License as published by the Free Software Foundation plus additions
19    that are listed in the file LICENSE.
20
21    This program is distributed in the hope that it will be useful, but
22    WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24    General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program; if not, write to the Free Software
28    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
29    02110-1301, USA.
30
31    Bacula® is a registered trademark of John Walker.
32    The licensor of Bacula is the Free Software Foundation Europe
33    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
34    Switzerland, email:ftf@fsfeurope.org.
35 */
36
37 #include "bacula.h"
38 #include "dird.h"
39
40 /* Imported variables */
41 extern int r_first;
42 extern int r_last;
43 extern struct s_res resources[];
44
45
46 /* Forward referenced functions */
47 extern "C" void *connect_thread(void *arg);
48 static void *handle_UA_client_request(void *arg);
49
50
51 /* Global variables */
52 static int started = FALSE;
53 static workq_t ua_workq;
54
55 struct s_addr_port {
56    char *addr;
57    char *port;
58 };
59
60 /* Called here by Director daemon to start UA (user agent)
61  * command thread. This routine creates the thread and then
62  * returns.
63  */
64 void start_UA_server(dlist *addrs)
65 {
66    pthread_t thid;
67    int status;
68    static dlist *myaddrs = addrs;
69
70    if ((status=pthread_create(&thid, NULL, connect_thread, (void *)myaddrs)) != 0) {
71       berrno be;
72       Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), be.strerror(status));
73    }
74    started = TRUE;
75    return;
76 }
77
78 extern "C"
79 void *connect_thread(void *arg)
80 {
81    pthread_detach(pthread_self());
82
83    /* Permit 10 console connections */
84    bnet_thread_server((dlist*)arg, 10, &ua_workq, handle_UA_client_request);
85    return NULL;
86 }
87
88 /*
89  * Create a Job Control Record for a control "job",
90  *   filling in all the appropriate fields.
91  */
92 JCR *new_control_jcr(const char *base_name, int job_type)
93 {
94    JCR *jcr;
95    jcr = new_jcr(sizeof(JCR), dird_free_jcr);
96    /*
97     * The job and defaults are not really used, but
98     *  we set them up to ensure that everything is correctly
99     *  initialized.
100     */
101    LockRes();
102    jcr->job = (JOB *)GetNextRes(R_JOB, NULL);
103    set_jcr_defaults(jcr, jcr->job);
104    UnlockRes();
105    jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
106    create_unique_job_name(jcr, base_name);
107    jcr->sched_time = jcr->start_time;
108    jcr->JobType = job_type;
109    jcr->JobLevel = L_NONE;
110    set_jcr_job_status(jcr, JS_Running);
111    jcr->JobId = 0;
112    return jcr;
113 }
114
115 /*
116  * Handle Director User Agent commands
117  *
118  */
119 static void *handle_UA_client_request(void *arg)
120 {
121    int stat;
122    UAContext *ua;
123    JCR *jcr;
124
125    pthread_detach(pthread_self());
126
127    jcr = new_control_jcr("*Console*", JT_CONSOLE);
128
129    ua = new_ua_context(jcr);
130    ua->UA_sock = (BSOCK *)arg;
131
132    bnet_recv(ua->UA_sock);          /* Get first message */
133    if (!authenticate_user_agent(ua)) {
134       goto getout;
135    }
136
137    while (!ua->quit) {
138       stat = bnet_recv(ua->UA_sock);
139       if (stat >= 0) {
140          pm_strcpy(ua->cmd, ua->UA_sock->msg);
141          parse_ua_args(ua);
142          if (ua->argc > 0 && ua->argk[0][0] == '.') {
143             do_a_dot_command(ua, ua->cmd);
144          } else {
145             do_a_command(ua, ua->cmd);
146          }
147          if (!ua->quit) {
148             if (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                   bsendmsg(ua, _("You have messages.\n"));
155                   ua->user_notified_msg_pending = TRUE;
156                }
157             }
158             bnet_sig(ua->UA_sock, BNET_EOD); /* send end of command */
159          }
160       } else if (is_bnet_stop(ua->UA_sock)) {
161          ua->quit = true;
162       } else { /* signal */
163          bnet_sig(ua->UA_sock, BNET_POLL);
164       }
165    }
166
167 getout:
168    close_db(ua);
169    free_ua_context(ua);
170    free_jcr(jcr);
171
172    return NULL;
173 }
174
175 /*
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.
181  */
182 UAContext *new_ua_context(JCR *jcr)
183 {
184    UAContext *ua;
185
186    ua = (UAContext *)malloc(sizeof(UAContext));
187    memset(ua, 0, sizeof(UAContext));
188    ua->jcr = jcr;
189    ua->db = jcr->db;
190    ua->cmd = get_pool_memory(PM_FNAME);
191    ua->args = get_pool_memory(PM_FNAME);
192    ua->verbose = true;
193    ua->automount = true;
194    return ua;
195 }
196
197 void free_ua_context(UAContext *ua)
198 {
199    if (ua->cmd) {
200       free_pool_memory(ua->cmd);
201    }
202    if (ua->args) {
203       free_pool_memory(ua->args);
204    }
205    if (ua->prompt) {
206       free(ua->prompt);
207    }
208
209    if (ua->UA_sock) {
210       bnet_close(ua->UA_sock);
211       ua->UA_sock = NULL;
212    }
213    free(ua);
214 }
215
216
217 /*
218  * Called from main Bacula thread
219  */
220 void term_ua_server()
221 {
222    if (!started) {
223       return;
224    }
225 }