Add.
[shishi.git] / src / shishi.c
blob4280773d75e5df416ae9b8ec4c418bae47dc7358
1 /* shishi.c --- Shishi command line interface.
2 * Copyright (C) 2002, 2003, 2004, 2006 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 2 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 asprintf. */
49 #include "vasprintf.h"
51 /* Get error. */
52 #include "error.h"
54 #include "shishi_cmd.h"
56 int
57 main (int argc, char *argv[])
59 struct gengetopt_args_info args;
60 time_t starttime, endtime, renew_till;
61 Shishi *sh;
62 int rc;
64 setlocale (LC_ALL, "");
65 bindtextdomain (PACKAGE, LOCALEDIR);
66 textdomain (PACKAGE);
67 set_program_name (argv[0]);
69 if (cmdline_parser (argc, argv, &args) != 0)
70 error (EXIT_FAILURE, 0, _("Try `%s --help' for more information."),
71 program_name);
73 if (args.inputs_num > 2 ||
74 args.destroy_given + args.list_given + args.renew_given > 1)
76 error (0, 0, _("too many arguments"));
77 error (EXIT_FAILURE, 0, _("Try `%s --help' for more information."),
78 program_name);
81 if (args.help_given)
83 cmdline_parser_print_help ();
84 printf (_("\nMandatory arguments to long options are "
85 "mandatory for short options too.\n\nReport bugs to <%s>.\n"),
86 PACKAGE_BUGREPORT);
87 return EXIT_SUCCESS;
90 rc = shishi_init_with_paths (&sh, args.ticket_file_arg,
91 args.system_configuration_file_arg,
92 args.configuration_file_arg);
93 if (rc == SHISHI_HANDLE_ERROR)
94 error (EXIT_FAILURE, 0, _("Could not initialize libshishi."));
96 rc = shishi_cfg_clientkdcetype_set (sh, args.encryption_type_arg);
97 if (rc != SHISHI_OK)
98 error (EXIT_FAILURE, 0, _("Could not set encryption types: %s"),
99 shishi_strerror (rc));
101 if (args.inputs_num > 0)
103 rc = shishi_parse_name (sh, args.inputs[0],
104 (args.client_name_arg ? NULL :
105 &args.client_name_arg),
106 (args.realm_arg ? NULL : &args.realm_arg));
108 if (rc != SHISHI_OK)
109 error (EXIT_FAILURE, 0,
110 _("Could not parse client principal \"%s\": %s"),
111 args.inputs[0], shishi_strerror (rc));
114 if (args.inputs_num > 1)
116 rc = shishi_parse_name (sh, args.inputs[1],
117 (args.server_name_arg ? NULL :
118 &args.server_name_arg),
119 (args.realm_arg ? NULL : &args.realm_arg));
121 if (rc != SHISHI_OK)
122 error (EXIT_FAILURE, 0,
123 _("Could not parse server principal \"%s\": %s"),
124 args.inputs[1], shishi_strerror (rc));
127 rc = shishi_cfg (sh, args.library_options_arg);
128 if (rc != SHISHI_OK)
129 error (EXIT_FAILURE, 0, _("Could not read library options: %s"),
130 shishi_strerror (rc));
132 if (args.verbose_given > 0)
133 shishi_cfg (sh, "verbose");
134 if (args.verbose_given > 1)
135 shishi_cfg (sh, "verbose-noise");
136 if (args.verbose_given > 2)
137 shishi_cfg (sh, "verbose-asn1");
138 if (args.verbose_given > 3)
139 shishi_cfg (sh, "verbose-crypto");
140 if (args.verbose_given > 4)
141 shishi_cfg (sh, "verbose-crypto-noise");
143 if (args.starttime_arg)
145 starttime = shishi_get_date (args.starttime_arg, NULL);
146 if (starttime == -1)
147 error (EXIT_FAILURE, 0, _("Invalid --starttime date `%s'"),
148 args.starttime_arg);
150 else
151 starttime = time (NULL);
153 if (args.endtime_arg)
155 endtime = shishi_get_date (args.endtime_arg, &starttime);
156 if (endtime == -1)
157 error (EXIT_FAILURE, 0, _("Invalid --endtime date `%s'"),
158 args.starttime_arg);
160 else
161 endtime = 0;
163 if (args.renew_till_arg)
165 renew_till = shishi_get_date (args.renew_till_arg, &starttime);
166 if (renew_till == -1)
167 error (EXIT_FAILURE, 0, _("Invalid --renew-till date `%s'"),
168 args.renew_till_arg);
170 else
171 renew_till = 0;
173 if (args.client_name_arg)
174 shishi_principal_default_set (sh, args.client_name_arg);
176 if (args.realm_arg)
177 shishi_realm_default_set (sh, args.realm_arg);
179 if (!args.ticket_granter_arg)
180 asprintf (&args.ticket_granter_arg, "krbtgt/%s",
181 shishi_realm_default (sh));
183 if (args.list_flag)
185 if (!args.quiet_flag)
186 printf (_("Tickets in `%s':\n"), shishi_tkts_default_file (sh));
188 rc = shishi_tkts_print_for_service (shishi_tkts_default (sh),
189 stdout, args.server_name_arg);
190 if (rc != SHISHI_OK)
191 error (EXIT_FAILURE, 0, _("Could not list tickets: %s"),
192 shishi_strerror (rc));
194 else if (args.destroy_flag)
196 int i, removed = 0;
198 for (i = 0; i < shishi_tkts_size (shishi_tkts_default (sh)); i++)
200 if (args.server_name_arg &&
201 !shishi_tkt_server_p (shishi_tkts_nth (shishi_tkts_default (sh),
203 args.server_name_arg))
204 continue;
206 if (args.verbose_given)
208 printf (_("Removing ticket:\n"));
209 shishi_tkt_pretty_print (shishi_tkts_nth
210 (shishi_tkts_default (sh), i), stdout);
213 rc = shishi_tkts_remove (shishi_tkts_default (sh), i);
214 if (rc != SHISHI_OK)
215 error (EXIT_FAILURE, 0, _("Could not destroy ticket %d:\n%s"), i,
216 shishi_strerror (rc));
218 i--;
219 removed++;
222 if (!args.quiet_flag)
224 if (removed == 0)
225 printf (_("No tickets removed.\n"));
226 else
227 printf (ngettext ("%d ticket removed.\n",
228 "%d tickets removed.\n", removed), removed);
231 else if (args.renew_given)
233 error (EXIT_FAILURE, 0, "Command --renew not implemented.");
235 else
237 Shishi_tkt *tkt;
238 Shishi_tkts_hint hint;
240 memset (&hint, 0, sizeof (hint));
241 hint.client = args.client_name_arg;
242 hint.server = args.server_name_arg ? args.server_name_arg :
243 args.ticket_granter_arg;
244 hint.starttime = starttime;
245 hint.endtime = endtime;
246 hint.renew_till = renew_till;
247 if (args.renewable_flag)
248 hint.tktflags |= SHISHI_TICKETFLAGS_RENEWABLE;
249 if (args.proxiable_flag)
250 hint.tktflags |= SHISHI_TICKETFLAGS_PROXIABLE;
251 if (args.proxy_flag)
252 hint.tktflags |= SHISHI_TICKETFLAGS_PROXY;
253 if (args.forwardable_flag)
254 hint.tktflags |= SHISHI_TICKETFLAGS_FORWARDABLE;
255 if (args.forwarded_flag)
256 hint.tktflags |= SHISHI_TICKETFLAGS_FORWARDED;
258 tkt = shishi_tkts_get (shishi_tkts_default (sh), &hint);
259 if (!tkt)
260 error (EXIT_FAILURE, 0, _("Could not get ticket as `%s' for `%s'."),
261 hint.client ? hint.client : shishi_principal_default (sh),
262 hint.server);
264 shishi_tkt_pretty_print (tkt, stdout);
267 shishi_tkts_expire (shishi_tkts_default (sh));
269 if (args.ticket_write_file_arg)
270 shishi_tkts_default_file_set (sh, args.ticket_write_file_arg);
272 shishi_done (sh);
274 return EXIT_SUCCESS;