#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
-#include <string.h>
#include <pulse/xmalloc.h>
-#include <pulse/i18n.h>
+#include <pulsecore/i18n.h>
#include <pulsecore/macro.h>
#include <pulsecore/core-error.h>
#include <pulsecore/log.h>
.default_sink = NULL,
.default_source = NULL,
.default_server = NULL,
- .autospawn = TRUE,
- .disable_shm = FALSE,
- .cookie_file = NULL,
- .cookie_valid = FALSE,
- .shm_size = 0
+ .default_dbus_server = NULL,
+ .cookie_file_from_env = NULL,
+ .cookie_from_x11_valid = false,
+ .cookie_file_from_application = NULL,
+ .cookie_file_from_client_conf = NULL,
+ .autospawn = true,
+ .disable_shm = false,
+ .shm_size = 0,
+ .auto_connect_localhost = false,
+ .auto_connect_display = false
};
pa_client_conf *pa_client_conf_new(void) {
c->daemon_binary = pa_xstrdup(PA_BINARY);
c->extra_arguments = pa_xstrdup("--log-target=syslog");
- c->cookie_file = pa_xstrdup(PA_NATIVE_COOKIE_FILE);
return c;
}
pa_xfree(c->default_sink);
pa_xfree(c->default_source);
pa_xfree(c->default_server);
- pa_xfree(c->cookie_file);
+ pa_xfree(c->default_dbus_server);
+ pa_xfree(c->cookie_file_from_env);
+ pa_xfree(c->cookie_file_from_application);
+ pa_xfree(c->cookie_file_from_client_conf);
pa_xfree(c);
}
-int pa_client_conf_load(pa_client_conf *c, const char *filename) {
+int pa_client_conf_load(pa_client_conf *c) {
FILE *f = NULL;
char *fn = NULL;
int r = -1;
/* Prepare the configuration parse table */
pa_config_item table[] = {
- { "daemon-binary", pa_config_parse_string, NULL, NULL },
- { "extra-arguments", pa_config_parse_string, NULL, NULL },
- { "default-sink", pa_config_parse_string, NULL, NULL },
- { "default-source", pa_config_parse_string, NULL, NULL },
- { "default-server", pa_config_parse_string, NULL, NULL },
- { "autospawn", pa_config_parse_bool, NULL, NULL },
- { "cookie-file", pa_config_parse_string, NULL, NULL },
- { "disable-shm", pa_config_parse_bool, NULL, NULL },
- { "shm-size-bytes", pa_config_parse_size, NULL, NULL },
- { NULL, NULL, NULL, NULL },
+ { "daemon-binary", pa_config_parse_string, &c->daemon_binary, NULL },
+ { "extra-arguments", pa_config_parse_string, &c->extra_arguments, NULL },
+ { "default-sink", pa_config_parse_string, &c->default_sink, NULL },
+ { "default-source", pa_config_parse_string, &c->default_source, NULL },
+ { "default-server", pa_config_parse_string, &c->default_server, NULL },
+ { "default-dbus-server", pa_config_parse_string, &c->default_dbus_server, NULL },
+ { "autospawn", pa_config_parse_bool, &c->autospawn, NULL },
+ { "cookie-file", pa_config_parse_string, &c->cookie_file_from_client_conf, NULL },
+ { "disable-shm", pa_config_parse_bool, &c->disable_shm, NULL },
+ { "enable-shm", pa_config_parse_not_bool, &c->disable_shm, NULL },
+ { "shm-size-bytes", pa_config_parse_size, &c->shm_size, NULL },
+ { "auto-connect-localhost", pa_config_parse_bool, &c->auto_connect_localhost, NULL },
+ { "auto-connect-display", pa_config_parse_bool, &c->auto_connect_display, NULL },
+ { NULL, NULL, NULL, NULL },
};
- table[0].data = &c->daemon_binary;
- table[1].data = &c->extra_arguments;
- table[2].data = &c->default_sink;
- table[3].data = &c->default_source;
- table[4].data = &c->default_server;
- table[5].data = &c->autospawn;
- table[6].data = &c->cookie_file;
- table[7].data = &c->disable_shm;
- table[8].data = &c->shm_size;
+ if (!(f = pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn)))
+ if (errno != ENOENT)
+ goto finish;
- if (filename) {
+ r = f ? pa_config_parse(fn, f, table, NULL, NULL) : 0;
- if (!(f = fopen(filename, "r"))) {
- pa_log(_("Failed to open configuration file '%s': %s"), fn, pa_cstrerror(errno));
- goto finish;
+finish:
+ pa_xfree(fn);
+
+ if (f)
+ fclose(f);
+
+ return r;
+}
+
+int pa_client_conf_load_cookie(pa_client_conf *c, uint8_t *cookie, size_t cookie_length) {
+ int r;
+
+ pa_assert(c);
+ pa_assert(cookie);
+ pa_assert(cookie_length > 0);
+
+ if (c->cookie_file_from_env) {
+ r = pa_authkey_load_auto(c->cookie_file_from_env, true, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
+
+ pa_log_warn("Failed to load cookie from %s (configured with environment variable PULSE_COOKIE): %s",
+ c->cookie_file_from_env, pa_cstrerror(errno));
+ }
+
+ if (c->cookie_from_x11_valid) {
+ if (cookie_length == sizeof(c->cookie_from_x11)) {
+ memcpy(cookie, c->cookie_from_x11, cookie_length);
+ return 0;
}
- fn = pa_xstrdup(fn);
+ pa_log_warn("Failed to load cookie from X11 root window property PULSE_COOKIE: size mismatch.");
+ }
- } else {
+ if (c->cookie_file_from_application) {
+ r = pa_authkey_load_auto(c->cookie_file_from_application, true, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
- if (!(f = pa_open_config_file(DEFAULT_CLIENT_CONFIG_FILE, DEFAULT_CLIENT_CONFIG_FILE_USER, ENV_CLIENT_CONFIG_FILE, &fn)))
- if (errno != ENOENT)
- goto finish;
+ pa_log_warn("Failed to load cookie from %s (configured by the application): %s", c->cookie_file_from_application,
+ pa_cstrerror(errno));
}
- r = f ? pa_config_parse(fn, f, table, NULL) : 0;
+ if (c->cookie_file_from_client_conf) {
+ r = pa_authkey_load_auto(c->cookie_file_from_client_conf, true, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
- if (!r)
- r = pa_client_conf_load_cookie(c);
+ pa_log_warn("Failed to load cookie from %s (configured in client.conf): %s", c->cookie_file_from_client_conf,
+ pa_cstrerror(errno));
+ }
-finish:
- pa_xfree(fn);
+ r = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE, false, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
- if (f)
- fclose(f);
+ r = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE_FALLBACK, false, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
- return r;
+ r = pa_authkey_load_auto(PA_NATIVE_COOKIE_FILE, true, cookie, cookie_length);
+ if (r >= 0)
+ return 0;
+
+ pa_log("Failed to load cookie file from %s: %s", PA_NATIVE_COOKIE_FILE, pa_cstrerror(errno));
+ memset(cookie, 0, cookie_length);
+
+ return -1;
}
int pa_client_conf_env(pa_client_conf *c) {
if ((e = getenv(ENV_DEFAULT_SERVER))) {
pa_xfree(c->default_server);
c->default_server = pa_xstrdup(e);
+
+ /* We disable autospawning automatically if a specific server was set */
+ c->autospawn = false;
}
if ((e = getenv(ENV_DAEMON_BINARY))) {
c->daemon_binary = pa_xstrdup(e);
}
- if ((e = getenv(ENV_COOKIE_FILE))) {
- pa_xfree(c->cookie_file);
- c->cookie_file = pa_xstrdup(e);
-
- return pa_client_conf_load_cookie(c);
+ if ((e = getenv(ENV_COOKIE_FILE)) && *e) {
+ pa_xfree(c->cookie_file_from_env);
+ c->cookie_file_from_env = pa_xstrdup(e);
}
return 0;
}
-int pa_client_conf_load_cookie(pa_client_conf* c) {
+void pa_client_conf_set_cookie_file_from_application(pa_client_conf *c, const char *cookie_file) {
pa_assert(c);
+ pa_assert(!cookie_file || *cookie_file);
- if (!c->cookie_file)
- return -1;
-
- c->cookie_valid = FALSE;
-
- if (pa_authkey_load_auto(c->cookie_file, c->cookie, sizeof(c->cookie)) < 0)
- return -1;
-
- c->cookie_valid = TRUE;
- return 0;
+ pa_xfree(c->cookie_file_from_application);
+ c->cookie_file_from_application = pa_xstrdup(cookie_file);
}