etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / kuser / kdeltkt.c
blob6ace53a3180ef5a6cff9d0ff688ca424f73f949c
1 /* $NetBSD: kdeltkt.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
4 #include "kuser_locl.h"
6 static char *etypestr = 0;
7 static char *ccachestr = 0;
8 static char *flagstr = 0;
9 static int quiet_flag = 0;
10 static int help_flag = 0;
11 static int version_flag = 0;
13 struct getargs args[] = {
14 { "cache", 'c', arg_string, &ccachestr,
15 "Credentials cache", "cachename" },
16 { "enctype", 'e', arg_string, &etypestr,
17 "Encryption type", "enctype" },
18 { "flags", 'f', arg_string, &flagstr,
19 "Flags", "flags" },
20 { "quiet", 'q', arg_flag, &quiet_flag, "Quiet" },
21 { "version", 0, arg_flag, &version_flag },
22 { "help", 0, arg_flag, &help_flag }
25 static void
26 usage(int ret)
28 arg_printusage(args, sizeof(args)/sizeof(args[0]),
29 "Usage: ", "service1 [service2 ...]");
30 exit(ret);
33 static void do_kdeltkt (int argc, char *argv[], char *ccachestr, char *etypestr, int flags);
35 int main(int argc, char *argv[])
37 int optidx = 0;
38 int flags = 0;
40 setprogname(argv[0]);
42 if (getarg(args, sizeof(args)/sizeof(args[0]), argc, argv, &optidx))
43 usage (1);
45 if (help_flag)
46 usage(0);
48 if (version_flag) {
49 print_version(NULL);
50 exit(0);
53 argc -= optidx;
54 argv += optidx;
56 if (argc < 1)
57 usage (1);
59 if (flagstr)
60 flags = atoi(flagstr);
62 do_kdeltkt(argc, argv, ccachestr, etypestr, flags);
64 return 0;
67 static void do_kdeltkt (int count, char *names[],
68 char *ccachestr, char *etypestr, int flags)
70 krb5_context context;
71 krb5_error_code ret;
72 int i, errors;
73 krb5_enctype etype;
74 krb5_ccache ccache;
75 krb5_principal me;
76 krb5_creds in_creds, out_creds;
77 int retflags;
78 char *princ;
80 ret = krb5_init_context(&context);
81 if (ret)
82 errx(1, "krb5_init_context failed: %d", ret);
84 if (etypestr) {
85 ret = krb5_string_to_enctype(context, etypestr, &etype);
86 if (ret)
87 krb5_err(context, 1, ret, "Can't convert enctype %s", etypestr);
88 retflags = KRB5_TC_MATCH_SRV_NAMEONLY | KRB5_TC_MATCH_KEYTYPE;
89 } else {
90 etype = 0;
91 retflags = KRB5_TC_MATCH_SRV_NAMEONLY;
94 if (ccachestr)
95 ret = krb5_cc_resolve(context, ccachestr, &ccache);
96 else
97 ret = krb5_cc_default(context, &ccache);
98 if (ret)
99 krb5_err(context, 1, ret, "Can't open credentials cache");
101 ret = krb5_cc_get_principal(context, ccache, &me);
102 if (ret)
103 krb5_err(context, 1, ret, "Can't get client principal");
105 errors = 0;
107 for (i = 0; i < count; i++) {
108 memset(&in_creds, 0, sizeof(in_creds));
110 in_creds.client = me;
112 ret = krb5_parse_name(context, names[i], &in_creds.server);
113 if (ret) {
114 if (!quiet_flag)
115 krb5_warn(context, ret, "Can't parse principal name %s", names[i]);
116 errors++;
117 continue;
120 ret = krb5_unparse_name(context, in_creds.server, &princ);
121 if (ret) {
122 krb5_warn(context, ret, "Can't unparse principal name %s", names[i]);
123 errors++;
124 continue;
127 in_creds.session.keytype = etype;
129 ret = krb5_cc_retrieve_cred(context, ccache, retflags,
130 &in_creds, &out_creds);
131 if (ret) {
132 krb5_warn(context, ret, "Can't retrieve credentials for %s", princ);
134 krb5_free_unparsed_name(context, princ);
136 errors++;
137 continue;
140 ret = krb5_cc_remove_cred(context, ccache, flags, &out_creds);
142 krb5_free_principal(context, in_creds.server);
144 if (ret) {
145 krb5_warn(context, ret, "Can't remove credentials for %s", princ);
147 krb5_free_cred_contents(context, &out_creds);
148 krb5_free_unparsed_name(context, princ);
150 errors++;
151 continue;
154 krb5_free_unparsed_name(context, princ);
155 krb5_free_cred_contents(context, &out_creds);
158 krb5_free_principal(context, me);
159 krb5_cc_close(context, ccache);
160 krb5_free_context(context);
162 if (errors)
163 exit(1);
165 exit(0);