Change GPLv2+ to GPLv3+.
[shishi.git] / src / shishi.c
blobe862a281e102f395e3e8dfb3ce2edbc9d47a93db
1 /* shishi.c --- Shishi command line interface.
2 * Copyright (C) 2002, 2003, 2004, 2006, 2007 Simon Josefsson
4 * This file is part of Shishi.
6 * Shishi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * Shishi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Shishi; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #if HAVE_CONFIG_H
23 # include "config.h"
24 #endif
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <ctype.h>
30 #include <string.h>
32 /* Get i18n. */
33 #ifdef HAVE_LOCALE_H
34 # include <locale.h>
35 #else
36 # define setlocale(Category, Locale) /* empty */
37 #endif
38 #include <gettext.h>
39 #define _(String) gettext (String)
40 #define gettext_noop(String) String
41 #define N_(String) gettext_noop (String)
43 #include <shishi.h>
45 /* Get set_program_name and program_name. */
46 #include "progname.h"
48 /* Get error. */
49 #include "error.h"
51 #include "shishi_cmd.h"
53 int
54 main (int argc, char *argv[])
56 struct gengetopt_args_info args;
57 time_t starttime, endtime, renew_till;
58 Shishi *sh;
59 int rc;
61 setlocale (LC_ALL, "");
62 bindtextdomain (PACKAGE, LOCALEDIR);
63 textdomain (PACKAGE);
64 set_program_name (argv[0]);
66 if (cmdline_parser (argc, argv, &args) != 0)
67 error (EXIT_FAILURE, 0, _("Try `%s --help' for more information."),
68 program_name);
70 if (args.inputs_num > 2 ||
71 args.destroy_given + args.list_given + args.renew_given > 1)
73 error (0, 0, _("too many arguments"));
74 error (EXIT_FAILURE, 0, _("Try `%s --help' for more information."),
75 program_name);
78 if (args.help_given)
80 cmdline_parser_print_help ();
81 printf (_("\nMandatory arguments to long options are "
82 "mandatory for short options too.\n\nReport bugs to <%s>.\n"),
83 PACKAGE_BUGREPORT);
84 return EXIT_SUCCESS;
87 rc = shishi_init_with_paths (&sh, args.ticket_file_arg,
88 args.system_configuration_file_arg,
89 args.configuration_file_arg);
90 if (rc == SHISHI_HANDLE_ERROR)
91 error (EXIT_FAILURE, 0, _("Could not initialize libshishi."));
93 rc = shishi_cfg_clientkdcetype_set (sh, args.encryption_type_arg);
94 if (rc != SHISHI_OK)
95 error (EXIT_FAILURE, 0, _("Could not set encryption types: %s"),
96 shishi_strerror (rc));
98 if (args.inputs_num > 0)
100 rc = shishi_parse_name (sh, args.inputs[0],
101 (args.client_name_arg ? NULL :
102 &args.client_name_arg),
103 (args.realm_arg ? NULL : &args.realm_arg));
105 if (rc != SHISHI_OK)
106 error (EXIT_FAILURE, 0,
107 _("Could not parse client principal \"%s\": %s"),
108 args.inputs[0], shishi_strerror (rc));
111 if (args.inputs_num > 1)
113 rc = shishi_parse_name (sh, args.inputs[1],
114 (args.server_name_arg ? NULL :
115 &args.server_name_arg),
116 (args.realm_arg ? NULL : &args.realm_arg));
118 if (rc != SHISHI_OK)
119 error (EXIT_FAILURE, 0,
120 _("Could not parse server principal \"%s\": %s"),
121 args.inputs[1], shishi_strerror (rc));
124 rc = shishi_cfg (sh, args.library_options_arg);
125 if (rc != SHISHI_OK)
126 error (EXIT_FAILURE, 0, _("Could not read library options: %s"),
127 shishi_strerror (rc));
129 if (args.verbose_given > 0)
130 shishi_cfg (sh, "verbose");
131 if (args.verbose_given > 1)
132 shishi_cfg (sh, "verbose-noise");
133 if (args.verbose_given > 2)
134 shishi_cfg (sh, "verbose-asn1");
135 if (args.verbose_given > 3)
136 shishi_cfg (sh, "verbose-crypto");
137 if (args.verbose_given > 4)
138 shishi_cfg (sh, "verbose-crypto-noise");
140 if (args.starttime_arg)
142 starttime = shishi_get_date (args.starttime_arg, NULL);
143 if (starttime == -1)
144 error (EXIT_FAILURE, 0, _("Invalid --starttime date `%s'"),
145 args.starttime_arg);
147 else
148 starttime = time (NULL);
150 if (args.endtime_arg)
152 endtime = shishi_get_date (args.endtime_arg, &starttime);
153 if (endtime == -1)
154 error (EXIT_FAILURE, 0, _("Invalid --endtime date `%s'"),
155 args.starttime_arg);
157 else
158 endtime = 0;
160 if (args.renew_till_arg)
162 renew_till = shishi_get_date (args.renew_till_arg, &starttime);
163 if (renew_till == -1)
164 error (EXIT_FAILURE, 0, _("Invalid --renew-till date `%s'"),
165 args.renew_till_arg);
167 else
168 renew_till = 0;
170 if (args.client_name_arg)
171 shishi_principal_default_set (sh, args.client_name_arg);
173 if (args.realm_arg)
174 shishi_realm_default_set (sh, args.realm_arg);
176 if (!args.ticket_granter_arg)
177 asprintf (&args.ticket_granter_arg, "krbtgt/%s",
178 shishi_realm_default (sh));
180 if (args.list_flag)
182 if (!args.quiet_flag)
183 printf (_("Tickets in `%s':\n"), shishi_tkts_default_file (sh));
185 rc = shishi_tkts_print_for_service (shishi_tkts_default (sh),
186 stdout, args.server_name_arg);
187 if (rc != SHISHI_OK)
188 error (EXIT_FAILURE, 0, _("Could not list tickets: %s"),
189 shishi_strerror (rc));
191 else if (args.destroy_flag)
193 int i, removed = 0;
195 for (i = 0; i < shishi_tkts_size (shishi_tkts_default (sh)); i++)
197 if (args.server_name_arg &&
198 !shishi_tkt_server_p (shishi_tkts_nth (shishi_tkts_default (sh),
200 args.server_name_arg))
201 continue;
203 if (args.verbose_given)
205 printf (_("Removing ticket:\n"));
206 shishi_tkt_pretty_print (shishi_tkts_nth
207 (shishi_tkts_default (sh), i), stdout);
210 rc = shishi_tkts_remove (shishi_tkts_default (sh), i);
211 if (rc != SHISHI_OK)
212 error (EXIT_FAILURE, 0, _("Could not destroy ticket %d:\n%s"), i,
213 shishi_strerror (rc));
215 i--;
216 removed++;
219 if (!args.quiet_flag)
221 if (removed == 0)
222 printf (_("No tickets removed.\n"));
223 else
224 printf (ngettext ("%d ticket removed.\n",
225 "%d tickets removed.\n", removed), removed);
228 else if (args.renew_given)
230 error (EXIT_FAILURE, 0, "Command --renew not implemented.");
232 else
234 Shishi_tkt *tkt;
235 Shishi_tkts_hint hint;
237 memset (&hint, 0, sizeof (hint));
238 hint.client = args.client_name_arg;
239 hint.server = args.server_name_arg ? args.server_name_arg :
240 args.ticket_granter_arg;
241 hint.starttime = starttime;
242 hint.endtime = endtime;
243 hint.renew_till = renew_till;
244 if (args.renewable_flag)
245 hint.tktflags |= SHISHI_TICKETFLAGS_RENEWABLE;
246 if (args.proxiable_flag)
247 hint.tktflags |= SHISHI_TICKETFLAGS_PROXIABLE;
248 if (args.proxy_flag)
249 hint.tktflags |= SHISHI_TICKETFLAGS_PROXY;
250 if (args.forwardable_flag)
251 hint.tktflags |= SHISHI_TICKETFLAGS_FORWARDABLE;
252 if (args.forwarded_flag)
253 hint.tktflags |= SHISHI_TICKETFLAGS_FORWARDED;
255 tkt = shishi_tkts_get (shishi_tkts_default (sh), &hint);
256 if (!tkt)
257 error (EXIT_FAILURE, 0, _("Could not get ticket as `%s' for `%s'."),
258 hint.client ? hint.client : shishi_principal_default (sh),
259 hint.server);
261 shishi_tkt_pretty_print (tkt, stdout);
264 shishi_tkts_expire (shishi_tkts_default (sh));
266 if (args.ticket_write_file_arg)
267 shishi_tkts_default_file_set (sh, args.ticket_write_file_arg);
269 shishi_done (sh);
271 return EXIT_SUCCESS;