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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
*
* ck-connector.c : Code for login managers to register with ConsoleKit.
*
* Copyright (c) 2007 David Zeuthen <davidz@redhat.com>
* Copyright (c) 2007 William Jon McCann <mccann@jhu.edu>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <dbus/dbus.h>
#include "ck-connector.h"
#define N_ELEMENTS(arr) (sizeof (arr) / sizeof ((arr)[0]))
#if defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
#define _CK_FUNCTION_NAME __func__
#elif defined(__GNUC__) || defined(_MSC_VER)
#define _CK_FUNCTION_NAME __FUNCTION__
#else
#define _CK_FUNCTION_NAME "unknown function"
#endif
#define CK_CONNECTOR_ERROR "org.freedesktop.CkConnector.Error"
#define _CK_WARNING_FORMAT "arguments to %s() were incorrect, assertion \"%s\" failed in file %s line %d.\n"
#define _ck_return_if_fail(condition) do { \
if (!(condition)) { \
fprintf (stderr, _CK_WARNING_FORMAT, _CK_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
return; \
} } while (0)
#define _ck_return_val_if_fail(condition, val) do { \
if (!(condition)) { \
fprintf (stderr, _CK_WARNING_FORMAT, _CK_FUNCTION_NAME, #condition, __FILE__, __LINE__); \
return val; \
} } while (0)
struct _CkConnector
{
int refcount;
char *cookie;
dbus_bool_t session_created;
DBusConnection *connection;
};
static struct {
char *name;
int type;
} parameter_lookup[] = {
{ "display-device", DBUS_TYPE_STRING },
{ "x11-display-device", DBUS_TYPE_STRING },
{ "x11-display", DBUS_TYPE_STRING },
{ "remote-host-name", DBUS_TYPE_STRING },
{ "session-type", DBUS_TYPE_STRING },
{ "is-local", DBUS_TYPE_BOOLEAN },
{ "unix-user", DBUS_TYPE_INT32 },
};
static int
lookup_parameter_type (const char *name)
{
int i;
int type;
type = DBUS_TYPE_INVALID;
for (i = 0; i < N_ELEMENTS (parameter_lookup); i++) {
if (strcmp (name, parameter_lookup[i].name) == 0) {
type = parameter_lookup[i].type;
break;
}
}
return type;
}
static dbus_bool_t
add_param_basic (DBusMessageIter *iter_array,
const char *name,
int type,
const void *value)
{
DBusMessageIter iter_struct;
DBusMessageIter iter_variant;
const char *container_type;
switch (type) {
case DBUS_TYPE_STRING:
container_type = DBUS_TYPE_STRING_AS_STRING;
break;
case DBUS_TYPE_BOOLEAN:
container_type = DBUS_TYPE_BOOLEAN_AS_STRING;
break;
case DBUS_TYPE_INT32:
container_type = DBUS_TYPE_INT32_AS_STRING;
break;
default:
goto oom;
break;
}
if (! dbus_message_iter_open_container (iter_array,
DBUS_TYPE_STRUCT,
NULL,
&iter_struct)) {
goto oom;
}
if (! dbus_message_iter_append_basic (&iter_struct,
DBUS_TYPE_STRING,
&name)) {
goto oom;
}
if (! dbus_message_iter_open_container (&iter_struct,
DBUS_TYPE_VARIANT,
container_type,
&iter_variant)) {
goto oom;
}
if (! dbus_message_iter_append_basic (&iter_variant,
type,
value)) {
goto oom;
}
if (! dbus_message_iter_close_container (&iter_struct,
&iter_variant)) {
goto oom;
}
if (! dbus_message_iter_close_container (iter_array,
&iter_struct)) {
goto oom;
}
return TRUE;
oom:
return FALSE;
}
/* Frees all resources allocated and disconnects from the system
* message bus.
*/
static void
_ck_connector_free (CkConnector *connector)
{
if (connector->connection != NULL) {
/* it's a private connection so it's all good */
dbus_connection_close (connector->connection);
}
if (connector->cookie != NULL) {
free (connector->cookie);
}
free (connector);
}
/**
* Decrements the reference count of a CkConnector, disconnecting
* from the bus and freeing the connector if the count reaches 0.
*
* @param connector the connector
* @see ck_connector_ref
*/
void
ck_connector_unref (CkConnector *connector)
{
_ck_return_if_fail (connector != NULL);
/* Probably should use some kind of atomic op here */
connector->refcount -= 1;
if (connector->refcount == 0) {
_ck_connector_free (connector);
}
}
/**
* Increments the reference count of a CkConnector.
*
* @param connector the connector
* @returns the connector
* @see ck_connector_unref
*/
CkConnector *
ck_connector_ref (CkConnector *connector)
{
_ck_return_val_if_fail (connector != NULL, NULL);
/* Probably should use some kind of atomic op here */
connector->refcount += 1;
return connector;
}
/**
* Constructs a new Connector to communicate with the ConsoleKit
* daemon. Returns #NULL if memory can't be allocated for the
* object.
*
* @returns a new CkConnector, free with ck_connector_unref()
*/
CkConnector *
ck_connector_new (void)
{
CkConnector *connector;
connector = calloc (1, sizeof (CkConnector));
if (connector == NULL) {
goto oom;
}
connector->refcount = 1;
connector->connection = NULL;
connector->cookie = NULL;
connector->session_created = FALSE;
oom:
return connector;
}
/**
* Connects to the D-Bus system bus daemon and issues the method call
* OpenSession on the ConsoleKit manager interface. The
* connection to the bus is private.
*
* Returns FALSE on OOM, if the system bus daemon is not running, if
* the ConsoleKit daemon is not running or if the caller doesn't have
* sufficient privileges.
*
* @returns #TRUE if the operation succeeds
*/
dbus_bool_t
ck_connector_open_session (CkConnector *connector,
DBusError *error)
{
DBusError local_error;
DBusMessage *message;
DBusMessage *reply;
dbus_bool_t ret;
char *cookie;
_ck_return_val_if_fail (connector != NULL, FALSE);
_ck_return_val_if_fail ((error) == NULL || !dbus_error_is_set ((error)), FALSE);
reply = NULL;
message = NULL;
ret = FALSE;
dbus_error_init (&local_error);
connector->connection = dbus_bus_get_private (DBUS_BUS_SYSTEM, &local_error);
if (connector->connection == NULL) {
if (dbus_error_is_set (&local_error)) {
dbus_set_error (error,
CK_CONNECTOR_ERROR,
"Unable to open session: %s",
local_error.message);
dbus_error_free (&local_error);
}
goto out;
}
dbus_connection_set_exit_on_disconnect (connector->connection, FALSE);
message = dbus_message_new_method_call ("org.freedesktop.ConsoleKit",
"/org/freedesktop/ConsoleKit/Manager",
"org.freedesktop.ConsoleKit.Manager",
"OpenSession");
if (message == NULL) {
goto out;
}
dbus_error_init (&local_error);
reply = dbus_connection_send_with_reply_and_block (connector->connection,
message,
-1,
&local_error);
if (reply == NULL) {
if (dbus_error_is_set (&local_error)) {
dbus_set_error (error,
CK_CONNECTOR_ERROR,
"Unable to open session: %s",
local_error.message);
dbus_error_free (&local_error);
goto out;
}
}
dbus_error_init (&local_error);
if (! dbus_message_get_args (reply,
&local_error,
DBUS_TYPE_STRING, &cookie,
DBUS_TYPE_INVALID)) {
if (dbus_error_is_set (&local_error)) {
dbus_set_error (error,
CK_CONNECTOR_ERROR,
"Unable to open session: %s",
local_error.message);
dbus_error_free (&local_error);
goto out;
}
}
connector->cookie = strdup (cookie);
if (connector->cookie == NULL) {
goto out;
}
connector->session_created = TRUE;
ret = TRUE;
out:
if (reply != NULL) {
dbus_message_unref (reply);
}
if (message != NULL) {
dbus_message_unref (message);
}
return ret;
}
static dbus_bool_t
ck_connector_open_session_with_parameters_valist (CkConnector *connector,
DBusError *error,
const char *first_parameter_name,
va_list var_args)
{
DBusError local_error;
DBusMessage *message;
DBusMessage *reply;
DBusMessageIter iter;
DBusMessageIter iter_array;
dbus_bool_t ret;
char *cookie;
const char *name;
_ck_return_val_if_fail (connector != NULL, FALSE);
reply = NULL;
message = NULL;
ret = FALSE;
dbus_error_init (&local_error);
connector->connection = dbus_bus_get_private (DBUS_BUS_SYSTEM, &local_error);
if (connector->connection == NULL) {
if (dbus_error_is_set (&local_error)) {
dbus_set_error (error,
CK_CONNECTOR_ERROR,
"Unable to open session: %s",
local_error.message);
dbus_error_free (&local_error);
}
goto out;
}
dbus_connection_set_exit_on_disconnect (connector->connection, FALSE);
message = dbus_message_new_method_call ("org.freedesktop.ConsoleKit",
"/org/freedesktop/ConsoleKit/Manager",
"org.freedesktop.ConsoleKit.Manager",
"OpenSessionWithParameters");
if (message == NULL) {
goto out;
}
dbus_message_iter_init_append (message, &iter);
if (! dbus_message_iter_open_container (&iter,
DBUS_TYPE_ARRAY,
"(sv)",
&iter_array)) {
goto out;
}
name = first_parameter_name;
while (name != NULL) {
int type;
const void *value;
dbus_bool_t res;
type = lookup_parameter_type (name);
value = va_arg (var_args, const void *);
if (type == DBUS_TYPE_INVALID) {
dbus_set_error (error,
CK_CONNECTOR_ERROR,
"Unknown parameter: %s",
name);
goto out;
}
if (value != NULL)
res = add_param_basic (&iter_array, name, type, value);
else
res = TRUE;
if (! res) {
dbus_set_error (error,
CK_CONNECTOR_ERROR,
"Error adding parameter: %s",
name);
goto out;
}
name = va_arg (var_args, char *);
}
if (! dbus_message_iter_close_container (&iter, &iter_array)) {
goto out;
}
dbus_error_init (&local_error);
reply = dbus_connection_send_with_reply_and_block (connector->connection,
message,
-1,
&local_error);
if (reply == NULL) {
if (dbus_error_is_set (&local_error)) {
dbus_set_error (error,
CK_CONNECTOR_ERROR,
"Unable to open session: %s",
local_error.message);
dbus_error_free (&local_error);
goto out;
}
}
dbus_error_init (&local_error);
if (! dbus_message_get_args (reply,
&local_error,
DBUS_TYPE_STRING, &cookie,
DBUS_TYPE_INVALID)) {
if (dbus_error_is_set (&local_error)) {
dbus_set_error (error,
CK_CONNECTOR_ERROR,
"Unable to open session: %s",
local_error.message);
dbus_error_free (&local_error);
goto out;
}
}
connector->cookie = strdup (cookie);
if (connector->cookie == NULL) {
goto out;
}
connector->session_created = TRUE;
ret = TRUE;
out:
if (reply != NULL) {
dbus_message_unref (reply);
}
if (message != NULL) {
dbus_message_unref (message);
}
return ret;
}
/**
* Opens a new session with parameter from variable argument list. The
* variable argument list should contain the name of each parameter
* followed by the value to append.
* For example:
*
* @code
*
* DBusError error;
* dbus_int32_t v_INT32 = 500;
* const char *v_STRING = "/dev/tty3";
*
* dbus_error_init (&error);
* ck_connector_open_session_with_parameters (connector,
* &error,
* "unix-user", &v_INT32,
* "display-device", &v_STRING,
* NULL);
* @endcode
*
* @param error error output
* @param first_parameter_name name of the first parameter
* @param ... value of first parameter, list of additional name-value pairs
* @returns #TRUE on success
*/
dbus_bool_t
ck_connector_open_session_with_parameters (CkConnector *connector,
DBusError *error,
const char *first_parameter_name,
...)
{
va_list var_args;
dbus_bool_t ret;
_ck_return_val_if_fail (connector != NULL, FALSE);
_ck_return_val_if_fail ((error) == NULL || !dbus_error_is_set ((error)), FALSE);
va_start (var_args, first_parameter_name);
ret = ck_connector_open_session_with_parameters_valist (connector,
error,
first_parameter_name,
var_args);
va_end (var_args);
return ret;
}
/**
* Connects to the D-Bus system bus daemon and issues the method call
* OpenSessionWithParameters on the ConsoleKit manager interface. The
* connection to the bus is private.
*
* The only parameter that is optional is x11_display - it may be set
* to NULL if there is no X11 server associated with the session.
*
* Returns FALSE on OOM, if the system bus daemon is not running, if
* the ConsoleKit daemon is not running or if the caller doesn't have
* sufficient privileges.
*
* @param user UID for the user owning the session
* @param display_device the tty device for the session
* @param x11_display the value of the X11 DISPLAY for the session
* @returns #TRUE if the operation succeeds
*/
dbus_bool_t
ck_connector_open_session_for_user (CkConnector *connector,
uid_t user,
const char *display_device,
const char *x11_display,
DBusError *error)
{
dbus_bool_t ret;
_ck_return_val_if_fail (connector != NULL, FALSE);
_ck_return_val_if_fail (display_device != NULL, FALSE);
_ck_return_val_if_fail ((error) == NULL || !dbus_error_is_set ((error)), FALSE);
ret = ck_connector_open_session_with_parameters (connector,
error,
"display-device", &display_device,
"x11-display", &x11_display,
"unix-user", &user,
NULL);
return ret;
}
/**
* Gets the cookie for the current open session.
* Returns #NULL if no session is open.
*
* @returns a constant string with the cookie.
*/
const char *
ck_connector_get_cookie (CkConnector *connector)
{
_ck_return_val_if_fail (connector != NULL, NULL);
if (! connector->session_created) {
return NULL;
} else {
return connector->cookie;
}
}
/**
* Issues the CloseSession method call on the ConsoleKit manager
* interface.
*
* Returns FALSE on OOM, if the system bus daemon is not running, if
* the ConsoleKit daemon is not running, if the caller doesn't have
* sufficient privilege or if a session isn't open.
*
* @returns #TRUE if the operation succeeds
*/
dbus_bool_t
ck_connector_close_session (CkConnector *connector,
DBusError *error)
{
DBusError local_error;
DBusMessage *message;
DBusMessage *reply;
dbus_bool_t ret;
dbus_bool_t session_closed;
_ck_return_val_if_fail (connector != NULL, FALSE);
_ck_return_val_if_fail ((error) == NULL || !dbus_error_is_set ((error)), FALSE);
reply = NULL;
message = NULL;
ret = FALSE;
if (!connector->session_created || connector->cookie == NULL) {
dbus_set_error (error,
CK_CONNECTOR_ERROR,
"Unable to close session: %s",
"no session open");
goto out;
}
dbus_error_init (&local_error);
message = dbus_message_new_method_call ("org.freedesktop.ConsoleKit",
"/org/freedesktop/ConsoleKit/Manager",
"org.freedesktop.ConsoleKit.Manager",
"CloseSession");
if (message == NULL) {
goto out;
}
if (! dbus_message_append_args (message,
DBUS_TYPE_STRING, &(connector->cookie),
DBUS_TYPE_INVALID)) {
goto out;
}
dbus_error_init (&local_error);
reply = dbus_connection_send_with_reply_and_block (connector->connection,
message,
-1,
&local_error);
if (reply == NULL) {
if (dbus_error_is_set (&local_error)) {
dbus_set_error (error,
CK_CONNECTOR_ERROR,
"Unable to close session: %s",
local_error.message);
dbus_error_free (&local_error);
goto out;
}
}
dbus_error_init (&local_error);
if (! dbus_message_get_args (reply,
&local_error,
DBUS_TYPE_BOOLEAN, &session_closed,
DBUS_TYPE_INVALID)) {
if (dbus_error_is_set (&local_error)) {
dbus_set_error (error,
CK_CONNECTOR_ERROR,
"Unable to close session: %s",
local_error.message);
dbus_error_free (&local_error);
goto out;
}
}
if (! session_closed) {
goto out;
}
connector->session_created = FALSE;
ret = TRUE;
out:
if (reply != NULL) {
dbus_message_unref (reply);
}
if (message != NULL) {
dbus_message_unref (message);
}
return ret;
}
|