Path: blob/main/crypto/heimdal/appl/ftp/ftpd/klist.c
34889 views
/*1* Copyright (c) 1995 - 2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "ftpd_locl.h"3435#ifdef KRB53637static int38print_cred(krb5_context context, krb5_creds *cred)39{40char t1[128], t2[128], *str;41krb5_error_code ret;42krb5_timestamp sec;4344krb5_timeofday (context, &sec);4546if(cred->times.starttime)47krb5_format_time(context, cred->times.starttime, t1, sizeof(t1), 1);48else49krb5_format_time(context, cred->times.authtime, t1, sizeof(t1), 1);5051if(cred->times.endtime > sec)52krb5_format_time(context, cred->times.endtime, t2, sizeof(t2), 1);53else54strlcpy(t2, ">>>Expired<<<", sizeof(t2));5556ret = krb5_unparse_name (context, cred->server, &str);57if (ret) {58lreply(500, "krb5_unparse_name: %d", ret);59return 1;60}6162lreply(200, "%-20s %-20s %s", t1, t2, str);63free(str);64return 0;65}6667static int68print_tickets (krb5_context context,69krb5_ccache ccache,70krb5_principal principal)71{72krb5_error_code ret;73krb5_cc_cursor cursor;74krb5_creds cred;75char *str;7677ret = krb5_unparse_name (context, principal, &str);78if (ret) {79lreply(500, "krb5_unparse_name: %d", ret);80return 500;81}8283lreply(200, "%17s: %s:%s",84"Credentials cache",85krb5_cc_get_type(context, ccache),86krb5_cc_get_name(context, ccache));87lreply(200, "%17s: %s", "Principal", str);88free (str);8990ret = krb5_cc_start_seq_get (context, ccache, &cursor);91if (ret) {92lreply(500, "krb5_cc_start_seq_get: %d", ret);93return 500;94}9596lreply(200, " Issued Expires Principal");9798while ((ret = krb5_cc_next_cred (context,99ccache,100&cursor,101&cred)) == 0) {102if (print_cred(context, &cred))103return 500;104krb5_free_cred_contents (context, &cred);105}106if (ret != KRB5_CC_END) {107lreply(500, "krb5_cc_get_next: %d", ret);108return 500;109}110ret = krb5_cc_end_seq_get (context, ccache, &cursor);111if (ret) {112lreply(500, "krb5_cc_end_seq_get: %d", ret);113return 500;114}115116return 200;117}118119static int120klist5(void)121{122krb5_error_code ret;123krb5_context context;124krb5_ccache ccache;125krb5_principal principal;126int exit_status = 200;127128ret = krb5_init_context (&context);129if (ret) {130lreply(500, "krb5_init_context failed: %d", ret);131return 500;132}133134if (k5ccname)135ret = krb5_cc_resolve(context, k5ccname, &ccache);136else137ret = krb5_cc_default (context, &ccache);138if (ret) {139lreply(500, "krb5_cc_default: %d", ret);140return 500;141}142143ret = krb5_cc_get_principal (context, ccache, &principal);144if (ret) {145if(ret == ENOENT)146lreply(500, "No ticket file: %s",147krb5_cc_get_name(context, ccache));148else149lreply(500, "krb5_cc_get_principal: %d", ret);150151return 500;152}153exit_status = print_tickets (context, ccache, principal);154155ret = krb5_cc_close (context, ccache);156if (ret) {157lreply(500, "krb5_cc_close: %d", ret);158exit_status = 500;159}160161krb5_free_principal (context, principal);162krb5_free_context (context);163return exit_status;164}165#endif166167void168klist(void)169{170#if KRB5171int res = klist5();172reply(res, " ");173#else174reply(500, "Command not implemented.");175#endif176}177178179180