]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/dird/ua_server.c
Server address binding + bscan updates -- see kes25Sep02
[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 /*
11    Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
12
13    This program is free software; you can redistribute it and/or
14    modify it under the terms of the GNU General Public License as
15    published by the Free Software Foundation; either version 2 of
16    the License, or (at your option) any later version.
17
18    This program is distributed in the hope that it will be useful,
19    but WITHOUT ANY WARRANTY; without even the implied warranty of
20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21    General Public License for more details.
22
23    You should have received a copy of the GNU General Public
24    License along with this program; if not, write to the Free
25    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
26    MA 02111-1307, USA.
27
28  */
29
30 #include "bacula.h"
31 #include "dird.h"
32 #include "ua.h"
33
34 /* Imported subroutines */
35 extern void run_job(JCR *jcr);
36
37 /* Imported variables */
38 extern int r_first;
39 extern int r_last;
40 extern struct s_res resources[];
41 extern int console_msg_pending;
42 extern char my_name[];
43
44 /* Static variables */
45
46 /* Exported variables */
47 int quit_cmd_thread = 0;
48
49 /* Imported functions */
50
51 /* Forward referenced functions */
52
53 static void *connect_thread(void *arg);
54 static void handle_UA_client_request(void *arg);
55
56
57 /* Global variables */
58 static int started = FALSE;
59 static workq_t ua_workq;
60
61 struct s_addr_port {
62    char *addr;
63    int port;
64 };
65
66 /* Called here by Director daemon to start UA (user agent)
67  * command thread. This routine creates the thread and then
68  * returns.
69  */
70 void start_UA_server(char *UA_addr, int UA_port)
71 {
72    pthread_t thid;
73    int status;
74    static struct s_addr_port arg;
75
76    arg.port = UA_port;
77    arg.addr = UA_addr;
78    if ((status=pthread_create(&thid, NULL, connect_thread, (void *)&arg)) != 0) {
79       Emsg1(M_ABORT, 0, _("Cannot create UA thread: %s\n"), strerror(status));
80    }
81    started = TRUE;
82    return;
83 }
84
85 static void *connect_thread(void *arg)
86 {
87    struct s_addr_port *UA = (struct s_addr_port *)arg;
88
89    pthread_detach(pthread_self());
90
91    /*  ****FIXME**** put # 5 on config parameter */
92    bnet_thread_server(UA->addr, UA->port, 5, &ua_workq, handle_UA_client_request);
93    return NULL;
94 }
95
96 /*
97  * Handle Director User Agent commands   
98  *
99  */
100 static void handle_UA_client_request(void *arg)
101 {
102    int stat;
103    UAContext ua;
104    BSOCK *UA_sock = (BSOCK *) arg;
105
106    pthread_detach(pthread_self());
107
108    memset(&ua, 0, sizeof(ua));
109    ua.automount = TRUE;
110    ua.verbose = 1;
111    ua.jcr = new_jcr(sizeof(JCR), dird_free_jcr);
112    ua.jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
113    ua.UA_sock = UA_sock;
114    ua.cmd = get_pool_memory(PM_FNAME);
115    ua.args = get_pool_memory(PM_FNAME);
116
117    create_unique_job_name(ua.jcr, "*Console*");
118    ua.jcr->sched_time = ua.jcr->start_time;
119    ua.jcr->JobType = JT_CONSOLE;
120
121    bnet_recv(ua.UA_sock);          /* Get first message */
122    if (!authenticate_user_agent(ua.UA_sock)) {
123       goto getout;
124    }
125
126    while (!ua.quit) {
127       stat = bnet_recv(ua.UA_sock);
128       if (stat > 0) {
129          ua.cmd = check_pool_memory_size(ua.cmd, ua.UA_sock->msglen+1);
130          bstrncpy(ua.cmd, ua.UA_sock->msg, ua.UA_sock->msglen+1);
131          parse_command_args(&ua);
132          if (ua.argc > 0 && ua.argk[0][0] == '.') {
133             do_a_dot_command(&ua, ua.cmd);
134          } else {
135             do_a_command(&ua, ua.cmd);
136          }
137          if (!ua.quit) {
138             if (ua.auto_display_messages) {
139                strcpy(ua.cmd, "messages");
140                qmessagescmd(&ua, ua.cmd);
141                ua.user_notified_msg_pending = FALSE;
142             } else if (!ua.user_notified_msg_pending && console_msg_pending) {
143                bsendmsg(&ua, _("You have messages.\n"));
144                ua.user_notified_msg_pending = TRUE;
145             }
146             bnet_sig(ua.UA_sock, BNET_EOD); /* send end of command */
147          }
148       } else if (stat == 0) {
149          if (ua.UA_sock->msglen == BNET_TERMINATE) {
150             ua.quit = TRUE;
151             break;
152          }
153          bnet_sig(ua.UA_sock, BNET_POLL);
154       } else {
155          break;                    /* error, exit */
156       }
157    }
158
159 getout:
160    if (ua.UA_sock) {
161       bnet_close(ua.UA_sock);
162       ua.UA_sock = NULL;
163    }
164
165    close_db(&ua);                     /* do this before freeing JCR */
166
167    if (ua.jcr) {
168       free_jcr(ua.jcr);
169       ua.jcr = NULL;
170    }
171    if (ua.prompt) {
172       free(ua.prompt);
173    }
174    if (ua.cmd) {
175       free_pool_memory(ua.cmd);
176    }
177    if (ua.args) {
178       free_pool_memory(ua.args);
179    }
180    return;
181 }
182
183 /*
184  * Called from main Bacula thread 
185  */
186 void term_ua_server()
187 {
188    if (!started) {
189       return;
190    }
191    quit_cmd_thread = TRUE;
192 }