6b3be69abda3a7b3fbd2a1bd6c0fb50c03c21257
[oweals/gnunet.git] / src / identity / gnunet-identity.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file identity/gnunet-identity.c
22  * @brief IDENTITY management command line tool
23  * @author Christian Grothoff
24  *
25  * Todo:
26  * - add options to get/set default egos
27  * - print short hashes of egos when printing
28  */
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_identity_service.h"
32
33 /**
34  * Handle to IDENTITY service.
35  */
36 static struct GNUNET_IDENTITY_Handle *sh;
37
38 /**
39  * Was "list" specified?
40  */
41 static int list;
42
43 /**
44  * Was "monitor" specified?
45  */
46 static int monitor;
47
48 /**
49  * -C option
50  */
51 static char *create_ego;
52
53 /**
54  * -D option
55  */
56 static char *delete_ego;
57
58 /**
59  * Handle for create operation.
60  */
61 static struct GNUNET_IDENTITY_Operation *create_op;
62
63 /**
64  * Handle for delete operation.
65  */
66 static struct GNUNET_IDENTITY_Operation *delete_op;
67
68
69 /**
70  * Task run on shutdown.
71  *
72  * @param cls NULL
73  * @param tc unused
74  */
75 static void
76 shutdown_task (void *cls,
77                const struct GNUNET_SCHEDULER_TaskContext *tc)
78 {
79   GNUNET_IDENTITY_disconnect (sh);
80   sh = NULL;
81 }
82
83
84
85 /**
86  * Test if we are finished yet.
87  */
88 static void
89 test_finished ()
90 {
91   if ( (NULL == create_op) &&
92        (NULL == delete_op) &&
93        (! list) &&
94        (! monitor) )  
95     GNUNET_SCHEDULER_shutdown ();
96 }
97
98
99 /**
100  * Deletion operation finished.
101  *
102  * @param cls pointer to operation handle
103  * @param emsg NULL on success, otherwise an error message
104  */
105 static void
106 delete_finished (void *cls,
107                  const char *emsg)
108 {
109   struct GNUNET_IDENTITY_Operation **op = cls;
110
111   *op = NULL;
112   if (NULL != emsg)
113     fprintf (stderr,
114              "%s\n",
115              gettext (emsg));
116   test_finished ();
117 }
118
119
120 /**
121  * Creation operation finished.
122  *
123  * @param cls pointer to operation handle
124  * @param ego ego handle
125  * @param ctx context for application to store data for this ego
126  *                 (during the lifetime of this process, initially NULL)
127  * @param identifier identifier assigned by the user for this ego
128  */
129 static void
130 create_finished (void *cls,
131                  const char *emsg)
132 {
133   struct GNUNET_IDENTITY_Operation **op = cls;
134
135   *op = NULL;
136   if (NULL != emsg)
137     fprintf (stderr,
138              _("Failed to create ego: %s\n"),
139              emsg);
140   test_finished ();
141 }
142
143
144 /**
145  * If listing is enabled, prints information about the egos.
146  *
147  * This function is initially called for all egos and then again
148  * whenever a ego's identifier changes or if it is deleted.  At the
149  * end of the initial pass over all egos, the function is once called
150  * with 'NULL' for 'ego'. That does NOT mean that the callback won't
151  * be invoked in the future or that there was an error.
152  *
153  * When used with 'GNUNET_IDENTITY_create' or 'GNUNET_IDENTITY_get',
154  * this function is only called ONCE, and 'NULL' being passed in
155  * 'ego' does indicate an error (i.e. name is taken or no default
156  * value is known).  If 'ego' is non-NULL and if '*ctx'
157  * is set in those callbacks, the value WILL be passed to a subsequent
158  * call to the identity callback of 'GNUNET_IDENTITY_connect' (if 
159  * that one was not NULL).
160  *
161  * When an identity is renamed, this function is called with the
162  * (known) ego but the NEW identifier.  
163  *
164  * When an identity is deleted, this function is called with the
165  * (known) ego and "NULL" for the 'identifier'.  In this case,
166  * the 'ego' is henceforth invalid (and the 'ctx' should also be
167  * cleaned up).
168  *
169  * @param cls closure
170  * @param ego ego handle
171  * @param ctx context for application to store data for this ego
172  *                 (during the lifetime of this process, initially NULL)
173  * @param identifier identifier assigned by the user for this ego,
174  *                   NULL if the user just deleted the ego and it
175  *                   must thus no longer be used
176 */
177 static void
178 print_ego (void *cls,
179            struct GNUNET_IDENTITY_Ego *ego,
180            void **ctx,
181            const char *identifier)
182 {  
183
184   if (! (list | monitor))
185     return;
186   if ( (NULL == ego) && (! monitor) )
187   {
188     GNUNET_SCHEDULER_shutdown ();
189     return;
190   }
191   fprintf (stderr, "%s\n", identifier);
192 }
193
194
195 /**
196  * Main function that will be run by the scheduler.
197  *
198  * @param cls closure
199  * @param args remaining command-line arguments
200  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
201  * @param cfg configuration
202  */
203 static void
204 run (void *cls, char *const *args, const char *cfgfile,
205      const struct GNUNET_CONFIGURATION_Handle *cfg)
206 {
207   sh = GNUNET_IDENTITY_connect (cfg, &print_ego, NULL);
208   if (NULL != delete_ego)
209     delete_op = GNUNET_IDENTITY_delete (sh,
210                                         delete_ego,
211                                         &delete_finished,
212                                         &delete_op);
213   if (NULL != create_ego)
214     create_op = GNUNET_IDENTITY_create (sh,
215                                         create_ego,
216                                         &create_finished,
217                                         &create_op);
218   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
219                                 &shutdown_task, NULL);
220   test_finished ();
221 }
222
223
224 /**
225  * The main function.
226  *
227  * @param argc number of arguments from the command line
228  * @param argv command line arguments
229  * @return 0 ok, 1 on error
230  */
231 int
232 main (int argc, char *const *argv)
233 {
234   int res;
235
236   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
237     {'C', "create", "NAME",
238      gettext_noop ("create ego NAME"),
239      1, &GNUNET_GETOPT_set_string, &create_ego},
240     {'D', "delete", "NAME",
241      gettext_noop ("delete ego NAME "),
242      1, &GNUNET_GETOPT_set_string, &delete_ego},
243     {'L', "list", NULL,
244      gettext_noop ("list all egos"),
245      0, &GNUNET_GETOPT_set_one, &list},
246     {'m', "monitor", NULL,
247      gettext_noop ("run in monitor mode egos"),
248      0, &GNUNET_GETOPT_set_one, &monitor},
249     GNUNET_GETOPT_OPTION_END
250   };
251
252   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
253     return 2;
254
255   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-identity",
256                             gettext_noop ("Maintain egos"), 
257                             options, &run,
258                             NULL);
259   GNUNET_free ((void *) argv);
260
261   if (GNUNET_OK != res)
262     return 1;
263   return 0;
264 }
265
266 /* end of gnunet-identity.c */