/
ua_server.cc
149 lines (124 loc) · 4.07 KB
/
ua_server.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
BAREOS® - Backup Archiving REcovery Open Sourced
Copyright (C) 2000-2007 Free Software Foundation Europe e.V.
Copyright (C) 2011-2012 Planets Communications B.V.
Copyright (C) 2013-2016 Bareos GmbH & Co. KG
This program is Free Software; you can redistribute it and/or
modify it under the terms of version three of the GNU Affero General Public
License as published by the Free Software Foundation and included
in the file LICENSE.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
/*
* Kern Sibbald, September MM
*/
/**
* @file
* User Agent Server
*/
#include "include/bareos.h"
#include "dird.h"
#include "dird/authenticate.h"
#include "dird/auth_pam.h"
#include "dird/job.h"
#include "dird/ua_cmds.h"
#include "dird/ua_db.h"
#include "dird/ua_input.h"
#include "dird/ua_output.h"
#include "dird/ua_server.h"
#include "lib/bnet.h"
/* Imported variables */
/* Forward referenced functions */
/**
* Create a Job Control Record for a control "job", filling in all the appropriate fields.
*/
JobControlRecord *new_control_jcr(const char *base_name, int job_type)
{
JobControlRecord *jcr;
jcr = new_jcr(sizeof(JobControlRecord), DirdFreeJcr);
/*
* The job and defaults are not really used, but we set them up to ensure that
* everything is correctly initialized.
*/
LockRes();
jcr->res.job = (JobResource *)GetNextRes(R_JOB, NULL);
SetJcrDefaults(jcr, jcr->res.job);
UnlockRes();
jcr->sd_auth_key = bstrdup("dummy"); /* dummy Storage daemon key */
CreateUniqueJobName(jcr, base_name);
jcr->sched_time = jcr->start_time;
jcr->setJobType(job_type);
jcr->setJobLevel(L_NONE);
jcr->setJobStatus(JS_Running);
jcr->JobId = 0;
return jcr;
}
/**
* Handle Director User Agent commands
*/
void *HandleUserAgentClientRequest(BareosSocket *user_agent_socket)
{
int status;
UaContext *ua;
JobControlRecord *jcr;
pthread_detach(pthread_self());
jcr = new_control_jcr("-Console-", JT_CONSOLE);
ua = new_ua_context(jcr);
ua->UA_sock = user_agent_socket;
SetJcrInTsd(INVALID_JCR);
if (!AuthenticateUserAgent(ua)) {
goto getout;
}
if (!PamAuthenticateUseragent(user_agent_socket, ua->cons ? ua->cons->name() : std::string("user"))) {
goto getout;
}
while (!ua->quit) {
if (ua->api) {
user_agent_socket->signal(BNET_MAIN_PROMPT);
}
status = user_agent_socket->recv();
if (status >= 0) {
PmStrcpy(ua->cmd, ua->UA_sock->msg);
ParseUaArgs(ua);
Do_a_command(ua);
DequeueMessages(ua->jcr);
if (!ua->quit) {
if (console_msg_pending && ua->AclAccessOk(Command_ACL, "messages")) {
if (ua->auto_display_messages) {
PmStrcpy(ua->cmd, "messages");
DotMessagesCmd(ua, ua->cmd);
ua->user_notified_msg_pending = false;
} else if (!ua->gui && !ua->user_notified_msg_pending && console_msg_pending) {
if (ua->api) {
user_agent_socket->signal(BNET_MSGS_PENDING);
} else {
bsendmsg(ua, _("You have messages.\n"));
}
ua->user_notified_msg_pending = true;
}
}
if (!ua->api) {
user_agent_socket->signal(BNET_EOD); /* send end of command */
}
}
} else if (IsBnetStop(user_agent_socket)) {
ua->quit = true;
} else { /* signal */
user_agent_socket->signal(BNET_POLL);
}
}
getout:
CloseDb(ua);
FreeUaContext(ua);
FreeJcr(jcr);
user_agent_socket->close();
delete user_agent_socket;
return NULL;
}