|
root / base / usr / src / lib / policykit
policykit Plain Text 629 lines 18.0 KB
  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
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

include		../Makefile.lib
include		$(SRC)/lib/policykit/Makefile.policykit

SUBDIRS =	libpolkit
HDRSUBDIRS =	libpolkit

all :		TARGET= all
clean :	TARGET= clean
clobber :	TARGET= clobber
install :	TARGET= install
install_h :	TARGET= install_h

.KEEP_STATE:

all clean clobber install: $(SUBDIRS)

install_h: $(ROOTHDRDIR) $(ROOTHDRS) $(SUBDIRS)

#
# Don't check 3rd party headers.
#
check:

$(SUBDIRS):	FRC
	@cd $@; pwd; $(MAKE) $(TARGET)

FRC:

include ../Makefile.targ
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

VERSPKG = $(POLICYKIT_VERSION)
PCFILE = polkit.pc

include $(SRC)/lib/Makefile.lib
include $(SRC)/lib/policykit/Makefile.policykit

CPPFLAGS =	$(POLICYKIT_DBUS_CPPFLAGS) $(POLICYKIT_GLIB_CPPFLAGS) $(CPPFLAGS.master)

CERRWARN +=	-Wno-unused-variable
CERRWARN +=	-Wno-unused-function

CSTD =	$(CSTD_GNU99)

#
# Ensure `all' is the default target.
#
all:

#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Definitions common for PolicyKit
#

POLICYKIT_VERSION = 0.2

POLICYKIT_DBUS_CPPFLAGS =	-DDBUS_API_SUBJECT_TO_CHANGE \
				-I$(ADJUNCT_PROTO)/usr/include/dbus-1.0 \
				-I$(ADJUNCT_PROTO)/usr/lib/dbus-1.0/include
POLICYKIT_GLIB_CPPFLAGS =	-I$(ADJUNCT_PROTO)/usr/include/glib-2.0 \
				-I$(ADJUNCT_PROTO)/usr/lib/glib-2.0/include

POLICYKIT_DBUS_LDLIBS =	-ldbus-1
POLICYKIT_GLIB_LDLIBS =	-lglib-2.0
NATIVE_LIBS +=		libglib-2.0.so
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"%Z%%M%	%I%	%E% SMI"

include	../../Makefile.lib
include ../Makefile.com

HDRS =		libpolkit.h
HDRDIR =	common
ROOTHDRDIR =	$(ROOT)/usr/include/libpolkit

# Hammerhead: amd64-only
SUBDIRS =	$(MACH64)

all :		TARGET= all
clean :	TARGET= clean
clobber :	TARGET= clobber
install :	TARGET= install

.KEEP_STATE:

all clean clobber install: $(SUBDIRS)

install_h:	$(ROOTHDRS)

$(SUBDIRS): FRC
	@cd $@; pwd; $(MAKE) $(TARGET)

FRC:

include $(SRC)/lib/Makefile.targ
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Copyright 2019 OmniOS Community Edition (OmniOSce) Association.

LIBRARY =	libpolkit.a
VERS =		.0.0.0
VERS_MAJ =	.0
VERSPKG =	$(POLICYKIT_VERSION)
OBJECTS =	libpolkit-rbac.o
PCFILE =	polkit.pc

include ../../Makefile.com

LIBS =		$(DYNLIB)
LDLIBS +=	$(POLICYKIT_GLIB_LDLIBS)
LDLIBS +=	-lc -lsecdb

SRCDIR =	../common

CFLAGS +=	$(CCVERBOSE)
CPPFLAGS +=	-DPACKAGE_LOCALE_DIR=\"/usr/lib/locale\"

ROOTMAJLINK =	$(ROOTLIBDIR)/$(LIBRARY:.a=.so)$(VERS_MAJ)

# smatch has problems parsing the glib header files
SMATCH=off

.KEEP_STATE:

all:		$(LIBS)

$(ROOTMAJLINK):
	-$(RM) $@; $(SYMLINK) $(DYNLIB) $@

include $(SRC)/lib/Makefile.targ
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source. A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
# Copyright 2025 Hammerhead Project
#

include ../Makefile.com
include $(SRC)/lib/Makefile.lib.64

install: all $(ROOTLIBS64) $(ROOTLINKS64)
/***************************************************************************
 *
 * libpolkit-rbac.c : RBAC implementation of the libpolkit API
 *
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Licensed under the Academic Free License version 2.1
 *
 **************************************************************************/

#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 <auth_attr.h>
#include <secdb.h>

#include <glib.h>
#include <dbus/dbus-glib.h>

#include "libpolkit.h"

#define LIBPOLKIT_MAGIC 0x3117beef

#ifdef __SUNPRO_C
#define __FUNCTION__ __func__
#endif

#define LIBPOLKIT_CHECK_CONTEXT(_ctx_, _ret_)				\
	do {									\
		if (_ctx_ == NULL) {						\
			g_warning ("%s: given LibPolKitContext is NULL",     \
				   __FUNCTION__);			        \
			return _ret_;					        \
		}								\
		if (_ctx_->magic != LIBPOLKIT_MAGIC) {			\
			g_warning ("%s: given LibPolKitContext is invalid (read magic 0x%08x, should be 0x%08x)",  \
				   __FUNCTION__, _ctx_->magic, LIBPOLKIT_MAGIC);	\
			return _ret_;					        \
		}								\
	} while(0)


struct LibPolKitContext_s
{
	guint32 magic;
};

/** Get a new context.
 *
 *  @return                     Pointer to new context or NULL if an error occured
 */
LibPolKitContext *
libpolkit_new_context (DBusConnection *connection)
{
	LibPolKitContext *ctx;

	ctx = g_new0 (LibPolKitContext, 1);
	ctx->magic = LIBPOLKIT_MAGIC;

	return ctx;
}

/** Free a context
 *
 *  @param  ctx                 The context obtained from libpolkit_new_context
 *  @return                     Pointer to new context or NULL if an error occured
 */
gboolean
libpolkit_free_context (LibPolKitContext *ctx)
{
	LIBPOLKIT_CHECK_CONTEXT (ctx, FALSE);

	ctx->magic = 0;
	g_free (ctx);
	return TRUE;
}

LibPolKitResult
libpolkit_get_allowed_resources_for_privilege_for_uid (LibPolKitContext    *ctx,
						       const char          *user,
						       const char          *privilege,
						       GList              **resources,
						       GList              **restrictions,
						       int                 *num_non_temporary)
{
	LibPolKitResult res;
	char **resource_list;
	int num_resources;
	char **restriction_list;
	int num_restrictions;

	LIBPOLKIT_CHECK_CONTEXT (ctx, LIBPOLKIT_RESULT_INVALID_CONTEXT);

	res = LIBPOLKIT_RESULT_ERROR;
	*resources = NULL;
	*restrictions = NULL;

	res = LIBPOLKIT_RESULT_OK;

	return res;
}

LibPolKitResult
libpolkit_is_uid_allowed_for_privilege (LibPolKitContext   *ctx,
					const char         *system_bus_unique_name,
					const char         *user,
					const char         *privilege,
					const char         *resource,
					gboolean           *out_is_allowed,
					gboolean           *out_is_temporary,
					char              **out_is_privileged_but_restricted_to_system_bus_unique_name)
{
	LibPolKitResult res;
	const char *myresource = "";
	const char *mysystem_bus_unique_name = "";
	char *but_restricted_to = NULL;
	uid_t uid;
	struct passwd *pw;
	char *authname;
	int i;
	gboolean authname_free = FALSE;

	LIBPOLKIT_CHECK_CONTEXT (ctx, LIBPOLKIT_RESULT_INVALID_CONTEXT);

	uid = (uid_t)atol (user);
	if ((pw = getpwuid (uid)) == NULL) {
		*out_is_allowed = FALSE;
		*out_is_temporary = FALSE;
		return LIBPOLKIT_RESULT_NO_SUCH_USER;
	}

	/* map PolicyKit privilege to RBAC authorization */
	if (strcmp (privilege, "hal-storage-removable-mount") == 0) {
		authname = "solaris.device.mount.removable";
	} else if (strcmp (privilege, "hal-storage-removable-mount-all-options") == 0) {
		authname = "solaris.device.mount.alloptions.removable";
	} else if (strcmp (privilege, "hal-storage-fixed-mount") == 0) {
		authname = "solaris.device.mount.fixed";
	} else if (strcmp (privilege, "hal-storage-fixed-mount-all-options") == 0) {
		authname = "solaris.device.mount.alloptions.fixed";
	} else if (strcmp(privilege, "hal-power-suspend") == 0) {
		authname = "solaris.system.power.suspend.ram";
	} else if (strcmp(privilege, "hal-power-hibernate") == 0) {
                authname = "solaris.system.power.suspend.disk";
	} else if ((strcmp(privilege, "hal-power-shutdown") == 0) ||
	    (strcmp(privilege, "hal-power-reboot") == 0)) {
                authname = "solaris.system.shutdown";
	} else if (strcmp(privilege, "hal-power-cpu") == 0) {
                authname = "solaris.system.power.cpu";
	} else if (strcmp(privilege, "hal-power-brightness") == 0) {
                authname = "solaris.system.power.brightness";
	} else if (strcmp (privilege, "hal-power-cpu") == 0) {
		authname = "solaris.system.power.cpu";
	} else {
		/* replace '-' with '.' */
		authname = g_strdup (privilege);
		authname_free = TRUE;
		for (i = 0; i < strlen (authname); i++) {
			if (authname[i] == '-') {
				authname[i] = '.';
			}
		}
	}

	*out_is_allowed = (chkauthattr(authname, pw->pw_name) != 0);
	*out_is_temporary = FALSE;

	if (authname_free) {
		g_free(authname);
	}

	return LIBPOLKIT_RESULT_OK;
}

LibPolKitResult
libpolkit_get_privilege_list (LibPolKitContext      *ctx,
			      GList                **result)
{
	LibPolKitResult res;
	char **privilege_list;
	int num_privileges = 0;
	int i;

	LIBPOLKIT_CHECK_CONTEXT (ctx, LIBPOLKIT_RESULT_INVALID_CONTEXT);

	*result = NULL;

	for (i = 0; i < num_privileges; i++) {
		*result = g_list_append (*result, g_strdup (privilege_list[i]));
	}

	res = LIBPOLKIT_RESULT_OK;

	return res;
}

LibPolKitResult
libpolkit_revoke_temporary_privilege (LibPolKitContext      *ctx,
                                      const char            *user,
                                      const char            *privilege,
                                      const char            *resource,
                                      gboolean              *result)
{
	return LIBPOLKIT_RESULT_OK;
}
/***************************************************************************
 *
 * libpolkit.h : Wraps a subset of methods on the PolicyKit daemon
 *
 * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
 *
 * Licensed under the Academic Free License version 2.1
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 **************************************************************************/

#ifndef LIBPOLKIT_H
#define LIBPOLKIT_H

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <glib.h>
#include <dbus/dbus.h>

typedef enum {
	LIBPOLKIT_RESULT_OK,
	LIBPOLKIT_RESULT_ERROR,
	LIBPOLKIT_RESULT_INVALID_CONTEXT,
	LIBPOLKIT_RESULT_NOT_PRIVILEGED,
	LIBPOLKIT_RESULT_NO_SUCH_PRIVILEGE,
	LIBPOLKIT_RESULT_NO_SUCH_USER
} LibPolKitResult;

struct LibPolKitContext_s;
typedef struct LibPolKitContext_s LibPolKitContext;

LibPolKitContext  *libpolkit_new_context                              (DBusConnection        *connection);

gboolean           libpolkit_free_context                             (LibPolKitContext      *ctx);

LibPolKitResult    libpolkit_get_privilege_list                       (LibPolKitContext      *ctx,
								       GList                **result);

LibPolKitResult    libpolkit_is_uid_allowed_for_privilege             (LibPolKitContext      *ctx,
								       const char            *system_bus_unique_name,
								       const char            *user,
								       const char            *privilege,
								       const char            *resource,
								       gboolean              *out_is_allowed,
								       gboolean              *out_is_temporary,
								       char                 **out_is_privileged_but_restricted_to_system_bus_unique_name);

LibPolKitResult    libpolkit_revoke_temporary_privilege               (LibPolKitContext      *ctx,
								       const char            *user,
								       const char            *privilege,
								       const char            *resource,
								       gboolean              *result);

LibPolKitResult    libpolkit_get_allowed_resources_for_privilege_for_uid (LibPolKitContext      *ctx,
									  const char            *user,
									  const char            *privilege,
									  GList                **resources,
									  GList                **restrictions,
									  int                   *num_non_temporary);

#endif /* LIBPOLKIT_H */


#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
#

#
# MAPFILE HEADER START
#
# WARNING:  STOP NOW.  DO NOT MODIFY THIS FILE.
# Object versioning must comply with the rules detailed in
#
#	usr/src/lib/README.mapfiles
#
# You should not be making modifications here until you've read the most current
# copy of that file. If you need help, contact a gatekeeper for guidance.
#
# MAPFILE HEADER END
#

$mapfile_version 2

SYMBOL_VERSION SUNW_1.1 {
    global:
	libpolkit_new_context;
	libpolkit_free_context;
	libpolkit_get_privilege_list;
	libpolkit_is_uid_allowed_for_privilege;
	libpolkit_revoke_temporary_privilege;
	libpolkit_get_allowed_resources_for_privilege_for_uid;
};

SYMBOL_VERSION SUNWprivate_1.1 {
    global:
	SUNWprivate_1.1;
    local:
	*;
};
#
# Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# Licensed under the Academic Free License version 2.1
#

prefix=/usr
exec_prefix=${prefix}
libdir=${exec_prefix}/__LIBDIR__
includedir=${prefix}/include

Name: libpolkit
Description: library for querying and setting system-wide policy
Version: __VERSPKG__
Requires: glib-2.0
Libs: -L${libdir} -lpolkit
Cflags: -I${includedir}/libpolkit