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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
|
/*
* admin_server_dispatch.c: handlers for admin RPC method calls
*
* Copyright (C) 2014-2016 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include "internal.h"
#include "admin_server_dispatch.h"
#include "admin_server.h"
#include "viralloc.h"
#include "virerror.h"
#include "virlog.h"
#include "rpc/virnetdaemon.h"
#include "rpc/virnetserver.h"
#include "virthreadjob.h"
#include "virtypedparam.h"
#include "virutil.h"
#define VIR_FROM_THIS VIR_FROM_ADMIN
VIR_LOG_INIT("daemon.admin");
typedef struct daemonAdmClientPrivate daemonAdmClientPrivate;
/* Separate private data for admin connection */
struct daemonAdmClientPrivate {
/* Just a placeholder, not that there is anything to be locked */
virMutex lock;
virNetDaemon *dmn;
};
void
remoteAdmClientFree(void *data)
{
struct daemonAdmClientPrivate *priv = data;
virMutexDestroy(&priv->lock);
virObjectUnref(priv->dmn);
g_free(priv);
}
void *
remoteAdmClientNew(virNetServerClient *client G_GNUC_UNUSED,
void *opaque)
{
struct daemonAdmClientPrivate *priv;
uid_t clientuid;
gid_t clientgid;
pid_t clientpid;
unsigned long long timestamp;
if (virNetServerClientGetUNIXIdentity(client,
&clientuid,
&clientgid,
&clientpid,
×tamp) < 0)
return NULL;
VIR_DEBUG("New client pid %lld uid %lld",
(long long)clientpid,
(long long)clientuid);
if (geteuid() != clientuid) {
virReportRestrictedError(_("Disallowing client %1$lld with uid %2$lld"),
(long long)clientpid,
(long long)clientuid);
return NULL;
}
priv = g_new0(struct daemonAdmClientPrivate, 1);
if (virMutexInit(&priv->lock) < 0) {
VIR_FREE(priv);
virReportSystemError(errno, "%s", _("unable to init mutex"));
return NULL;
}
/*
* We don't necessarily need to ref this object right now as there
* must be one ref being held throughout the life of the daemon,
* but let's just be safe for future.
*/
priv->dmn = virObjectRef(opaque);
return priv;
}
void *remoteAdmClientNewPostExecRestart(virNetServerClient *client,
virJSONValue *object G_GNUC_UNUSED,
void *opaque)
{
return remoteAdmClientNew(client, opaque);
}
virJSONValue *remoteAdmClientPreExecRestart(virNetServerClient *client G_GNUC_UNUSED,
void *data G_GNUC_UNUSED)
{
virJSONValue *object = virJSONValueNewObject();
/* No content to add at this time - just need empty object */
return object;
}
/* Helpers */
static virNetServer *
get_nonnull_server(virNetDaemon *dmn, admin_nonnull_server srv)
{
return virNetDaemonGetServer(dmn, srv.name);
}
static void
make_nonnull_server(admin_nonnull_server *srv_dst,
virNetServer *srv_src)
{
srv_dst->name = g_strdup(virNetServerGetName(srv_src));
}
static virNetServerClient *
get_nonnull_client(virNetServer *srv, admin_nonnull_client clnt)
{
return virNetServerGetClient(srv, clnt.id);
}
static void
make_nonnull_client(admin_nonnull_client *clt_dst,
virNetServerClient *clt_src)
{
clt_dst->id = virNetServerClientGetID(clt_src);
clt_dst->timestamp = virNetServerClientGetTimestamp(clt_src);
clt_dst->transport = virNetServerClientGetTransport(clt_src);
}
/* Functions */
static int
adminDispatchConnectOpen(virNetServer *server G_GNUC_UNUSED,
virNetServerClient *client,
virNetMessage *msg G_GNUC_UNUSED,
struct virNetMessageError *rerr,
struct admin_connect_open_args *args)
{
unsigned int flags;
struct daemonAdmClientPrivate *priv =
virNetServerClientGetPrivateData(client);
int ret = -1;
VIR_LOCK_GUARD lock = virLockGuardLock(&priv->lock);
VIR_DEBUG("priv=%p dmn=%p", priv, priv->dmn);
flags = args->flags;
virCheckFlagsGoto(0, cleanup);
ret = 0;
cleanup:
if (ret < 0)
virNetMessageSaveError(rerr);
return ret;
}
static int
adminDispatchConnectClose(virNetServer *server G_GNUC_UNUSED,
virNetServerClient *client,
virNetMessage *msg G_GNUC_UNUSED,
struct virNetMessageError *rerr G_GNUC_UNUSED)
{
virNetServerClientDelayedClose(client);
return 0;
}
static int
adminConnectGetLibVersion(virNetDaemon *dmn G_GNUC_UNUSED,
unsigned long long *libVer)
{
if (libVer)
*libVer = LIBVIR_VERSION_NUMBER;
return 0;
}
static virNetDaemon *
adminGetConn(virNetServerClient *client)
{
struct daemonAdmClientPrivate *priv =
virNetServerClientGetPrivateData(client);
return priv->dmn;
}
static int
adminDispatchServerGetThreadpoolParameters(virNetServer *server G_GNUC_UNUSED,
virNetServerClient *client,
virNetMessage *msg G_GNUC_UNUSED,
struct virNetMessageError *rerr,
struct admin_server_get_threadpool_parameters_args *args,
struct admin_server_get_threadpool_parameters_ret *ret)
{
int rv = -1;
virNetServer *srv = NULL;
virTypedParameterPtr params = NULL;
int nparams = 0;
struct daemonAdmClientPrivate *priv =
virNetServerClientGetPrivateData(client);
if (!(srv = virNetDaemonGetServer(priv->dmn, args->srv.name)))
goto cleanup;
if (adminServerGetThreadPoolParameters(srv, ¶ms, &nparams,
args->flags) < 0)
goto cleanup;
if (virTypedParamsSerialize(params, nparams,
ADMIN_SERVER_THREADPOOL_PARAMETERS_MAX,
(struct _virTypedParameterRemote **) &ret->params.params_val,
&ret->params.params_len, 0) < 0)
goto cleanup;
rv = 0;
cleanup:
if (rv < 0)
virNetMessageSaveError(rerr);
virTypedParamsFree(params, nparams);
virObjectUnref(srv);
return rv;
}
static int
adminDispatchServerSetThreadpoolParameters(virNetServer *server G_GNUC_UNUSED,
virNetServerClient *client,
virNetMessage *msg G_GNUC_UNUSED,
struct virNetMessageError *rerr,
struct admin_server_set_threadpool_parameters_args *args)
{
int rv = -1;
virNetServer *srv = NULL;
virTypedParameterPtr params = NULL;
int nparams = 0;
struct daemonAdmClientPrivate *priv =
virNetServerClientGetPrivateData(client);
if (!(srv = virNetDaemonGetServer(priv->dmn, args->srv.name))) {
virReportError(VIR_ERR_NO_SERVER,
_("no server with matching name '%1$s' found"),
args->srv.name);
goto cleanup;
}
if (virTypedParamsDeserialize((struct _virTypedParameterRemote *) args->params.params_val,
args->params.params_len,
ADMIN_SERVER_THREADPOOL_PARAMETERS_MAX,
¶ms,
&nparams) < 0)
goto cleanup;
if (adminServerSetThreadPoolParameters(srv, params,
nparams, args->flags) < 0)
goto cleanup;
rv = 0;
cleanup:
if (rv < 0)
virNetMessageSaveError(rerr);
virTypedParamsFree(params, nparams);
virObjectUnref(srv);
return rv;
}
static int
adminDispatchClientGetInfo(virNetServer *server G_GNUC_UNUSED,
virNetServerClient *client,
virNetMessage *msg G_GNUC_UNUSED,
struct virNetMessageError *rerr,
struct admin_client_get_info_args *args,
struct admin_client_get_info_ret *ret)
{
int rv = -1;
virNetServer *srv = NULL;
virNetServerClient *clnt = NULL;
virTypedParameterPtr params = NULL;
int nparams = 0;
struct daemonAdmClientPrivate *priv =
virNetServerClientGetPrivateData(client);
if (!(srv = virNetDaemonGetServer(priv->dmn, args->clnt.srv.name))) {
virReportError(VIR_ERR_NO_SERVER,
_("no server with matching name '%1$s' found"),
args->clnt.srv.name);
goto cleanup;
}
if (!(clnt = virNetServerGetClient(srv, args->clnt.id))) {
virReportError(VIR_ERR_NO_CLIENT,
_("no client with matching id '%1$llu' found"),
(unsigned long long) args->clnt.id);
goto cleanup;
}
if (adminClientGetInfo(clnt, ¶ms, &nparams, args->flags) < 0)
goto cleanup;
if (virTypedParamsSerialize(params, nparams,
ADMIN_CLIENT_INFO_PARAMETERS_MAX,
(struct _virTypedParameterRemote **) &ret->params.params_val,
&ret->params.params_len,
VIR_TYPED_PARAM_STRING_OKAY) < 0)
goto cleanup;
rv = 0;
cleanup:
if (rv < 0)
virNetMessageSaveError(rerr);
virTypedParamsFree(params, nparams);
virObjectUnref(clnt);
virObjectUnref(srv);
return rv;
}
static int
adminDispatchServerGetClientLimits(virNetServer *server G_GNUC_UNUSED,
virNetServerClient *client,
virNetMessage *msg G_GNUC_UNUSED,
struct virNetMessageError *rerr G_GNUC_UNUSED,
admin_server_get_client_limits_args *args,
admin_server_get_client_limits_ret *ret)
{
int rv = -1;
virNetServer *srv = NULL;
virTypedParameterPtr params = NULL;
int nparams = 0;
struct daemonAdmClientPrivate *priv =
virNetServerClientGetPrivateData(client);
if (!(srv = virNetDaemonGetServer(priv->dmn, args->srv.name)))
goto cleanup;
if (adminServerGetClientLimits(srv, ¶ms, &nparams, args->flags) < 0)
goto cleanup;
if (virTypedParamsSerialize(params, nparams,
ADMIN_SERVER_CLIENT_LIMITS_MAX,
(struct _virTypedParameterRemote **) &ret->params.params_val,
&ret->params.params_len, 0) < 0)
goto cleanup;
rv = 0;
cleanup:
if (rv < 0)
virNetMessageSaveError(rerr);
virTypedParamsFree(params, nparams);
virObjectUnref(srv);
return rv;
}
static int
adminDispatchServerSetClientLimits(virNetServer *server G_GNUC_UNUSED,
virNetServerClient *client,
virNetMessage *msg G_GNUC_UNUSED,
struct virNetMessageError *rerr G_GNUC_UNUSED,
admin_server_set_client_limits_args *args)
{
int rv = -1;
virNetServer *srv = NULL;
virTypedParameterPtr params = NULL;
int nparams = 0;
struct daemonAdmClientPrivate *priv =
virNetServerClientGetPrivateData(client);
if (!(srv = virNetDaemonGetServer(priv->dmn, args->srv.name))) {
virReportError(VIR_ERR_NO_SERVER,
_("no server with matching name '%1$s' found"),
args->srv.name);
goto cleanup;
}
if (virTypedParamsDeserialize((struct _virTypedParameterRemote *) args->params.params_val,
args->params.params_len,
ADMIN_SERVER_CLIENT_LIMITS_MAX, ¶ms, &nparams) < 0)
goto cleanup;
if (adminServerSetClientLimits(srv, params, nparams, args->flags) < 0)
goto cleanup;
rv = 0;
cleanup:
if (rv < 0)
virNetMessageSaveError(rerr);
virTypedParamsFree(params, nparams);
virObjectUnref(srv);
return rv;
}
/* Returns the number of outputs stored in @outputs */
static int
adminConnectGetLoggingOutputs(char **outputs, unsigned int flags)
{
char *tmp = NULL;
virCheckFlags(0, -1);
if (!(tmp = virLogGetOutputs()))
return -1;
*outputs = tmp;
return virLogGetNbOutputs();
}
/* Returns the number of defined filters or -1 in case of an error */
static int
adminConnectGetLoggingFilters(char **filters, unsigned int flags)
{
char *tmp = NULL;
int ret = 0;
virCheckFlags(0, -1);
if ((ret = virLogGetNbFilters()) > 0 && !(tmp = virLogGetFilters()))
return -1;
*filters = tmp;
return ret;
}
static int
adminConnectSetLoggingOutputs(virNetDaemon *dmn G_GNUC_UNUSED,
const char *outputs,
unsigned int flags)
{
virCheckFlags(0, -1);
return virLogSetOutputs(outputs);
}
static int
adminConnectSetLoggingFilters(virNetDaemon *dmn G_GNUC_UNUSED,
const char *filters,
unsigned int flags)
{
virCheckFlags(0, -1);
return virLogSetFilters(filters);
}
static int
adminConnectSetDaemonTimeout(virNetDaemon *dmn,
unsigned int timeout,
unsigned int flags)
{
virCheckFlags(0, -1);
return virNetDaemonAutoShutdown(dmn, timeout);
}
static int
adminDispatchConnectGetLoggingOutputs(virNetServer *server G_GNUC_UNUSED,
virNetServerClient *client G_GNUC_UNUSED,
virNetMessage *msg G_GNUC_UNUSED,
struct virNetMessageError *rerr,
admin_connect_get_logging_outputs_args *args,
admin_connect_get_logging_outputs_ret *ret)
{
char *outputs = NULL;
int noutputs = 0;
if ((noutputs = adminConnectGetLoggingOutputs(&outputs, args->flags)) < 0) {
virNetMessageSaveError(rerr);
return -1;
}
ret->outputs = g_steal_pointer(&outputs);
ret->noutputs = noutputs;
return 0;
}
static int
adminDispatchConnectGetLoggingFilters(virNetServer *server G_GNUC_UNUSED,
virNetServerClient *client G_GNUC_UNUSED,
virNetMessage *msg G_GNUC_UNUSED,
struct virNetMessageError *rerr,
admin_connect_get_logging_filters_args *args,
admin_connect_get_logging_filters_ret *ret)
{
char *filters = NULL;
int nfilters = 0;
if ((nfilters = adminConnectGetLoggingFilters(&filters, args->flags)) < 0) {
virNetMessageSaveError(rerr);
return -1;
}
if (nfilters == 0) {
ret->filters = NULL;
} else {
char **ret_filters = NULL;
ret_filters = g_new0(char *, 1);
*ret_filters = filters;
ret->filters = ret_filters;
}
ret->nfilters = nfilters;
return 0;
}
#include "admin_server_dispatch_stubs.h"
|