1 /***************************************************************************
4 * polkit-is-privileged.c : Determine if a user has privileges
6 * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 **************************************************************************/
32 #include <dbus/dbus.h>
34 #include <libpolkit/libpolkit.h>
37 usage (int argc
, char *argv
[])
39 fprintf (stderr
, "polkit-is-privileged version " PACKAGE_VERSION
"\n");
43 "usage : %s -u <uid> -p <privilege> [-r <resource>]\n"
44 " [-s <system-bus-connection-name>]", argv
[0]);
48 " -u, --user Username or user id\n"
49 " -s, --system-bus-unique-name Unique system bus connection name\n"
50 " -r, --resource Resource\n"
51 " -p, --privilege Privilege to test for\n"
52 " -h, --help Show this information and exit\n"
53 " -v, --verbose Verbose operation\n"
54 " -V, --version Print version number\n"
56 "Queries system policy whether a given user is allowed for a given\n"
57 "privilege for a given resource. The resource may be omitted.\n"
62 main (int argc
, char *argv
[])
66 char *privilege
= NULL
;
67 char *resource
= NULL
;
68 char *system_bus_unique_name
= NULL
;
69 static const struct option long_options
[] = {
70 {"user", required_argument
, NULL
, 'u'},
71 {"system-bus-unique-name", required_argument
, NULL
, 's'},
72 {"resource", required_argument
, NULL
, 'r'},
73 {"privilege", required_argument
, NULL
, 'p'},
74 {"help", no_argument
, NULL
, 'h'},
75 {"verbose", no_argument
, NULL
, 'v'},
76 {"version", no_argument
, NULL
, 'V'},
79 LibPolKitContext
*ctx
= NULL
;
81 gboolean is_temporary
;
82 LibPolKitResult result
;
83 gboolean is_verbose
= FALSE
;
85 DBusConnection
*connection
= NULL
;
92 c
= getopt_long (argc
, argv
, "u:r:p:s:hVv", long_options
, NULL
);
99 system_bus_unique_name
= g_strdup (optarg
);
103 user
= g_strdup (optarg
);
107 resource
= g_strdup (optarg
);
111 privilege
= g_strdup (optarg
);
124 printf ("polkit-is-privileged version " PACKAGE_VERSION
"\n");
134 if (user
== NULL
|| privilege
== NULL
) {
140 printf ("user = '%s'\n", user
);
141 printf ("privilege = '%s'\n", privilege
);
142 if (resource
!= NULL
)
143 printf ("resource = '%s'\n", resource
);
146 #ifdef POLKITD_ENABLED
147 dbus_error_init (&error
);
148 connection
= dbus_bus_get (DBUS_BUS_SYSTEM
, &error
);
149 if (connection
== NULL
) {
150 g_warning ("Cannot connect to system message bus");
153 #endif /* POLKITD_ENABLED */
155 ctx
= libpolkit_new_context (connection
);
157 g_warning ("Cannot get libpolkit context");
161 result
= libpolkit_is_uid_allowed_for_privilege (ctx
,
162 system_bus_unique_name
,
170 case LIBPOLKIT_RESULT_OK
:
171 rc
= is_allowed
? 0 : 1;
174 case LIBPOLKIT_RESULT_ERROR
:
175 g_warning ("Error determing whether user is privileged.");
178 case LIBPOLKIT_RESULT_INVALID_CONTEXT
:
179 g_print ("Invalid context.\n");
182 case LIBPOLKIT_RESULT_NOT_PRIVILEGED
:
183 g_print ("Not privileged.\n");
185 case LIBPOLKIT_RESULT_NO_SUCH_PRIVILEGE
:
186 g_print ("No such privilege '%s'.\n", privilege
);
189 case LIBPOLKIT_RESULT_NO_SUCH_USER
:
190 g_print ("No such user '%s'.\n", user
);
195 printf ("result %d\n", result
);
196 printf ("is_allowed %d\n", is_allowed
);
201 libpolkit_free_context (ctx
);