]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - crypto/heimdal/appl/ftp/ftpd/klist.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / crypto / heimdal / appl / ftp / ftpd / klist.c
1 /*
2  * Copyright (c) 1995 - 2005 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  * 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include "ftpd_locl.h"
35
36 #ifdef KRB5
37
38 static int
39 print_cred(krb5_context context, krb5_creds *cred)
40 {
41     char t1[128], t2[128], *str;
42     krb5_error_code ret;
43     krb5_timestamp sec;
44
45     krb5_timeofday (context, &sec);
46
47     if(cred->times.starttime)
48         krb5_format_time(context, cred->times.starttime, t1, sizeof(t1), 1);
49     else
50         krb5_format_time(context, cred->times.authtime, t1, sizeof(t1), 1);
51     
52     if(cred->times.endtime > sec)
53         krb5_format_time(context, cred->times.endtime, t2, sizeof(t2), 1);
54     else
55         strlcpy(t2, ">>>Expired<<<", sizeof(t2));
56
57     ret = krb5_unparse_name (context, cred->server, &str);
58     if (ret) {
59         lreply(500, "krb5_unparse_name: %d", ret);
60         return 1;
61     }
62
63     lreply(200, "%-20s %-20s %s", t1, t2, str);
64     free(str);
65     return 0;
66 }
67
68 static int
69 print_tickets (krb5_context context,
70                krb5_ccache ccache,
71                krb5_principal principal)
72 {
73     krb5_error_code ret;
74     krb5_cc_cursor cursor;
75     krb5_creds cred;
76     char *str;
77
78     ret = krb5_unparse_name (context, principal, &str);
79     if (ret) {
80         lreply(500, "krb5_unparse_name: %d", ret);
81         return 500;
82     }
83
84     lreply(200, "%17s: %s:%s", 
85            "Credentials cache",
86            krb5_cc_get_type(context, ccache),
87            krb5_cc_get_name(context, ccache));
88     lreply(200, "%17s: %s", "Principal", str);
89     free (str);
90
91     ret = krb5_cc_start_seq_get (context, ccache, &cursor);
92     if (ret) {
93         lreply(500, "krb5_cc_start_seq_get: %d", ret);
94         return 500;
95     }
96
97     lreply(200, "  Issued               Expires              Principal");
98
99     while ((ret = krb5_cc_next_cred (context,
100                                      ccache,
101                                      &cursor,
102                                      &cred)) == 0) {
103         if (print_cred(context, &cred))
104             return 500;         
105         krb5_free_cred_contents (context, &cred);
106     }
107     if (ret != KRB5_CC_END) {
108         lreply(500, "krb5_cc_get_next: %d", ret);
109         return 500;
110     }
111     ret = krb5_cc_end_seq_get (context, ccache, &cursor);
112     if (ret) {
113         lreply(500, "krb5_cc_end_seq_get: %d", ret);
114         return 500;
115     }
116
117     return 200;
118 }
119
120 static int
121 klist5(void)
122 {
123     krb5_error_code ret;
124     krb5_context context;
125     krb5_ccache ccache;
126     krb5_principal principal;
127     int exit_status = 200;
128
129     ret = krb5_init_context (&context);
130     if (ret) {
131         lreply(500, "krb5_init_context failed: %d", ret);
132         return 500;
133     }
134
135     if (k5ccname)
136         ret = krb5_cc_resolve(context, k5ccname, &ccache);
137     else
138         ret = krb5_cc_default (context, &ccache);
139     if (ret) {
140         lreply(500, "krb5_cc_default: %d", ret);            
141         return 500;
142     }
143
144     ret = krb5_cc_get_principal (context, ccache, &principal);
145     if (ret) {
146         if(ret == ENOENT)
147             lreply(500, "No ticket file: %s",
148                    krb5_cc_get_name(context, ccache));
149         else
150             lreply(500, "krb5_cc_get_principal: %d", ret);
151
152         return 500;
153     }
154     exit_status = print_tickets (context, ccache, principal);
155
156     ret = krb5_cc_close (context, ccache);
157     if (ret) {
158         lreply(500, "krb5_cc_close: %d", ret);      
159         exit_status = 500;
160     }
161
162     krb5_free_principal (context, principal);
163     krb5_free_context (context);
164     return exit_status;
165 }
166 #endif
167
168 void
169 klist(void)
170 {
171 #if KRB5
172     int res = klist5();
173     reply(res, " ");
174 #else
175     reply(500, "Command not implemented.");
176 #endif
177 }
178