a56b229b5a5228e9957740784187f8c9036f5f88
[oweals/gnunet.git] / src / gns / gnunet-gns.c
1 /*
2      This file is part of GNUnet.
3      (C) 2012 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 gnunet-gns.c
22  * @brief command line tool to manipulate the local zone
23  * @author Christian Grothoff
24  *
25  * TODO:
26  * - everything
27  */
28 #include "platform.h"
29 #include <gnunet_util_lib.h>
30 #include <gnunet_namestore_service.h>
31
32 /**
33  * Handle to the namestore.
34  */
35 static struct GNUNET_NAMESTORE_Handle *ns;
36
37 /**
38  * Hash of the public key of our zone.
39  */
40 static GNUNET_HashCode zone;
41
42 /**
43  * Private key for the our zone.
44  */
45 static struct GNUNET_CRYPTO_RsaPrivateKey *zone_pkey;
46
47 /**
48  * Keyfile to manipulate.
49  */
50 static char *keyfile;   
51                 
52
53 /**
54  * Task run on shutdown.  Cleans up everything.
55  *
56  * @param cls unused
57  * @param tc scheduler context
58  */
59 static void
60 do_shutdown (void *cls,
61              const struct GNUNET_SCHEDULER_TaskContext *tc)
62 {
63   if (NULL != ns)
64   {
65     GNUNET_NAMESTORE_disconnect (ns, GNUNET_NO);
66     ns = NULL;
67   }
68   if (NULL != zone_pkey)
69   {
70     GNUNET_CRYPTO_rsa_key_free (zone_pkey);
71     zone_pkey = NULL;
72   }
73 }
74
75
76 /**
77  * Main function that will be run.
78  *
79  * @param cls closure
80  * @param args remaining command-line arguments
81  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
82  * @param cfg configuration
83  */
84 static void
85 run (void *cls, char *const *args, const char *cfgfile,
86      const struct GNUNET_CONFIGURATION_Handle *cfg)
87 {
88   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded pub;
89
90   if (NULL == keyfile)
91   {
92     fprintf (stderr,
93              _("Option `%s' not given, but I need a zone key file!\n"),
94              "z");
95     return;
96   }
97   zone_pkey = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
98   GNUNET_free (keyfile);
99   keyfile = NULL;
100   if (NULL == zone_pkey)
101   {
102     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
103                 _("Failed to read or create private zone key\n"));
104     return;
105   }
106   GNUNET_CRYPTO_rsa_key_get_public (zone_pkey,
107                                     &pub);
108   GNUNET_CRYPTO_hash (&pub, sizeof (pub), &zone);
109   ns = GNUNET_NAMESTORE_connect (cfg);
110   if (NULL == ns)
111     {
112       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
113                   _("Failed to connect to namestore\n"));
114       return;
115     }
116   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
117                                 &do_shutdown, NULL);
118 }
119
120
121 /**
122  * The main function for gnunet-gns.
123  *
124  * @param argc number of arguments from the command line
125  * @param argv command line arguments
126  * @return 0 ok, 1 on error
127  */
128 int
129 main (int argc, char *const *argv)
130 {
131   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
132     {'z', "zonekey", "FILENAME",
133      gettext_noop ("filename with the zone key"), 1,
134      &GNUNET_GETOPT_set_string, &keyfile},   
135     GNUNET_GETOPT_OPTION_END
136   };
137
138   int ret;
139
140   GNUNET_log_setup ("gnunet-gns", "WARNING", NULL);
141   ret =
142       (GNUNET_OK ==
143        GNUNET_PROGRAM_run (argc, argv, "gnunet-gns",
144                            _("GNUnet GNS zone manipulation tool"), 
145                            options,
146                            &run, NULL)) ? 0 : 1;
147
148   return ret;
149 }
150
151 /* end of gnunet-gns.c */