Print this page
10136 smatch fix for policykit
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/policykit/polkit-is-privileged.c
+++ new/usr/src/cmd/policykit/polkit-is-privileged.c
1 1 /***************************************************************************
2 2 * CVSID: $Id$
3 3 *
4 4 * polkit-is-privileged.c : Determine if a user has privileges
5 5 *
6 6 * Copyright (C) 2006 David Zeuthen, <david@fubar.dk>
7 7 *
8 8 * This program is free software; you can redistribute it and/or modify
9 9 * it under the terms of the GNU General Public License as published by
10 10 * the Free Software Foundation; either version 2 of the License, or
11 11 * (at your option) any later version.
12 12 *
13 13 * This program is distributed in the hope that it will be useful,
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 16 * GNU General Public License for more details.
17 17 *
18 18 * You should have received a copy of the GNU General Public License
19 19 * along with this program; if not, write to the Free Software
20 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 21 *
22 22 **************************************************************************/
23 23
24 +/*
25 + * Copyright (c) 2018, Joyent, Inc.
26 + */
24 27
25 28 #ifdef HAVE_CONFIG_H
26 29 # include <config.h>
27 30 #endif
28 31
29 32 #include <stdio.h>
30 33 #include <stdlib.h>
31 34 #include <getopt.h>
32 35 #include <dbus/dbus.h>
33 36
34 37 #include <libpolkit/libpolkit.h>
35 38
36 39 static void
37 40 usage (int argc, char *argv[])
38 41 {
39 42 fprintf (stderr, "polkit-is-privileged version " PACKAGE_VERSION "\n");
40 43
41 44 fprintf (stderr,
42 45 "\n"
43 46 "usage : %s -u <uid> -p <privilege> [-r <resource>]\n"
44 47 " [-s <system-bus-connection-name>]", argv[0]);
45 48 fprintf (stderr,
46 49 "\n"
47 50 "Options:\n"
48 51 " -u, --user Username or user id\n"
49 52 " -s, --system-bus-unique-name Unique system bus connection name\n"
50 53 " -r, --resource Resource\n"
51 54 " -p, --privilege Privilege to test for\n"
52 55 " -h, --help Show this information and exit\n"
53 56 " -v, --verbose Verbose operation\n"
54 57 " -V, --version Print version number\n"
55 58 "\n"
56 59 "Queries system policy whether a given user is allowed for a given\n"
57 60 "privilege for a given resource. The resource may be omitted.\n"
58 61 "\n");
59 62 }
60 63
61 64 int
62 65 main (int argc, char *argv[])
63 66 {
64 67 int rc;
65 68 char *user = NULL;
66 69 char *privilege = NULL;
67 70 char *resource = NULL;
68 71 char *system_bus_unique_name = NULL;
69 72 static const struct option long_options[] = {
70 73 {"user", required_argument, NULL, 'u'},
71 74 {"system-bus-unique-name", required_argument, NULL, 's'},
72 75 {"resource", required_argument, NULL, 'r'},
73 76 {"privilege", required_argument, NULL, 'p'},
74 77 {"help", no_argument, NULL, 'h'},
75 78 {"verbose", no_argument, NULL, 'v'},
76 79 {"version", no_argument, NULL, 'V'},
77 80 {NULL, 0, NULL, 0}
78 81 };
79 82 LibPolKitContext *ctx = NULL;
80 83 gboolean is_allowed;
81 84 gboolean is_temporary;
82 85 LibPolKitResult result;
83 86 gboolean is_verbose = FALSE;
84 87 DBusError error;
85 88 DBusConnection *connection = NULL;
86 89
87 90 rc = 1;
88 91
89 92 while (TRUE) {
90 93 int c;
91 94
92 95 c = getopt_long (argc, argv, "u:r:p:s:hVv", long_options, NULL);
93 96
94 97 if (c == -1)
95 98 break;
96 99
97 100 switch (c) {
98 101 case 's':
99 102 system_bus_unique_name = g_strdup (optarg);
100 103 break;
101 104
102 105 case 'u':
103 106 user = g_strdup (optarg);
104 107 break;
105 108
106 109 case 'r':
107 110 resource = g_strdup (optarg);
108 111 break;
109 112
110 113 case 'p':
111 114 privilege = g_strdup (optarg);
112 115 break;
113 116
114 117 case 'v':
115 118 is_verbose = TRUE;
116 119 break;
117 120
118 121 case 'h':
119 122 usage (argc, argv);
120 123 rc = 0;
121 124 goto out;
122 125
123 126 case 'V':
124 127 printf ("polkit-is-privileged version " PACKAGE_VERSION "\n");
125 128 rc = 0;
126 129 goto out;
127 130
128 131 default:
129 132 usage (argc, argv);
130 133 goto out;
131 134 }
132 135 }
133 136
134 137 if (user == NULL || privilege == NULL) {
135 138 usage (argc, argv);
136 139 return 1;
137 140 }
138 141
139 142 if (is_verbose) {
140 143 printf ("user = '%s'\n", user);
141 144 printf ("privilege = '%s'\n", privilege);
142 145 if (resource != NULL)
143 146 printf ("resource = '%s'\n", resource);
144 147 }
145 148
146 149 #ifdef POLKITD_ENABLED
147 150 dbus_error_init (&error);
148 151 connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
149 152 if (connection == NULL) {
150 153 g_warning ("Cannot connect to system message bus");
151 154 return 1;
152 155 }
153 156 #endif /* POLKITD_ENABLED */
154 157
155 158 ctx = libpolkit_new_context (connection);
156 159 if (ctx == NULL) {
157 160 g_warning ("Cannot get libpolkit context");
158 161 goto out;
159 162 }
160 163
161 164 result = libpolkit_is_uid_allowed_for_privilege (ctx,
162 165 system_bus_unique_name,
163 166 user,
164 167 privilege,
165 168 resource,
166 169 &is_allowed,
167 170 &is_temporary,
168 171 NULL);
169 172 switch (result) {
170 173 case LIBPOLKIT_RESULT_OK:
171 174 rc = is_allowed ? 0 : 1;
172 175 break;
173 176
174 177 case LIBPOLKIT_RESULT_ERROR:
175 178 g_warning ("Error determing whether user is privileged.");
176 179 break;
177 180
178 181 case LIBPOLKIT_RESULT_INVALID_CONTEXT:
179 182 g_print ("Invalid context.\n");
180 183 goto out;
181 184
182 185 case LIBPOLKIT_RESULT_NOT_PRIVILEGED:
183 186 g_print ("Not privileged.\n");
184 187 goto out;
185 188
186 189 case LIBPOLKIT_RESULT_NO_SUCH_PRIVILEGE:
187 190 g_print ("No such privilege '%s'.\n", privilege);
188 191 goto out;
189 192
190 193 case LIBPOLKIT_RESULT_NO_SUCH_USER:
191 194 g_print ("No such user '%s'.\n", user);
↓ open down ↓ |
158 lines elided |
↑ open up ↑ |
192 195 goto out;
193 196 }
194 197
195 198 if (is_verbose) {
196 199 printf ("result %d\n", result);
197 200 printf ("is_allowed %d\n", is_allowed);
198 201 }
199 202
200 203 out:
201 204 if (ctx != NULL)
202 - libpolkit_free_context (ctx);
205 + (void) libpolkit_free_context (ctx);
203 206
204 207 return rc;
205 208 }
206 209
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX