]> code.delx.au - pulseaudio/blob - src/modules/module-protocol-stub.c
esound auth-ip-acl fix
[pulseaudio] / src / modules / module-protocol-stub.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
6
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2 of the License,
10 or (at your option) any later version.
11
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA.
21 ***/
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <limits.h>
32
33 #ifdef HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef HAVE_ARPA_INET_H
37 #include <arpa/inet.h>
38 #endif
39 #ifdef HAVE_NETINET_IN_H
40 #include <netinet/in.h>
41 #endif
42
43 #include <pulse/xmalloc.h>
44
45 #include <pulsecore/winsock.h>
46 #include <pulsecore/core-error.h>
47 #include <pulsecore/module.h>
48 #include <pulsecore/socket-server.h>
49 #include <pulsecore/socket-util.h>
50 #include <pulsecore/core-util.h>
51 #include <pulsecore/modargs.h>
52 #include <pulsecore/log.h>
53 #include <pulsecore/native-common.h>
54 #include <pulsecore/creds.h>
55
56 #ifdef USE_TCP_SOCKETS
57 #define SOCKET_DESCRIPTION "(TCP sockets)"
58 #define SOCKET_USAGE "port=<TCP port number> listen=<address to listen on>"
59 #else
60 #define SOCKET_DESCRIPTION "(UNIX sockets)"
61 #define SOCKET_USAGE "socket=<path to UNIX socket>"
62 #endif
63
64 #if defined(USE_PROTOCOL_SIMPLE)
65 # include <pulsecore/protocol-simple.h>
66 # define TCPWRAP_SERVICE "pulseaudio-simple"
67 # define IPV4_PORT 4711
68 # define UNIX_SOCKET "simple"
69 # define MODULE_ARGUMENTS "rate", "format", "channels", "sink", "source", "playback", "record",
70
71 # if defined(USE_TCP_SOCKETS)
72 # include "module-simple-protocol-tcp-symdef.h"
73 # else
74 # include "module-simple-protocol-unix-symdef.h"
75 # endif
76
77 PA_MODULE_DESCRIPTION("Simple protocol "SOCKET_DESCRIPTION);
78 PA_MODULE_USAGE("rate=<sample rate> "
79 "format=<sample format> "
80 "channels=<number of channels> "
81 "sink=<sink to connect to> "
82 "source=<source to connect to> "
83 "playback=<enable playback?> "
84 "record=<enable record?> "
85 SOCKET_USAGE);
86 #elif defined(USE_PROTOCOL_CLI)
87 # include <pulsecore/protocol-cli.h>
88 # define TCPWRAP_SERVICE "pulseaudio-cli"
89 # define IPV4_PORT 4712
90 # define UNIX_SOCKET "cli"
91 # define MODULE_ARGUMENTS
92
93 # ifdef USE_TCP_SOCKETS
94 # include "module-cli-protocol-tcp-symdef.h"
95 # else
96 # include "module-cli-protocol-unix-symdef.h"
97 # endif
98
99 PA_MODULE_DESCRIPTION("Command line interface protocol "SOCKET_DESCRIPTION);
100 PA_MODULE_USAGE(SOCKET_USAGE);
101 #elif defined(USE_PROTOCOL_HTTP)
102 # include <pulsecore/protocol-http.h>
103 # define TCPWRAP_SERVICE "pulseaudio-http"
104 # define IPV4_PORT 4714
105 # define UNIX_SOCKET "http"
106 # define MODULE_ARGUMENTS
107
108 # ifdef USE_TCP_SOCKETS
109 # include "module-http-protocol-tcp-symdef.h"
110 # else
111 # include "module-http-protocol-unix-symdef.h"
112 # endif
113
114 PA_MODULE_DESCRIPTION("HTTP "SOCKET_DESCRIPTION);
115 PA_MODULE_USAGE(SOCKET_USAGE);
116 #elif defined(USE_PROTOCOL_NATIVE)
117 # include <pulsecore/protocol-native.h>
118 # define TCPWRAP_SERVICE "pulseaudio-native"
119 # define IPV4_PORT PA_NATIVE_DEFAULT_PORT
120 # define UNIX_SOCKET PA_NATIVE_DEFAULT_UNIX_SOCKET
121 # define MODULE_ARGUMENTS_COMMON "cookie", "auth-cookie", "auth-cookie-enabled", "auth-anonymous",
122
123 # ifdef USE_TCP_SOCKETS
124 # include "module-native-protocol-tcp-symdef.h"
125 # else
126 # include "module-native-protocol-unix-symdef.h"
127 # endif
128
129 # if defined(HAVE_CREDS) && !defined(USE_TCP_SOCKETS)
130 # define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-group", "auth-group-enable",
131 # define AUTH_USAGE "auth-group=<system group to allow access> auth-group-enable=<enable auth by UNIX group?> "
132 # elif defined(USE_TCP_SOCKETS)
133 # define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-ip-acl",
134 # define AUTH_USAGE "auth-ip-acl=<IP address ACL to allow access> "
135 # else
136 # define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
137 # define AUTH_USAGE
138 # endif
139
140 PA_MODULE_DESCRIPTION("Native protocol "SOCKET_DESCRIPTION);
141 PA_MODULE_USAGE("auth-anonymous=<don't check for cookies?> "
142 "auth-cookie=<path to cookie file> "
143 "auth-cookie-enabled=<enable cookie authentification? "
144 AUTH_USAGE
145 SOCKET_USAGE);
146 #elif defined(USE_PROTOCOL_ESOUND)
147 # include <pulsecore/protocol-esound.h>
148 # include <pulsecore/esound.h>
149 # define TCPWRAP_SERVICE "esound"
150 # define IPV4_PORT ESD_DEFAULT_PORT
151 # define MODULE_ARGUMENTS_COMMON "sink", "source", "auth-anonymous", "cookie", "auth-cookie", "auth-cookie-enabled",
152
153 # ifdef USE_TCP_SOCKETS
154 # include "module-esound-protocol-tcp-symdef.h"
155 # else
156 # include "module-esound-protocol-unix-symdef.h"
157 # endif
158
159 # if defined(USE_TCP_SOCKETS)
160 # define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON "auth-ip-acl",
161 # define AUTH_USAGE "auth-ip-acl=<IP address ACL to allow access> "
162 # else
163 # define MODULE_ARGUMENTS MODULE_ARGUMENTS_COMMON
164 # define AUTH_USAGE
165 # endif
166
167 PA_MODULE_DESCRIPTION("ESOUND protocol "SOCKET_DESCRIPTION);
168 PA_MODULE_USAGE("sink=<sink to connect to> "
169 "source=<source to connect to> "
170 "auth-anonymous=<don't verify cookies?> "
171 "auth-cookie=<path to cookie file> "
172 "auth-cookie-enabled=<enable cookie authentification? "
173 AUTH_USAGE
174 SOCKET_USAGE);
175 #else
176 # error "Broken build system"
177 #endif
178
179 PA_MODULE_LOAD_ONCE(FALSE);
180 PA_MODULE_AUTHOR("Lennart Poettering");
181 PA_MODULE_VERSION(PACKAGE_VERSION);
182
183 static const char* const valid_modargs[] = {
184 MODULE_ARGUMENTS
185 #if defined(USE_TCP_SOCKETS)
186 "port",
187 "listen",
188 #else
189 "socket",
190 #endif
191 NULL
192 };
193
194 struct userdata {
195 pa_module *module;
196
197 #if defined(USE_PROTOCOL_SIMPLE)
198 pa_simple_protocol *simple_protocol;
199 pa_simple_options *simple_options;
200 #elif defined(USE_PROTOCOL_CLI)
201 pa_cli_protocol *cli_protocol;
202 #elif defined(USE_PROTOCOL_HTTP)
203 pa_http_protocol *http_protocol;
204 #elif defined(USE_PROTOCOL_NATIVE)
205 pa_native_protocol *native_protocol;
206 pa_native_options *native_options;
207 #else
208 pa_esound_protocol *esound_protocol;
209 pa_esound_options *esound_options;
210 #endif
211
212 #if defined(USE_TCP_SOCKETS)
213 pa_socket_server *socket_server_ipv4;
214 pa_socket_server *socket_server_ipv6;
215 #else
216 pa_socket_server *socket_server_unix;
217 char *socket_path;
218 #endif
219 };
220
221 static void socket_server_on_connection_cb(pa_socket_server*s, pa_iochannel *io, void *userdata) {
222 struct userdata *u = userdata;
223
224 pa_assert(s);
225 pa_assert(io);
226 pa_assert(u);
227
228 #if defined(USE_PROTOCOL_SIMPLE)
229 pa_simple_protocol_connect(u->simple_protocol, io, u->simple_options);
230 #elif defined(USE_PROTOCOL_CLI)
231 pa_cli_protocol_connect(u->cli_protocol, io, u->module);
232 #elif defined(USE_PROTOCOL_HTTP)
233 pa_http_protocol_connect(u->http_protocol, io, u->module);
234 #elif defined(USE_PROTOCOL_NATIVE)
235 pa_native_protocol_connect(u->native_protocol, io, u->native_options);
236 #else
237 pa_esound_protocol_connect(u->esound_protocol, io, u->esound_options);
238 #endif
239 }
240
241 int pa__init(pa_module*m) {
242 pa_modargs *ma = NULL;
243 struct userdata *u = NULL;
244
245 #if defined(USE_TCP_SOCKETS)
246 uint32_t port = IPV4_PORT;
247 const char *listen_on;
248 #else
249 int r;
250 #endif
251
252 #if defined(USE_PROTOCOL_NATIVE)
253 char t[256];
254 #endif
255
256 pa_assert(m);
257
258 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
259 pa_log("Failed to parse module arguments");
260 goto fail;
261 }
262
263 m->userdata = u = pa_xnew0(struct userdata, 1);
264 u->module = m;
265
266 #if defined(USE_PROTOCOL_SIMPLE)
267 u->simple_protocol = pa_simple_protocol_get(m->core);
268
269 u->simple_options = pa_simple_options_new();
270 if (pa_simple_options_parse(u->simple_options, m->core, ma) < 0)
271 goto fail;
272 u->simple_options->module = m;
273 #elif defined(USE_PROTOCOL_CLI)
274 u->cli_protocol = pa_cli_protocol_get(m->core);
275 #elif defined(USE_PROTOCOL_HTTP)
276 u->http_protocol = pa_http_protocol_get(m->core);
277 #elif defined(USE_PROTOCOL_NATIVE)
278 u->native_protocol = pa_native_protocol_get(m->core);
279
280 u->native_options = pa_native_options_new();
281 if (pa_native_options_parse(u->native_options, m->core, ma) < 0)
282 goto fail;
283 u->native_options->module = m;
284 #else
285 u->esound_protocol = pa_esound_protocol_get(m->core);
286
287 u->esound_options = pa_esound_options_new();
288 if (pa_esound_options_parse(u->esound_options, m->core, ma) < 0)
289 goto fail;
290 u->esound_options->module = m;
291 #endif
292
293 #if defined(USE_TCP_SOCKETS)
294 if (pa_modargs_get_value_u32(ma, "port", &port) < 0 || port < 1 || port > 0xFFFF) {
295 pa_log("port= expects a numerical argument between 1 and 65535.");
296 goto fail;
297 }
298
299 listen_on = pa_modargs_get_value(ma, "listen", NULL);
300
301 if (listen_on) {
302 u->socket_server_ipv6 = pa_socket_server_new_ipv6_string(m->core->mainloop, listen_on, (uint16_t) port, TCPWRAP_SERVICE);
303 u->socket_server_ipv4 = pa_socket_server_new_ipv4_string(m->core->mainloop, listen_on, (uint16_t) port, TCPWRAP_SERVICE);
304 } else {
305 u->socket_server_ipv6 = pa_socket_server_new_ipv6_any(m->core->mainloop, (uint16_t) port, TCPWRAP_SERVICE);
306 u->socket_server_ipv4 = pa_socket_server_new_ipv4_any(m->core->mainloop, (uint16_t) port, TCPWRAP_SERVICE);
307 }
308
309 if (!u->socket_server_ipv4 && !u->socket_server_ipv6)
310 goto fail;
311
312 if (u->socket_server_ipv4)
313 pa_socket_server_set_callback(u->socket_server_ipv4, socket_server_on_connection_cb, u);
314 if (u->socket_server_ipv6)
315 pa_socket_server_set_callback(u->socket_server_ipv6, socket_server_on_connection_cb, u);
316
317 #else
318
319 # if defined(USE_PROTOCOL_ESOUND)
320
321 # if defined(USE_PER_USER_ESOUND_SOCKET)
322 u->socket_path = pa_sprintf_malloc("/tmp/.esd-%lu/socket", (unsigned long) getuid());
323 # else
324 u->socket_path = pa_xstrdup("/tmp/.esd/socket");
325 # endif
326
327 /* This socket doesn't reside in our own runtime dir but in
328 * /tmp/.esd/, hence we have to create the dir first */
329
330 if (pa_make_secure_parent_dir(u->socket_path, pa_in_system_mode() ? 0755U : 0700U, (uid_t)-1, (gid_t)-1) < 0) {
331 pa_log("Failed to create socket directory '%s': %s\n", u->socket_path, pa_cstrerror(errno));
332 goto fail;
333 }
334
335 # else
336 if (!(u->socket_path = pa_runtime_path(pa_modargs_get_value(ma, "socket", UNIX_SOCKET)))) {
337 pa_log("Failed to generate socket path.");
338 goto fail;
339 }
340 # endif
341
342 if ((r = pa_unix_socket_remove_stale(u->socket_path)) < 0) {
343 pa_log("Failed to remove stale UNIX socket '%s': %s", u->socket_path, pa_cstrerror(errno));
344 goto fail;
345 } else if (r > 0)
346 pa_log_info("Removed stale UNIX socket '%s'.", u->socket_path);
347
348 if (!(u->socket_server_unix = pa_socket_server_new_unix(m->core->mainloop, u->socket_path)))
349 goto fail;
350
351 pa_socket_server_set_callback(u->socket_server_unix, socket_server_on_connection_cb, u);
352
353 #endif
354
355 #if defined(USE_PROTOCOL_NATIVE)
356 # if defined(USE_TCP_SOCKETS)
357 if (u->socket_server_ipv4)
358 if (pa_socket_server_get_address(u->socket_server_ipv4, t, sizeof(t)))
359 pa_native_protocol_add_server_string(u->native_protocol, t);
360
361 if (u->socket_server_ipv6)
362 if (pa_socket_server_get_address(u->socket_server_ipv6, t, sizeof(t)))
363 pa_native_protocol_add_server_string(u->native_protocol, t);
364 # else
365 if (pa_socket_server_get_address(u->socket_server_unix, t, sizeof(t)))
366 pa_native_protocol_add_server_string(u->native_protocol, t);
367
368 # endif
369 #endif
370
371 if (ma)
372 pa_modargs_free(ma);
373
374 return 0;
375
376 fail:
377
378 if (ma)
379 pa_modargs_free(ma);
380
381 pa__done(m);
382
383 return -1;
384 }
385
386 void pa__done(pa_module*m) {
387 struct userdata *u;
388
389 pa_assert(m);
390
391 if (!(u = m->userdata))
392 return;
393
394 #if defined(USE_PROTOCOL_SIMPLE)
395 if (u->simple_protocol) {
396 pa_simple_protocol_disconnect(u->simple_protocol, u->module);
397 pa_simple_protocol_unref(u->simple_protocol);
398 }
399 if (u->simple_options)
400 pa_simple_options_unref(u->simple_options);
401 #elif defined(USE_PROTOCOL_CLI)
402 if (u->cli_protocol) {
403 pa_cli_protocol_disconnect(u->cli_protocol, u->module);
404 pa_cli_protocol_unref(u->cli_protocol);
405 }
406 #elif defined(USE_PROTOCOL_HTTP)
407 if (u->http_protocol) {
408 pa_http_protocol_disconnect(u->http_protocol, u->module);
409 pa_http_protocol_unref(u->http_protocol);
410 }
411 #elif defined(USE_PROTOCOL_NATIVE)
412 if (u->native_protocol) {
413
414 char t[256];
415
416 # if defined(USE_TCP_SOCKETS)
417 if (u->socket_server_ipv4)
418 if (pa_socket_server_get_address(u->socket_server_ipv4, t, sizeof(t)))
419 pa_native_protocol_remove_server_string(u->native_protocol, t);
420
421 if (u->socket_server_ipv6)
422 if (pa_socket_server_get_address(u->socket_server_ipv6, t, sizeof(t)))
423 pa_native_protocol_remove_server_string(u->native_protocol, t);
424 # else
425 if (u->socket_server_unix)
426 if (pa_socket_server_get_address(u->socket_server_unix, t, sizeof(t)))
427 pa_native_protocol_remove_server_string(u->native_protocol, t);
428 # endif
429
430 pa_native_protocol_disconnect(u->native_protocol, u->module);
431 pa_native_protocol_unref(u->native_protocol);
432 }
433 if (u->native_options)
434 pa_native_options_unref(u->native_options);
435 #else
436 if (u->esound_protocol) {
437 pa_esound_protocol_disconnect(u->esound_protocol, u->module);
438 pa_esound_protocol_unref(u->esound_protocol);
439 }
440 if (u->esound_options)
441 pa_esound_options_unref(u->esound_options);
442 #endif
443
444 #if defined(USE_TCP_SOCKETS)
445 if (u->socket_server_ipv4)
446 pa_socket_server_unref(u->socket_server_ipv4);
447 if (u->socket_server_ipv6)
448 pa_socket_server_unref(u->socket_server_ipv6);
449 #else
450 if (u->socket_server_unix)
451 pa_socket_server_unref(u->socket_server_unix);
452
453 # if defined(USE_PROTOCOL_ESOUND) && !defined(USE_PER_USER_ESOUND_SOCKET)
454 if (u->socket_path) {
455 char *p = pa_parent_dir(u->socket_path);
456 rmdir(p);
457 pa_xfree(p);
458 }
459 # endif
460
461 pa_xfree(u->socket_path);
462 #endif
463
464 pa_xfree(u);
465 }