summaryrefslogtreecommitdiff
path: root/src/polkit/polkit-session.c
blob: 19002098024b93346c9ea6550ecbc1270e34b262 (plain)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/***************************************************************************
 *
 * polkit-session.c : sessions
 *
 * Copyright (C) 2007 David Zeuthen, <david@fubar.dk>
 *
 * 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.
 *
 **************************************************************************/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#include <errno.h>

#include "polkit-debug.h"
#include "polkit-session.h"
#include "polkit-utils.h"
#include "polkit-test.h"
#include "polkit-private.h"

/**
 * SECTION:polkit-session
 * @title: Session
 * @short_description: Represents a ConsoleKit Session.
 *
 * This class is used to represent a session.
 **/

/**
 * PolKitSession:
 *
 * Objects of this class are used to record information about a
 * session.
 **/
struct _PolKitSession
{
        int refcount;
        uid_t uid;
        PolKitSeat *seat;
        char *ck_objref;
        polkit_bool_t is_active;
        polkit_bool_t is_local;
        char *remote_host;
};

/**
 * polkit_session_new:
 * 
 * Creates a new #PolKitSession object.
 * 
 * Returns: the new object
 **/
PolKitSession *
polkit_session_new (void)
{
        PolKitSession *session;
        session = kit_new0 (PolKitSession, 1);
        if (session == NULL)
                goto out;
        session->refcount = 1;
out:
        return session;
}

/**
 * polkit_session_ref:
 * @session: The session object
 * 
 * Increase reference count.
 * 
 * Returns: the object
 **/
PolKitSession *
polkit_session_ref (PolKitSession *session)
{
        kit_return_val_if_fail (session != NULL, session);
        session->refcount++;
        return session;
}


/**
 * polkit_session_unref:
 * @session: The session object
 * 
 * Decreases the reference count of the object. If it becomes zero,
 * the object is freed. Before freeing, reference counts on embedded
 * objects are decresed by one.
 **/
void 
polkit_session_unref (PolKitSession *session)
{
        kit_return_if_fail (session != NULL);
        session->refcount--;
        if (session->refcount > 0) 
                return;
        kit_free (session->ck_objref);
        kit_free (session->remote_host);
        if (session->seat != NULL)
                polkit_seat_unref (session->seat);
        kit_free (session);
}

/**
 * polkit_session_set_uid:
 * @session: The session object
 * @uid: UNIX user id
 * 
 * Set the UNIX user id of the user owning the session.
 *
 * Returns: #TRUE only if the value validated and was set
 **/
polkit_bool_t
polkit_session_set_uid (PolKitSession *session, uid_t uid)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        session->uid = uid;
        return TRUE;
}

/**
 * polkit_session_set_ck_objref:
 * @session: The session object
 * @ck_objref: D-Bus object path
 * 
 * Set the D-Bus object path to the ConsoleKit session object.
 *
 * Returns: #TRUE only if the value validated and was set
 **/
polkit_bool_t
polkit_session_set_ck_objref (PolKitSession *session, const char *ck_objref)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        kit_return_val_if_fail (_pk_validate_identifier (ck_objref), FALSE);
        if (session->ck_objref != NULL)
                kit_free (session->ck_objref);
        session->ck_objref = kit_strdup (ck_objref);
        if (session->ck_objref == NULL)
                return FALSE;
        else
                return TRUE;
}

/**
 * polkit_session_set_ck_is_active:
 * @session: The session object
 * @is_active: whether ConsoleKit reports the session as active
 * 
 * Set whether ConsoleKit regard the session as active.
 *
 * Returns: #TRUE only if the value validated and was set
 **/
polkit_bool_t
polkit_session_set_ck_is_active (PolKitSession *session, polkit_bool_t is_active)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        session->is_active = is_active;
        return TRUE;
}

/**
 * polkit_session_set_ck_is_local:
 * @session: The session object
 * @is_local: whether ConsoleKit reports the session as local
 * 
 * Set whether ConsoleKit regard the session as local.
 *
 * Returns: #TRUE only if the value validated and was set
 **/
polkit_bool_t
polkit_session_set_ck_is_local (PolKitSession *session, polkit_bool_t is_local)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        session->is_local = is_local;
        return TRUE;
}

/**
 * polkit_session_set_ck_remote_host:
 * @session: The session object
 * @remote_host: hostname of the host/display that ConsoleKit reports
 * the session to occur at
 * 
 * Set the remote host/display that ConsoleKit reports the session to
 * occur at.
 *
 * Returns: #TRUE only if the value validated and was set
 **/
polkit_bool_t
polkit_session_set_ck_remote_host (PolKitSession *session, const char *remote_host)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        /* TODO: FIXME: probably need to allow a lot more here */
        kit_return_val_if_fail (_pk_validate_identifier (remote_host), FALSE);
        if (session->remote_host != NULL)
                kit_free (session->remote_host);
        session->remote_host = kit_strdup (remote_host);
        if (session->remote_host == NULL)
                return FALSE;
        else
                return TRUE;
}

/**
 * polkit_session_set_seat:
 * @session: The session object
 * @seat: a #PolKitSeat object
 * 
 * Set the seat that the session belongs to. The reference count on
 * the given object will be increased by one. If an existing seat
 * object was set already, the reference count on that one will be
 * decreased by one.
 *
 * Returns: #TRUE only if the value validated and was set
 **/
polkit_bool_t
polkit_session_set_seat (PolKitSession *session, PolKitSeat *seat)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        kit_return_val_if_fail (polkit_seat_validate (seat), FALSE);
        if (session->seat != NULL)
                polkit_seat_unref (session->seat);
        session->seat = seat != NULL ? polkit_seat_ref (seat) : NULL;
        return TRUE;
}

/**
 * polkit_session_get_uid:
 * @session: The session object
 * @out_uid: UNIX user id
 * 
 * Get the UNIX user id of the user owning the session.
 * 
 * Returns: TRUE iff the value is returned
 **/
polkit_bool_t
polkit_session_get_uid (PolKitSession *session, uid_t *out_uid)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        kit_return_val_if_fail (out_uid != NULL, FALSE);
        *out_uid = session->uid;
        return TRUE;
}

/**
 * polkit_session_get_ck_objref:
 * @session: The session object
 * @out_ck_objref: D-Bus object path. Shall not be freed by the caller.
 * 
 * Get the D-Bus object path to the ConsoleKit session object.
 * 
 * Returns: TRUE iff the value is returned
 **/
polkit_bool_t
polkit_session_get_ck_objref (PolKitSession *session, char **out_ck_objref)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        kit_return_val_if_fail (out_ck_objref != NULL, FALSE);
        *out_ck_objref = session->ck_objref;
        return TRUE;
}

/**
 * polkit_session_get_ck_is_active:
 * @session: The session object
 * @out_is_active: whether ConsoleKit reports the session as active
 * 
 * Get whether ConsoleKit regard the session as active.
 * 
 * Returns: TRUE iff the value is returned
 **/
polkit_bool_t
polkit_session_get_ck_is_active (PolKitSession *session, polkit_bool_t *out_is_active)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        kit_return_val_if_fail (out_is_active != NULL, FALSE);
        *out_is_active = session->is_active;
        return TRUE;
}

/**
 * polkit_session_get_ck_is_local:
 * @session: The session object
 * @out_is_local: whether ConsoleKit reports the session as local
 * 
 * Set whether ConsoleKit regard the session as local.
 * 
 * Returns: TRUE iff the value is returned
 **/
polkit_bool_t
polkit_session_get_ck_is_local (PolKitSession *session, polkit_bool_t *out_is_local)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        kit_return_val_if_fail (out_is_local != NULL, FALSE);
        *out_is_local = session->is_local;
        return TRUE;
}

/**
 * polkit_session_get_ck_remote_host:
 * @session: The session object
 * @out_remote_host: hostname of the host/display that ConsoleKit
 * reports the session to occur at. Shall not be freed by the caller.
 * 
 * Get the remote host/display that ConsoleKit reports the session to
 * occur at.
 * 
 * Returns: TRUE iff the value is returned
 **/
polkit_bool_t
polkit_session_get_ck_remote_host (PolKitSession *session, char **out_remote_host)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        kit_return_val_if_fail (out_remote_host != NULL, FALSE);
        *out_remote_host = session->remote_host;
        return TRUE;
}

/**
 * polkit_session_get_seat:
 * @session: The session object
 * @out_seat: Returns the seat the session belongs to. Shall not
 * be unreffed by the caller.
 * 
 * Get the seat that the session belongs to.
 * 
 * Returns: TRUE iff the value is returned
 **/
polkit_bool_t
polkit_session_get_seat (PolKitSession *session, PolKitSeat **out_seat)
{
        kit_return_val_if_fail (session != NULL, FALSE);
        kit_return_val_if_fail (out_seat != NULL, FALSE);
        *out_seat = session->seat;
        return TRUE;
}

/**
 * polkit_session_debug:
 * @session: the object
 * 
 * Print debug details
 **/
void
polkit_session_debug (PolKitSession *session)
{
        kit_return_if_fail (session != NULL);
        polkit_debug ("PolKitSession: refcount=%d uid=%d objpath=%s is_active=%d is_local=%d remote_host=%s", 
                      session->refcount, session->uid,
                      session->ck_objref, session->is_active, session->is_local, session->remote_host);
        if (session->seat != NULL)
                polkit_seat_debug (session->seat);
}


/**
 * polkit_session_validate:
 * @session: the object
 * 
 * Validate the object
 * 
 * Returns: #TRUE iff the object is valid.
 **/
polkit_bool_t
polkit_session_validate (PolKitSession *session)
{
        polkit_bool_t ret;
        kit_return_val_if_fail (session != NULL, FALSE);

        ret = FALSE;
        if (session->is_local) {
                if (session->remote_host != NULL)
                        goto error;
        } else {
                if (session->remote_host == NULL)
                        goto error;
        }
        ret = TRUE;
error:
        return ret;
}

#ifdef POLKIT_BUILD_TESTS

static polkit_bool_t
_run_test (void)
{
        char *str;
        PolKitSession *s;
        PolKitSeat *seat;
        PolKitSeat *seat2;
        uid_t uid;
        polkit_bool_t b;

        s = polkit_session_new ();
        if (s == NULL) {
                /* OOM */
        } else {
                if (! polkit_session_set_ck_objref (s, "/somesession")) {
                        /* OOM */
                } else {
                        kit_assert (polkit_session_get_ck_objref (s, &str) && strcmp (str, "/somesession") == 0);
                        polkit_session_ref (s);
                        polkit_session_unref (s);
                        polkit_session_debug (s);
                        if (! polkit_session_set_ck_objref (s, "/somesession2")) {
                                /* OOM */
                        } else {
                                kit_assert (polkit_session_get_ck_objref (s, &str) && strcmp (str, "/somesession2") == 0);
                        }

                        if ((seat = polkit_seat_new ()) != NULL) {
                                if (polkit_seat_set_ck_objref (seat, "/someseat")) {
                                        kit_assert (polkit_session_set_seat (s, seat));
                                        kit_assert (polkit_session_get_seat (s, &seat2) && seat == seat2);
                                }
                                polkit_seat_unref (seat);
                                if ((seat = polkit_seat_new ()) != NULL) {
                                        if (polkit_seat_set_ck_objref (seat, "/someseat2")) {
                                                kit_assert (polkit_session_set_seat (s, seat));
                                                kit_assert (polkit_session_get_seat (s, &seat2) && seat == seat2);
                                        }
                                        polkit_seat_unref (seat);
                                }
                        }

                        kit_assert (polkit_session_set_uid (s, 0));
                        kit_assert (polkit_session_get_uid (s, &uid) && uid == 0);
                        kit_assert (polkit_session_set_ck_is_active (s, TRUE));
                        kit_assert (polkit_session_get_ck_is_active (s, &b) && b == TRUE);
                        kit_assert (polkit_session_set_ck_is_local (s, TRUE));
                        kit_assert (polkit_session_get_ck_is_local (s, &b) && b == TRUE);
                        kit_assert (polkit_session_validate (s));

                        kit_assert (polkit_session_set_uid (s, 500));
                        kit_assert (polkit_session_get_uid (s, &uid) && uid == 500);
                        kit_assert (polkit_session_set_ck_is_active (s, FALSE));
                        kit_assert (polkit_session_get_ck_is_active (s, &b) && b == FALSE);
                        kit_assert (polkit_session_set_ck_is_local (s, FALSE));
                        kit_assert (polkit_session_get_ck_is_local (s, &b) && b == FALSE);

                        /* not valid because remote host is not set.. */
                        kit_assert (!polkit_session_validate (s));


                        if (polkit_session_set_ck_remote_host (s, "somehost.com")) {
                                kit_assert (polkit_session_get_ck_remote_host (s, &str) && strcmp (str, "somehost.com") == 0);
                                kit_assert (polkit_session_validate (s));

                                /* not valid because remote host is set and local==TRUE */
                                kit_assert (polkit_session_set_ck_is_local (s, TRUE));
                                kit_assert (!polkit_session_validate (s));
                                kit_assert (polkit_session_set_ck_is_local (s, FALSE));

                                if (polkit_session_set_ck_remote_host (s, "somehost2.com")) {
                                        kit_assert (polkit_session_get_ck_remote_host (s, &str) && strcmp (str, "somehost2.com") == 0);
                                        kit_assert (polkit_session_validate (s));
                                }
                                polkit_session_debug (s);
                        }

                }
                polkit_session_unref (s);
        }

        return TRUE;
}

KitTest _test_session = {
        "polkit_session",
        NULL,
        NULL,
        _run_test
};

#endif /* POLKIT_BUILD_TESTS */