remote_daemon_config.c 12.1 KB
Newer Older
1
/*
2
 * remote_daemon_config.c: libvirtd config file handling
3
 *
4
 * Copyright (C) 2006-2018 Red Hat, Inc.
5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright (C) 2006 Daniel P. Berrange
 *
 * 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
18
 * License along with this library.  If not, see
O
Osier Yang 已提交
19
 * <http://www.gnu.org/licenses/>.
20 21 22 23
 */

#include <config.h>

24
#include "remote_daemon_config.h"
25
#include "virconf.h"
26
#include "viralloc.h"
27
#include "virerror.h"
28
#include "virlog.h"
29
#include "rpc/virnetserver.h"
30
#include "configmake.h"
31 32
#include "remote_protocol.h"
#include "remote_driver.h"
33
#include "util/virnetdevopenvswitch.h"
34 35
#include "virstring.h"
#include "virutil.h"
36 37 38

#define VIR_FROM_THIS VIR_FROM_CONF

39 40
VIR_LOG_INIT("daemon.libvirtd-config");

41

42 43
static int
remoteConfigGetAuth(virConfPtr conf,
44
                    const char *filename,
45
                    const char *key,
46
                    int *auth)
47
{
48
    char *authstr = NULL;
49

50
    if (virConfGetValueString(conf, key, &authstr) < 0)
51 52
        return -1;

53
    if (!authstr)
54 55
        return 0;

56
    if (STREQ(authstr, "none")) {
57
        *auth = VIR_NET_SERVER_SERVICE_AUTH_NONE;
58
#if WITH_SASL
59
    } else if (STREQ(authstr, "sasl")) {
60 61
        *auth = VIR_NET_SERVER_SERVICE_AUTH_SASL;
#endif
62
    } else if (STREQ(authstr, "polkit")) {
63 64
        *auth = VIR_NET_SERVER_SERVICE_AUTH_POLKIT;
    } else {
65
        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
66 67 68
                       _("%s: %s: unsupported auth %s"),
                       filename, key, authstr);
        VIR_FREE(authstr);
69 70 71
        return -1;
    }

72
    VIR_FREE(authstr);
73 74 75 76 77 78 79
    return 0;
}

int
daemonConfigFilePath(bool privileged, char **configfile)
{
    if (privileged) {
80
        *configfile = g_strdup(SYSCONFDIR "/libvirt/" DAEMON_NAME ".conf");
81
    } else {
82
        char *configdir = NULL;
83

84
        if (!(configdir = virGetUserConfigDirectory()))
85 86
            goto error;

87
        *configfile = g_strdup_printf("%s/%s.conf", configdir, DAEMON_NAME);
88
        VIR_FREE(configdir);
89 90 91 92
    }

    return 0;

93
 error:
94 95 96 97
    return -1;
}

struct daemonConfig*
J
Ján Tomko 已提交
98
daemonConfigNew(bool privileged G_GNUC_UNUSED)
99 100 101
{
    struct daemonConfig *data;

102
    if (VIR_ALLOC(data) < 0)
103 104
        return NULL;

105
#ifdef WITH_IP
106 107 108 109 110
# ifdef LIBVIRTD
    data->listen_tls = 1; /* Only honoured if --listen is set */
# else /* ! LIBVIRTD */
    data->listen_tls = 0; /* Always honoured, --listen doesn't exist. */
# endif /* ! LIBVIRTD */
111 112
    data->listen_tcp = 0;

113 114
    data->tls_port = g_strdup(LIBVIRTD_TLS_PORT);
    data->tcp_port = g_strdup(LIBVIRTD_TCP_PORT);
115
#endif /* !WITH_IP */
116 117

    /* Only default to PolicyKit if running as root */
118
#if WITH_POLKIT
119 120 121 122 123 124 125
    if (privileged) {
        data->auth_unix_rw = REMOTE_AUTH_POLKIT;
        data->auth_unix_ro = REMOTE_AUTH_POLKIT;
    } else {
#endif
        data->auth_unix_rw = REMOTE_AUTH_NONE;
        data->auth_unix_ro = REMOTE_AUTH_NONE;
126
#if WITH_POLKIT
127 128 129
    }
#endif

130 131 132
    data->unix_sock_rw_perms = g_strdup(data->auth_unix_rw == REMOTE_AUTH_POLKIT ? "0777" : "0700");
    data->unix_sock_ro_perms = g_strdup("0777");
    data->unix_sock_admin_perms = g_strdup("0700");
133

134 135
#ifdef WITH_IP
# if WITH_SASL
136
    data->auth_tcp = REMOTE_AUTH_SASL;
137
# else
138
    data->auth_tcp = REMOTE_AUTH_NONE;
139
# endif
140
    data->auth_tls = REMOTE_AUTH_NONE;
141
#endif /* ! WITH_IP */
142 143 144

    data->min_workers = 5;
    data->max_workers = 20;
145
    data->max_clients = 5000;
146
    data->max_queued_clients = 1000;
147
    data->max_anonymous_clients = 20;
148 149 150 151 152 153 154 155 156 157 158

    data->prio_workers = 5;

    data->max_client_requests = 5;

    data->audit_level = 1;
    data->audit_logging = 0;

    data->keepalive_interval = 5;
    data->keepalive_count = 5;

159 160 161 162 163 164 165 166 167
    data->admin_min_workers = 5;
    data->admin_max_workers = 20;
    data->admin_max_clients = 5000;
    data->admin_max_queued_clients = 20;
    data->admin_max_client_requests = 5;

    data->admin_keepalive_interval = 5;
    data->admin_keepalive_count = 5;

168 169
    data->ovs_timeout = VIR_NETDEV_OVS_DEFAULT_TIMEOUT;

170 171 172 173 174 175 176 177 178 179 180
    return data;
}

void
daemonConfigFree(struct daemonConfig *data)
{
    char **tmp;

    if (!data)
        return;

181
#ifdef WITH_IP
182 183 184
    VIR_FREE(data->listen_addr);
    VIR_FREE(data->tls_port);
    VIR_FREE(data->tcp_port);
185 186
#endif /* ! WITH_IP */

187 188 189 190 191 192
    tmp = data->access_drivers;
    while (tmp && *tmp) {
        VIR_FREE(*tmp);
        tmp++;
    }
    VIR_FREE(data->access_drivers);
193

194
    VIR_FREE(data->unix_sock_admin_perms);
195 196 197 198 199
    VIR_FREE(data->unix_sock_ro_perms);
    VIR_FREE(data->unix_sock_rw_perms);
    VIR_FREE(data->unix_sock_group);
    VIR_FREE(data->unix_sock_dir);

200
    tmp = data->sasl_allowed_username_list;
201 202 203 204
    while (tmp && *tmp) {
        VIR_FREE(*tmp);
        tmp++;
    }
205
    VIR_FREE(data->sasl_allowed_username_list);
206

207 208
#ifdef WITH_IP
    tmp = data->tls_allowed_dn_list;
209 210 211 212
    while (tmp && *tmp) {
        VIR_FREE(*tmp);
        tmp++;
    }
213 214
    VIR_FREE(data->tls_allowed_dn_list);

215
    VIR_FREE(data->tls_priority);
216 217 218 219 220

    VIR_FREE(data->key_file);
    VIR_FREE(data->ca_file);
    VIR_FREE(data->cert_file);
    VIR_FREE(data->crl_file);
221
#endif /* ! WITH_IP */
222

A
Alex Jia 已提交
223
    VIR_FREE(data->host_uuid);
224
    VIR_FREE(data->host_uuid_source);
225 226 227 228 229 230
    VIR_FREE(data->log_filters);
    VIR_FREE(data->log_outputs);

    VIR_FREE(data);
}

231 232 233 234
static int
daemonConfigLoadOptions(struct daemonConfig *data,
                        const char *filename,
                        virConfPtr conf)
235
{
236
#ifdef WITH_IP
237 238 239 240 241 242 243 244 245 246
    if (virConfGetValueBool(conf, "listen_tcp", &data->listen_tcp) < 0)
        goto error;
    if (virConfGetValueBool(conf, "listen_tls", &data->listen_tls) < 0)
        goto error;
    if (virConfGetValueString(conf, "tls_port", &data->tls_port) < 0)
        goto error;
    if (virConfGetValueString(conf, "tcp_port", &data->tcp_port) < 0)
        goto error;
    if (virConfGetValueString(conf, "listen_addr", &data->listen_addr) < 0)
        goto error;
247
#endif /* !WITH_IP */
248

249
    if (remoteConfigGetAuth(conf, filename, "auth_unix_rw", &data->auth_unix_rw) < 0)
250
        goto error;
251
#if WITH_POLKIT
252 253 254 255 256
    /* Change default perms to be wide-open if PolicyKit is enabled.
     * Admin can always override in config file
     */
    if (data->auth_unix_rw == REMOTE_AUTH_POLKIT) {
        VIR_FREE(data->unix_sock_rw_perms);
257
        data->unix_sock_rw_perms = g_strdup("0777");
258 259
    }
#endif
260
    if (remoteConfigGetAuth(conf, filename, "auth_unix_ro", &data->auth_unix_ro) < 0)
261
        goto error;
262 263

#ifdef WITH_IP
264
    if (remoteConfigGetAuth(conf, filename, "auth_tcp", &data->auth_tcp) < 0)
265
        goto error;
266
    if (remoteConfigGetAuth(conf, filename, "auth_tls", &data->auth_tls) < 0)
267
        goto error;
268
#endif /* ! WITH_IP */
269

270 271
    if (virConfGetValueStringList(conf, "access_drivers", false,
                                  &data->access_drivers) < 0)
272 273
        goto error;

274 275 276 277 278 279 280 281
    if (virConfGetValueString(conf, "unix_sock_group", &data->unix_sock_group) < 0)
        goto error;
    if (virConfGetValueString(conf, "unix_sock_admin_perms", &data->unix_sock_admin_perms) < 0)
        goto error;
    if (virConfGetValueString(conf, "unix_sock_ro_perms", &data->unix_sock_ro_perms) < 0)
        goto error;
    if (virConfGetValueString(conf, "unix_sock_rw_perms", &data->unix_sock_rw_perms) < 0)
        goto error;
282

283 284
    if (virConfGetValueString(conf, "unix_sock_dir", &data->unix_sock_dir) < 0)
        goto error;
285

286
#ifdef WITH_IP
287 288 289 290
    if (virConfGetValueBool(conf, "tls_no_sanity_certificate", &data->tls_no_sanity_certificate) < 0)
        goto error;
    if (virConfGetValueBool(conf, "tls_no_verify_certificate", &data->tls_no_verify_certificate) < 0)
        goto error;
291

292 293 294 295 296 297 298 299
    if (virConfGetValueString(conf, "key_file", &data->key_file) < 0)
        goto error;
    if (virConfGetValueString(conf, "cert_file", &data->cert_file) < 0)
        goto error;
    if (virConfGetValueString(conf, "ca_file", &data->ca_file) < 0)
        goto error;
    if (virConfGetValueString(conf, "crl_file", &data->crl_file) < 0)
        goto error;
300

301 302
    if (virConfGetValueStringList(conf, "tls_allowed_dn_list", false,
                                  &data->tls_allowed_dn_list) < 0)
303 304
        goto error;

305 306 307
    if (virConfGetValueString(conf, "tls_priority", &data->tls_priority) < 0)
        goto error;
#endif /* ! WITH_IP */
308

309 310
    if (virConfGetValueStringList(conf, "sasl_allowed_username_list", false,
                                  &data->sasl_allowed_username_list) < 0)
311 312
        goto error;

313 314 315 316
    if (virConfGetValueUInt(conf, "min_workers", &data->min_workers) < 0)
        goto error;
    if (virConfGetValueUInt(conf, "max_workers", &data->max_workers) < 0)
        goto error;
317 318 319 320 321
    if (data->max_workers < 1) {
        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                       _("'max_workers' must be greater than 0"));
        goto error;
    }
322 323 324 325 326 327
    if (virConfGetValueUInt(conf, "max_clients", &data->max_clients) < 0)
        goto error;
    if (virConfGetValueUInt(conf, "max_queued_clients", &data->max_queued_clients) < 0)
        goto error;
    if (virConfGetValueUInt(conf, "max_anonymous_clients", &data->max_anonymous_clients) < 0)
        goto error;
328

329 330
    if (virConfGetValueUInt(conf, "prio_workers", &data->prio_workers) < 0)
        goto error;
331

332 333
    if (virConfGetValueUInt(conf, "max_client_requests", &data->max_client_requests) < 0)
        goto error;
334

335 336 337 338 339 340 341 342 343 344
    if (virConfGetValueUInt(conf, "admin_min_workers", &data->admin_min_workers) < 0)
        goto error;
    if (virConfGetValueUInt(conf, "admin_max_workers", &data->admin_max_workers) < 0)
        goto error;
    if (virConfGetValueUInt(conf, "admin_max_clients", &data->admin_max_clients) < 0)
        goto error;
    if (virConfGetValueUInt(conf, "admin_max_queued_clients", &data->admin_max_queued_clients) < 0)
        goto error;
    if (virConfGetValueUInt(conf, "admin_max_client_requests", &data->admin_max_client_requests) < 0)
        goto error;
345

346 347 348 349
    if (virConfGetValueUInt(conf, "audit_level", &data->audit_level) < 0)
        goto error;
    if (virConfGetValueBool(conf, "audit_logging", &data->audit_logging) < 0)
        goto error;
350

351 352 353 354
    if (virConfGetValueString(conf, "host_uuid", &data->host_uuid) < 0)
        goto error;
    if (virConfGetValueString(conf, "host_uuid_source", &data->host_uuid_source) < 0)
        goto error;
355

356 357 358 359 360 361
    if (virConfGetValueUInt(conf, "log_level", &data->log_level) < 0)
        goto error;
    if (virConfGetValueString(conf, "log_filters", &data->log_filters) < 0)
        goto error;
    if (virConfGetValueString(conf, "log_outputs", &data->log_outputs) < 0)
        goto error;
362

363 364 365 366
    if (virConfGetValueInt(conf, "keepalive_interval", &data->keepalive_interval) < 0)
        goto error;
    if (virConfGetValueUInt(conf, "keepalive_count", &data->keepalive_count) < 0)
        goto error;
367

368 369 370 371
    if (virConfGetValueInt(conf, "admin_keepalive_interval", &data->admin_keepalive_interval) < 0)
        goto error;
    if (virConfGetValueUInt(conf, "admin_keepalive_count", &data->admin_keepalive_count) < 0)
        goto error;
372

373 374 375
    if (virConfGetValueUInt(conf, "ovs_timeout", &data->ovs_timeout) < 0)
        goto error;

376 377
    return 0;

378
 error:
379 380
    return -1;
}
381 382 383 384 385 386 387 388 389 390


/* Read the config file if it exists.
 * Only used in the remote case, hence the name.
 */
int
daemonConfigLoadFile(struct daemonConfig *data,
                     const char *filename,
                     bool allow_missing)
{
J
Ján Tomko 已提交
391
    g_autoptr(virConf) conf = NULL;
392 393 394 395 396 397 398 399 400 401

    if (allow_missing &&
        access(filename, R_OK) == -1 &&
        errno == ENOENT)
        return 0;

    conf = virConfReadFile(filename, 0);
    if (!conf)
        return -1;

402
    return daemonConfigLoadOptions(data, filename, conf);
403 404 405 406 407 408
}

int daemonConfigLoadData(struct daemonConfig *data,
                         const char *filename,
                         const char *filedata)
{
J
Ján Tomko 已提交
409
    g_autoptr(virConf) conf = NULL;
410

J
Ján Tomko 已提交
411
    conf = virConfReadString(filedata, 0);
412 413 414
    if (!conf)
        return -1;

415
    return daemonConfigLoadOptions(data, filename, conf);
416
}