1 /*
2  * Copyright (C) 2009 Red Hat, Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General
15  * Public License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17  * Boston, MA 02111-1307, USA.
18  *
19  * Author: David Zeuthen <davidz@redhat.com>
20  */
21 
22 #ifdef HAVE_CONFIG_H
23 #  include "config.h"
24 #endif
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <glib/gi18n.h>
29 #include <polkit/polkit.h>
30 
31 static void
print_action(PolkitActionDescription * action,gboolean opt_verbose)32 print_action (PolkitActionDescription *action,
33               gboolean                 opt_verbose)
34 {
35 
36   if (!opt_verbose)
37     {
38       g_print ("%s\n", polkit_action_description_get_action_id (action));
39     }
40   else
41     {
42       const gchar *vendor;
43       const gchar *vendor_url;
44       const gchar *icon_name;
45       const gchar* const *annotation_keys;
46       guint n;
47 
48       vendor = polkit_action_description_get_vendor_name (action);
49       vendor_url = polkit_action_description_get_vendor_url (action);
50       icon_name = polkit_action_description_get_icon_name (action);
51 
52       g_print ("%s:\n", polkit_action_description_get_action_id (action));
53       g_print ("  description:       %s\n", polkit_action_description_get_description (action));
54       g_print ("  message:           %s\n", polkit_action_description_get_message (action));
55       if (vendor != NULL)
56         g_print ("  vendor:            %s\n", vendor);
57       if (vendor_url != NULL)
58         g_print ("  vendor_url:        %s\n", vendor_url);
59 
60       if (icon_name != NULL)
61         g_print ("  icon:              %s\n", icon_name);
62 
63       g_print ("  implicit any:      %s\n", polkit_implicit_authorization_to_string (polkit_action_description_get_implicit_any (action)));
64       g_print ("  implicit inactive: %s\n", polkit_implicit_authorization_to_string (polkit_action_description_get_implicit_inactive (action)));
65       g_print ("  implicit active:   %s\n", polkit_implicit_authorization_to_string (polkit_action_description_get_implicit_active (action)));
66 
67       annotation_keys = polkit_action_description_get_annotation_keys (action);
68       for (n = 0; annotation_keys[n] != NULL; n++)
69         {
70           const gchar *key;
71           const gchar *value;
72 
73           key = annotation_keys[n];
74           value = polkit_action_description_get_annotation (action, key);
75           g_print ("  annotation:        %s -> %s\n", key, value);
76         }
77       g_print ("\n");
78     }
79 }
80 
81 static gint
action_desc_compare_by_action_id_func(PolkitActionDescription * a,PolkitActionDescription * b)82 action_desc_compare_by_action_id_func (PolkitActionDescription *a,
83                                        PolkitActionDescription *b)
84 {
85   return g_strcmp0 (polkit_action_description_get_action_id (a),
86                     polkit_action_description_get_action_id (b));
87 }
88 
89 int
main(int argc,char * argv[])90 main (int argc, char *argv[])
91 {
92   guint ret;
93   gchar *opt_action_id;
94   gchar *s;
95   gboolean opt_show_version;
96   gboolean opt_verbose;
97   GOptionEntry options[] =
98     {
99       {
100 	"action-id", 'a', 0, G_OPTION_ARG_STRING, &opt_action_id,
101 	N_("Only output information about ACTION"), N_("ACTION")
102       },
103       {
104 	"verbose", 'v', 0, G_OPTION_ARG_NONE, &opt_verbose,
105 	N_("Output detailed action information"), NULL
106       },
107       {
108 	"version", 0, 0, G_OPTION_ARG_NONE, &opt_show_version,
109 	N_("Show version"), NULL
110       },
111       { NULL, 0, 0, 0, NULL, NULL, NULL }
112     };
113   GOptionContext *context;
114   PolkitAuthority *authority;
115   GList *l;
116   GList *actions;
117   GError *error;
118 
119   opt_action_id = NULL;
120   context = NULL;
121   authority = NULL;
122   actions = NULL;
123   ret = 1;
124 
125   /* Disable remote file access from GIO. */
126   setenv ("GIO_USE_VFS", "local", 1);
127 
128   opt_show_version = FALSE;
129   opt_verbose = FALSE;
130 
131   error = NULL;
132   context = g_option_context_new (N_("[--action-id ACTION]"));
133   s = g_strdup_printf (_("Report bugs to: %s\n"
134 			 "%s home page: <%s>"), PACKAGE_BUGREPORT,
135 		       PACKAGE_NAME, PACKAGE_URL);
136   g_option_context_set_description (context, s);
137   g_free (s);
138   g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
139   if (!g_option_context_parse (context, &argc, &argv, &error))
140     {
141       g_printerr ("%s: %s\n", g_get_prgname (), error->message);
142       g_error_free (error);
143       goto out;
144     }
145   if (argc > 1)
146     {
147       g_printerr (_("%s: Unexpected argument `%s'\n"), g_get_prgname (),
148 		  argv[1]);
149       goto out;
150     }
151   if (opt_show_version)
152     {
153       g_print ("pkaction version %s\n", PACKAGE_VERSION);
154       ret = 0;
155       goto out;
156     }
157 
158   authority = polkit_authority_get_sync (NULL /* GCancellable* */, &error);
159   if (authority == NULL)
160     {
161       g_printerr ("Error getting authority: %s\n", error->message);
162       g_error_free (error);
163       goto out;
164     }
165 
166   error = NULL;
167   actions = polkit_authority_enumerate_actions_sync (authority,
168                                                      NULL,      /* GCancellable */
169                                                      &error);
170   if (error != NULL)
171     {
172       g_printerr ("Error enumerating actions: %s\n", error->message);
173       g_error_free (error);
174       goto out;
175     }
176 
177   if (opt_action_id != NULL)
178     {
179       for (l = actions; l != NULL; l = l->next)
180         {
181           PolkitActionDescription *action = POLKIT_ACTION_DESCRIPTION (l->data);
182           const gchar *id;
183 
184           id = polkit_action_description_get_action_id (action);
185 
186           if (g_strcmp0 (id, opt_action_id) == 0)
187             {
188               print_action (action, opt_verbose);
189               break;
190             }
191         }
192 
193       if (l == NULL)
194         {
195           g_printerr ("No action with action id %s\n", opt_action_id);
196           goto out;
197         }
198     }
199   else
200     {
201       actions = g_list_sort (actions,
202                              (GCompareFunc) action_desc_compare_by_action_id_func);
203 
204       for (l = actions; l != NULL; l = l->next)
205         {
206           PolkitActionDescription *action = POLKIT_ACTION_DESCRIPTION (l->data);
207 
208           print_action (action, opt_verbose);
209         }
210     }
211 
212   ret = 0;
213 
214  out:
215   g_list_foreach (actions, (GFunc) g_object_unref, NULL);
216   g_list_free (actions);
217 
218   g_free (opt_action_id);
219 
220   if (authority != NULL)
221     g_object_unref (authority);
222 
223   g_option_context_free (context);
224 
225   return ret;
226 }
227 
228