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
36 # define setlocale(Category, Locale) /* empty */
39 #define _(String) gettext (String)
40 #define gettext_noop(String) String
41 #define N_(String) gettext_noop (String)
45 /* Get set_program_name and program_name. */
49 #include "vasprintf.h"
54 #include "shishi_cmd.h"
57 main (int argc
, char *argv
[])
59 struct gengetopt_args_info args
;
60 time_t starttime
, endtime
, renew_till
;
64 setlocale (LC_ALL
, "");
65 bindtextdomain (PACKAGE
, LOCALEDIR
);
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."),
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."),
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"),
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
);
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
));
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
));
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
);
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
);
147 error (EXIT_FAILURE
, 0, _("Invalid --starttime date `%s'"),
151 starttime
= time (NULL
);
153 if (args
.endtime_arg
)
155 endtime
= shishi_get_date (args
.endtime_arg
, &starttime
);
157 error (EXIT_FAILURE
, 0, _("Invalid --endtime date `%s'"),
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
);
173 if (args
.client_name_arg
)
174 shishi_principal_default_set (sh
, args
.client_name_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
));
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
);
191 error (EXIT_FAILURE
, 0, _("Could not list tickets: %s"),
192 shishi_strerror (rc
));
194 else if (args
.destroy_flag
)
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
))
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
);
215 error (EXIT_FAILURE
, 0, _("Could not destroy ticket %d:\n%s"), i
,
216 shishi_strerror (rc
));
222 if (!args
.quiet_flag
)
225 printf (_("No tickets removed.\n"));
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.");
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
;
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
);
260 error (EXIT_FAILURE
, 0, _("Could not get ticket as `%s' for `%s'."),
261 hint
.client
? hint
.client
: shishi_principal_default (sh
),
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
);