summaryrefslogtreecommitdiff
path: root/usr/src/cmd/policykit/polkit-is-privileged.c
blob: 85116cf3d0e28d75a50562bb82e8dfc9bbd3222c (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
/***************************************************************************
 * CVSID: $Id$
 *
 * polkit-is-privileged.c : Determine if a user has privileges
 *
 * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
 *
 * 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
 *
 **************************************************************************/


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

#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <dbus/dbus.h>

#include <libpolkit/libpolkit.h>

static void
usage (int argc, char *argv[])
{
	fprintf (stderr, "polkit-is-privileged version " PACKAGE_VERSION "\n");

	fprintf (stderr, 
		 "\n" 
		 "usage : %s -u <uid> -p <privilege> [-r <resource>]\n" 
		 "        [-s <system-bus-connection-name>]", argv[0]);
	fprintf (stderr,
		 "\n"
		 "Options:\n"
		 "    -u, --user                    Username or user id\n"
		 "    -s, --system-bus-unique-name  Unique system bus connection name\n"
		 "    -r, --resource                Resource\n"
		 "    -p, --privilege               Privilege to test for\n"
		 "    -h, --help                    Show this information and exit\n"
		 "    -v, --verbose                 Verbose operation\n"
		 "    -V, --version                 Print version number\n"
		 "\n"
		 "Queries system policy whether a given user is allowed for a given\n"
		 "privilege for a given resource. The resource may be omitted.\n"
		 "\n");
}

int 
main (int argc, char *argv[])
{
	int rc;
	char *user = NULL;
	char *privilege = NULL;
	char *resource = NULL;
	char *system_bus_unique_name = NULL;
	static const struct option long_options[] = {
		{"user", required_argument, NULL, 'u'},
		{"system-bus-unique-name", required_argument, NULL, 's'},
		{"resource", required_argument, NULL, 'r'},
		{"privilege", required_argument, NULL, 'p'},
		{"help", no_argument, NULL, 'h'},
		{"verbose", no_argument, NULL, 'v'},
		{"version", no_argument, NULL, 'V'},
		{NULL, 0, NULL, 0}
	};
	LibPolKitContext *ctx = NULL;
	gboolean is_allowed;
	gboolean is_temporary;
	LibPolKitResult result;
	gboolean is_verbose = FALSE;
	DBusError error;
	DBusConnection *connection = NULL;

	rc = 1;
	
	while (TRUE) {
		int c;
		
		c = getopt_long (argc, argv, "u:r:p:s:hVv", long_options, NULL);

		if (c == -1)
			break;
		
		switch (c) {
		case 's':
			system_bus_unique_name = g_strdup (optarg);
			break;

		case 'u':
			user = g_strdup (optarg);
			break;
			
		case 'r':
			resource = g_strdup (optarg);
			break;
			
		case 'p':
			privilege = g_strdup (optarg);
			break;
			
		case 'v':
			is_verbose = TRUE;
			break;

		case 'h':
			usage (argc, argv);
			rc = 0;
			goto out;

		case 'V':
			printf ("polkit-is-privileged version " PACKAGE_VERSION "\n");
			rc = 0;
			goto out;
			
		default:
			usage (argc, argv);
			goto out;
		}
	}

	if (user == NULL || privilege == NULL) {
		usage (argc, argv);
		return 1;
	}

	if (is_verbose) {
		printf ("user      = '%s'\n", user);
		printf ("privilege = '%s'\n", privilege);
		if (resource != NULL)
			printf ("resource  = '%s'\n", resource);
	}

#ifdef POLKITD_ENABLED
	dbus_error_init (&error);
	connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
	if (connection == NULL) {
		g_warning ("Cannot connect to system message bus");
		return 1;
	}
#endif /* POLKITD_ENABLED */

	ctx = libpolkit_new_context (connection);
	if (ctx == NULL) {
		g_warning ("Cannot get libpolkit context");
		goto out;
	}

	result = libpolkit_is_uid_allowed_for_privilege (ctx, 
							 system_bus_unique_name,
							 user,
							 privilege,
							 resource,
							 &is_allowed,
							 &is_temporary,
							 NULL);
	switch (result) {
	case LIBPOLKIT_RESULT_OK:
		rc = is_allowed ? 0 : 1;
		break;

	case LIBPOLKIT_RESULT_ERROR:
		g_warning ("Error determing whether user is privileged.");
		break;

	case LIBPOLKIT_RESULT_INVALID_CONTEXT:
		g_print ("Invalid context.\n");
		goto out;

	case LIBPOLKIT_RESULT_NOT_PRIVILEGED:
		g_print ("Not privileged.\n");

	case LIBPOLKIT_RESULT_NO_SUCH_PRIVILEGE:
		g_print ("No such privilege '%s'.\n", privilege);
		goto out;

	case LIBPOLKIT_RESULT_NO_SUCH_USER:
		g_print ("No such user '%s'.\n", user);
		goto out;
	}

	if (is_verbose) {
		printf ("result %d\n", result);
		printf ("is_allowed %d\n", is_allowed);
	}

out:
	if (ctx != NULL)
		libpolkit_free_context (ctx);

	return rc;
}