1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /*
3*7f2fe78bSCy Schubert * Copyright (c) 1994 by the University of Southern California
4*7f2fe78bSCy Schubert *
5*7f2fe78bSCy Schubert * EXPORT OF THIS SOFTWARE from the United States of America may
6*7f2fe78bSCy Schubert * require a specific license from the United States Government.
7*7f2fe78bSCy Schubert * It is the responsibility of any person or organization contemplating
8*7f2fe78bSCy Schubert * export to obtain such a license before exporting.
9*7f2fe78bSCy Schubert *
10*7f2fe78bSCy Schubert * WITHIN THAT CONSTRAINT, permission to copy, modify, and distribute
11*7f2fe78bSCy Schubert * this software and its documentation in source and binary forms is
12*7f2fe78bSCy Schubert * hereby granted, provided that any documentation or other materials
13*7f2fe78bSCy Schubert * related to such distribution or use acknowledge that the software
14*7f2fe78bSCy Schubert * was developed by the University of Southern California.
15*7f2fe78bSCy Schubert *
16*7f2fe78bSCy Schubert * DISCLAIMER OF WARRANTY. THIS SOFTWARE IS PROVIDED "AS IS". The
17*7f2fe78bSCy Schubert * University of Southern California MAKES NO REPRESENTATIONS OR
18*7f2fe78bSCy Schubert * WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not
19*7f2fe78bSCy Schubert * limitation, the University of Southern California MAKES NO
20*7f2fe78bSCy Schubert * REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY
21*7f2fe78bSCy Schubert * PARTICULAR PURPOSE. The University of Southern
22*7f2fe78bSCy Schubert * California shall not be held liable for any liability nor for any
23*7f2fe78bSCy Schubert * direct, indirect, or consequential damages with respect to any
24*7f2fe78bSCy Schubert * claim by the user or distributor of the ksu software.
25*7f2fe78bSCy Schubert *
26*7f2fe78bSCy Schubert * KSU was written by: Ari Medvinsky, ari@isi.edu
27*7f2fe78bSCy Schubert */
28*7f2fe78bSCy Schubert
29*7f2fe78bSCy Schubert #include "ksu.h"
30*7f2fe78bSCy Schubert #include "k5-base64.h"
31*7f2fe78bSCy Schubert #include "adm_proto.h"
32*7f2fe78bSCy Schubert #include <sys/types.h>
33*7f2fe78bSCy Schubert #include <sys/stat.h>
34*7f2fe78bSCy Schubert
35*7f2fe78bSCy Schubert /******************************************************************
36*7f2fe78bSCy Schubert krb5_cache_copy
37*7f2fe78bSCy Schubert
38*7f2fe78bSCy Schubert gets rid of any expired tickets in the secondary cache,
39*7f2fe78bSCy Schubert copies the default cache into the secondary cache,
40*7f2fe78bSCy Schubert
41*7f2fe78bSCy Schubert ************************************************************************/
42*7f2fe78bSCy Schubert
43*7f2fe78bSCy Schubert void show_credential();
44*7f2fe78bSCy Schubert
45*7f2fe78bSCy Schubert /* modifies only the cc_other, the algorithm may look a bit funny,
46*7f2fe78bSCy Schubert but I had to do it this way, since remove function did not come
47*7f2fe78bSCy Schubert with k5 beta 3 release.
48*7f2fe78bSCy Schubert */
49*7f2fe78bSCy Schubert
krb5_ccache_copy(context,cc_def,target_principal,cc_target,restrict_creds,primary_principal,stored)50*7f2fe78bSCy Schubert krb5_error_code krb5_ccache_copy(context, cc_def, target_principal, cc_target,
51*7f2fe78bSCy Schubert restrict_creds, primary_principal, stored)
52*7f2fe78bSCy Schubert /* IN */
53*7f2fe78bSCy Schubert krb5_context context;
54*7f2fe78bSCy Schubert krb5_ccache cc_def;
55*7f2fe78bSCy Schubert krb5_principal target_principal;
56*7f2fe78bSCy Schubert krb5_ccache cc_target;
57*7f2fe78bSCy Schubert krb5_boolean restrict_creds;
58*7f2fe78bSCy Schubert krb5_principal primary_principal;
59*7f2fe78bSCy Schubert /* OUT */
60*7f2fe78bSCy Schubert krb5_boolean *stored;
61*7f2fe78bSCy Schubert {
62*7f2fe78bSCy Schubert int i=0;
63*7f2fe78bSCy Schubert krb5_error_code retval=0;
64*7f2fe78bSCy Schubert krb5_creds ** cc_def_creds_arr = NULL;
65*7f2fe78bSCy Schubert krb5_creds ** cc_other_creds_arr = NULL;
66*7f2fe78bSCy Schubert
67*7f2fe78bSCy Schubert if (ks_ccache_is_initialized(context, cc_def)) {
68*7f2fe78bSCy Schubert if((retval = krb5_get_nonexp_tkts(context,cc_def,&cc_def_creds_arr))){
69*7f2fe78bSCy Schubert return retval;
70*7f2fe78bSCy Schubert }
71*7f2fe78bSCy Schubert }
72*7f2fe78bSCy Schubert
73*7f2fe78bSCy Schubert retval = krb5_cc_initialize(context, cc_target, target_principal);
74*7f2fe78bSCy Schubert if (retval)
75*7f2fe78bSCy Schubert return retval;
76*7f2fe78bSCy Schubert
77*7f2fe78bSCy Schubert if (restrict_creds) {
78*7f2fe78bSCy Schubert retval = krb5_store_some_creds(context, cc_target, cc_def_creds_arr,
79*7f2fe78bSCy Schubert cc_other_creds_arr, primary_principal,
80*7f2fe78bSCy Schubert stored);
81*7f2fe78bSCy Schubert } else {
82*7f2fe78bSCy Schubert *stored = krb5_find_princ_in_cred_list(context, cc_def_creds_arr,
83*7f2fe78bSCy Schubert primary_principal);
84*7f2fe78bSCy Schubert retval = krb5_store_all_creds(context, cc_target, cc_def_creds_arr,
85*7f2fe78bSCy Schubert cc_other_creds_arr);
86*7f2fe78bSCy Schubert }
87*7f2fe78bSCy Schubert
88*7f2fe78bSCy Schubert if (cc_def_creds_arr){
89*7f2fe78bSCy Schubert while (cc_def_creds_arr[i]){
90*7f2fe78bSCy Schubert krb5_free_creds(context, cc_def_creds_arr[i]);
91*7f2fe78bSCy Schubert i++;
92*7f2fe78bSCy Schubert }
93*7f2fe78bSCy Schubert }
94*7f2fe78bSCy Schubert
95*7f2fe78bSCy Schubert i=0;
96*7f2fe78bSCy Schubert
97*7f2fe78bSCy Schubert if(cc_other_creds_arr){
98*7f2fe78bSCy Schubert while (cc_other_creds_arr[i]){
99*7f2fe78bSCy Schubert krb5_free_creds(context, cc_other_creds_arr[i]);
100*7f2fe78bSCy Schubert i++;
101*7f2fe78bSCy Schubert }
102*7f2fe78bSCy Schubert }
103*7f2fe78bSCy Schubert
104*7f2fe78bSCy Schubert return retval;
105*7f2fe78bSCy Schubert }
106*7f2fe78bSCy Schubert
107*7f2fe78bSCy Schubert
krb5_store_all_creds(context,cc,creds_def,creds_other)108*7f2fe78bSCy Schubert krb5_error_code krb5_store_all_creds(context, cc, creds_def, creds_other)
109*7f2fe78bSCy Schubert krb5_context context;
110*7f2fe78bSCy Schubert krb5_ccache cc;
111*7f2fe78bSCy Schubert krb5_creds **creds_def;
112*7f2fe78bSCy Schubert krb5_creds **creds_other;
113*7f2fe78bSCy Schubert {
114*7f2fe78bSCy Schubert
115*7f2fe78bSCy Schubert int i = 0;
116*7f2fe78bSCy Schubert krb5_error_code retval = 0;
117*7f2fe78bSCy Schubert krb5_creds ** temp_creds= NULL;
118*7f2fe78bSCy Schubert
119*7f2fe78bSCy Schubert
120*7f2fe78bSCy Schubert if ((creds_def == NULL) && (creds_other == NULL))
121*7f2fe78bSCy Schubert return 0;
122*7f2fe78bSCy Schubert
123*7f2fe78bSCy Schubert if ((creds_def == NULL) && (creds_other != NULL))
124*7f2fe78bSCy Schubert temp_creds = creds_other;
125*7f2fe78bSCy Schubert
126*7f2fe78bSCy Schubert if ((creds_def != NULL) && (creds_other == NULL))
127*7f2fe78bSCy Schubert temp_creds = creds_def;
128*7f2fe78bSCy Schubert
129*7f2fe78bSCy Schubert
130*7f2fe78bSCy Schubert if (temp_creds){
131*7f2fe78bSCy Schubert while(temp_creds[i]){
132*7f2fe78bSCy Schubert if ((retval= krb5_cc_store_cred(context, cc,
133*7f2fe78bSCy Schubert temp_creds[i]))){
134*7f2fe78bSCy Schubert return retval;
135*7f2fe78bSCy Schubert }
136*7f2fe78bSCy Schubert i++;
137*7f2fe78bSCy Schubert }
138*7f2fe78bSCy Schubert }
139*7f2fe78bSCy Schubert else { /* both arrays have elements in them */
140*7f2fe78bSCy Schubert
141*7f2fe78bSCy Schubert return KRB5KRB_ERR_GENERIC;
142*7f2fe78bSCy Schubert
143*7f2fe78bSCy Schubert /************ while(creds_other[i]){
144*7f2fe78bSCy Schubert cmp = FALSE;
145*7f2fe78bSCy Schubert j = 0;
146*7f2fe78bSCy Schubert while(creds_def[j]){
147*7f2fe78bSCy Schubert cmp = compare_creds(creds_other[i],creds_def[j]);
148*7f2fe78bSCy Schubert
149*7f2fe78bSCy Schubert if( cmp == TRUE) break;
150*7f2fe78bSCy Schubert
151*7f2fe78bSCy Schubert j++;
152*7f2fe78bSCy Schubert }
153*7f2fe78bSCy Schubert if (cmp == FALSE){
154*7f2fe78bSCy Schubert if (retval= krb5_cc_store_cred(context, cc,
155*7f2fe78bSCy Schubert creds_other[i])){
156*7f2fe78bSCy Schubert return retval;
157*7f2fe78bSCy Schubert }
158*7f2fe78bSCy Schubert }
159*7f2fe78bSCy Schubert i ++;
160*7f2fe78bSCy Schubert }
161*7f2fe78bSCy Schubert
162*7f2fe78bSCy Schubert i=0;
163*7f2fe78bSCy Schubert while(creds_def[i]){
164*7f2fe78bSCy Schubert if (retval= krb5_cc_store_cred(context, cc,
165*7f2fe78bSCy Schubert creds_def[i])){
166*7f2fe78bSCy Schubert return retval;
167*7f2fe78bSCy Schubert }
168*7f2fe78bSCy Schubert i++;
169*7f2fe78bSCy Schubert }
170*7f2fe78bSCy Schubert
171*7f2fe78bSCy Schubert **************/
172*7f2fe78bSCy Schubert }
173*7f2fe78bSCy Schubert return 0;
174*7f2fe78bSCy Schubert }
175*7f2fe78bSCy Schubert
compare_creds(context,cred1,cred2)176*7f2fe78bSCy Schubert krb5_boolean compare_creds(context, cred1, cred2)
177*7f2fe78bSCy Schubert krb5_context context;
178*7f2fe78bSCy Schubert krb5_creds *cred1;
179*7f2fe78bSCy Schubert krb5_creds *cred2;
180*7f2fe78bSCy Schubert {
181*7f2fe78bSCy Schubert krb5_boolean retval;
182*7f2fe78bSCy Schubert
183*7f2fe78bSCy Schubert retval = krb5_principal_compare (context, cred1->client, cred2->client);
184*7f2fe78bSCy Schubert
185*7f2fe78bSCy Schubert if (retval == TRUE)
186*7f2fe78bSCy Schubert retval = krb5_principal_compare (context, cred1->server, cred2->server);
187*7f2fe78bSCy Schubert
188*7f2fe78bSCy Schubert return retval;
189*7f2fe78bSCy Schubert }
190*7f2fe78bSCy Schubert
191*7f2fe78bSCy Schubert
192*7f2fe78bSCy Schubert
193*7f2fe78bSCy Schubert
krb5_get_nonexp_tkts(context,cc,creds_array)194*7f2fe78bSCy Schubert krb5_error_code krb5_get_nonexp_tkts(context, cc, creds_array)
195*7f2fe78bSCy Schubert krb5_context context;
196*7f2fe78bSCy Schubert krb5_ccache cc;
197*7f2fe78bSCy Schubert krb5_creds ***creds_array;
198*7f2fe78bSCy Schubert {
199*7f2fe78bSCy Schubert
200*7f2fe78bSCy Schubert krb5_creds creds, temp_tktq, temp_tkt;
201*7f2fe78bSCy Schubert krb5_creds **temp_creds;
202*7f2fe78bSCy Schubert krb5_error_code retval=0;
203*7f2fe78bSCy Schubert krb5_cc_cursor cur;
204*7f2fe78bSCy Schubert int count = 0;
205*7f2fe78bSCy Schubert int chunk_count = 1;
206*7f2fe78bSCy Schubert
207*7f2fe78bSCy Schubert if ( ! ( temp_creds = (krb5_creds **) malloc( CHUNK * sizeof(krb5_creds *)))){
208*7f2fe78bSCy Schubert return ENOMEM;
209*7f2fe78bSCy Schubert }
210*7f2fe78bSCy Schubert
211*7f2fe78bSCy Schubert
212*7f2fe78bSCy Schubert memset(&temp_tktq, 0, sizeof(temp_tktq));
213*7f2fe78bSCy Schubert memset(&temp_tkt, 0, sizeof(temp_tkt));
214*7f2fe78bSCy Schubert memset(&creds, 0, sizeof(creds));
215*7f2fe78bSCy Schubert
216*7f2fe78bSCy Schubert /* initialize the cursor */
217*7f2fe78bSCy Schubert if ((retval = krb5_cc_start_seq_get(context, cc, &cur))) {
218*7f2fe78bSCy Schubert return retval;
219*7f2fe78bSCy Schubert }
220*7f2fe78bSCy Schubert
221*7f2fe78bSCy Schubert while (!(retval = krb5_cc_next_cred(context, cc, &cur, &creds))){
222*7f2fe78bSCy Schubert
223*7f2fe78bSCy Schubert if (!krb5_is_config_principal(context, creds.server) &&
224*7f2fe78bSCy Schubert (retval = krb5_check_exp(context, creds.times))){
225*7f2fe78bSCy Schubert if (retval != KRB5KRB_AP_ERR_TKT_EXPIRED){
226*7f2fe78bSCy Schubert return retval;
227*7f2fe78bSCy Schubert }
228*7f2fe78bSCy Schubert if (auth_debug){
229*7f2fe78bSCy Schubert fprintf(stderr,"krb5_ccache_copy: CREDS EXPIRED:\n");
230*7f2fe78bSCy Schubert fputs(" Valid starting Expires Service principal\n",stdout);
231*7f2fe78bSCy Schubert show_credential(context, &creds, cc);
232*7f2fe78bSCy Schubert fprintf(stderr,"\n");
233*7f2fe78bSCy Schubert }
234*7f2fe78bSCy Schubert }
235*7f2fe78bSCy Schubert else { /* these credentials didn't expire */
236*7f2fe78bSCy Schubert
237*7f2fe78bSCy Schubert if ((retval = krb5_copy_creds(context, &creds,
238*7f2fe78bSCy Schubert &temp_creds[count]))){
239*7f2fe78bSCy Schubert return retval;
240*7f2fe78bSCy Schubert }
241*7f2fe78bSCy Schubert count ++;
242*7f2fe78bSCy Schubert
243*7f2fe78bSCy Schubert if (count == (chunk_count * CHUNK -1)){
244*7f2fe78bSCy Schubert chunk_count ++;
245*7f2fe78bSCy Schubert if (!(temp_creds = (krb5_creds **) realloc(temp_creds,
246*7f2fe78bSCy Schubert chunk_count * CHUNK * sizeof(krb5_creds *)))){
247*7f2fe78bSCy Schubert return ENOMEM;
248*7f2fe78bSCy Schubert }
249*7f2fe78bSCy Schubert }
250*7f2fe78bSCy Schubert }
251*7f2fe78bSCy Schubert
252*7f2fe78bSCy Schubert }
253*7f2fe78bSCy Schubert
254*7f2fe78bSCy Schubert temp_creds[count] = NULL;
255*7f2fe78bSCy Schubert *creds_array = temp_creds;
256*7f2fe78bSCy Schubert
257*7f2fe78bSCy Schubert if (retval == KRB5_CC_END) {
258*7f2fe78bSCy Schubert retval = krb5_cc_end_seq_get(context, cc, &cur);
259*7f2fe78bSCy Schubert }
260*7f2fe78bSCy Schubert
261*7f2fe78bSCy Schubert return retval;
262*7f2fe78bSCy Schubert
263*7f2fe78bSCy Schubert }
264*7f2fe78bSCy Schubert
265*7f2fe78bSCy Schubert
krb5_check_exp(context,tkt_time)266*7f2fe78bSCy Schubert krb5_error_code krb5_check_exp(context, tkt_time)
267*7f2fe78bSCy Schubert krb5_context context;
268*7f2fe78bSCy Schubert krb5_ticket_times tkt_time;
269*7f2fe78bSCy Schubert {
270*7f2fe78bSCy Schubert krb5_error_code retval =0;
271*7f2fe78bSCy Schubert krb5_timestamp currenttime;
272*7f2fe78bSCy Schubert
273*7f2fe78bSCy Schubert if ((retval = krb5_timeofday (context, ¤ttime))){
274*7f2fe78bSCy Schubert return retval;
275*7f2fe78bSCy Schubert }
276*7f2fe78bSCy Schubert if (auth_debug){
277*7f2fe78bSCy Schubert fprintf(stderr,"krb5_check_exp: the krb5_clockskew is %d \n",
278*7f2fe78bSCy Schubert context->clockskew);
279*7f2fe78bSCy Schubert
280*7f2fe78bSCy Schubert fprintf(stderr,"krb5_check_exp: currenttime - endtime %d \n",
281*7f2fe78bSCy Schubert ts_delta(currenttime, tkt_time.endtime));
282*7f2fe78bSCy Schubert
283*7f2fe78bSCy Schubert }
284*7f2fe78bSCy Schubert
285*7f2fe78bSCy Schubert if (ts_after(currenttime, ts_incr(tkt_time.endtime, context->clockskew))) {
286*7f2fe78bSCy Schubert retval = KRB5KRB_AP_ERR_TKT_EXPIRED ;
287*7f2fe78bSCy Schubert return retval;
288*7f2fe78bSCy Schubert }
289*7f2fe78bSCy Schubert
290*7f2fe78bSCy Schubert return 0;
291*7f2fe78bSCy Schubert }
292*7f2fe78bSCy Schubert
293*7f2fe78bSCy Schubert
flags_string(cred)294*7f2fe78bSCy Schubert char *flags_string(cred)
295*7f2fe78bSCy Schubert krb5_creds *cred;
296*7f2fe78bSCy Schubert {
297*7f2fe78bSCy Schubert static char buf[32];
298*7f2fe78bSCy Schubert int i = 0;
299*7f2fe78bSCy Schubert
300*7f2fe78bSCy Schubert if (cred->ticket_flags & TKT_FLG_FORWARDABLE)
301*7f2fe78bSCy Schubert buf[i++] = 'F';
302*7f2fe78bSCy Schubert if (cred->ticket_flags & TKT_FLG_FORWARDED)
303*7f2fe78bSCy Schubert buf[i++] = 'f';
304*7f2fe78bSCy Schubert if (cred->ticket_flags & TKT_FLG_PROXIABLE)
305*7f2fe78bSCy Schubert buf[i++] = 'P';
306*7f2fe78bSCy Schubert if (cred->ticket_flags & TKT_FLG_PROXY)
307*7f2fe78bSCy Schubert buf[i++] = 'p';
308*7f2fe78bSCy Schubert if (cred->ticket_flags & TKT_FLG_MAY_POSTDATE)
309*7f2fe78bSCy Schubert buf[i++] = 'D';
310*7f2fe78bSCy Schubert if (cred->ticket_flags & TKT_FLG_POSTDATED)
311*7f2fe78bSCy Schubert buf[i++] = 'd';
312*7f2fe78bSCy Schubert if (cred->ticket_flags & TKT_FLG_INVALID)
313*7f2fe78bSCy Schubert buf[i++] = 'i';
314*7f2fe78bSCy Schubert if (cred->ticket_flags & TKT_FLG_RENEWABLE)
315*7f2fe78bSCy Schubert buf[i++] = 'R';
316*7f2fe78bSCy Schubert if (cred->ticket_flags & TKT_FLG_INITIAL)
317*7f2fe78bSCy Schubert buf[i++] = 'I';
318*7f2fe78bSCy Schubert if (cred->ticket_flags & TKT_FLG_HW_AUTH)
319*7f2fe78bSCy Schubert buf[i++] = 'H';
320*7f2fe78bSCy Schubert if (cred->ticket_flags & TKT_FLG_PRE_AUTH)
321*7f2fe78bSCy Schubert buf[i++] = 'A';
322*7f2fe78bSCy Schubert buf[i] = '\0';
323*7f2fe78bSCy Schubert return(buf);
324*7f2fe78bSCy Schubert }
325*7f2fe78bSCy Schubert
printtime(krb5_timestamp ts)326*7f2fe78bSCy Schubert void printtime(krb5_timestamp ts)
327*7f2fe78bSCy Schubert {
328*7f2fe78bSCy Schubert char fmtbuf[18], fill = ' ';
329*7f2fe78bSCy Schubert
330*7f2fe78bSCy Schubert if (!krb5_timestamp_to_sfstring(ts, fmtbuf, sizeof(fmtbuf), &fill))
331*7f2fe78bSCy Schubert printf("%s", fmtbuf);
332*7f2fe78bSCy Schubert }
333*7f2fe78bSCy Schubert
334*7f2fe78bSCy Schubert
335*7f2fe78bSCy Schubert krb5_error_code
krb5_get_login_princ(luser,princ_list)336*7f2fe78bSCy Schubert krb5_get_login_princ(luser, princ_list)
337*7f2fe78bSCy Schubert const char *luser;
338*7f2fe78bSCy Schubert char ***princ_list;
339*7f2fe78bSCy Schubert {
340*7f2fe78bSCy Schubert struct stat sbuf;
341*7f2fe78bSCy Schubert struct passwd *pwd;
342*7f2fe78bSCy Schubert char pbuf[MAXPATHLEN];
343*7f2fe78bSCy Schubert FILE *fp;
344*7f2fe78bSCy Schubert char * linebuf;
345*7f2fe78bSCy Schubert char *newline;
346*7f2fe78bSCy Schubert int gobble, result;
347*7f2fe78bSCy Schubert char ** buf_out;
348*7f2fe78bSCy Schubert struct stat st_temp;
349*7f2fe78bSCy Schubert int count = 0, chunk_count = 1;
350*7f2fe78bSCy Schubert
351*7f2fe78bSCy Schubert /* no account => no access */
352*7f2fe78bSCy Schubert
353*7f2fe78bSCy Schubert if ((pwd = getpwnam(luser)) == NULL) {
354*7f2fe78bSCy Schubert return 0;
355*7f2fe78bSCy Schubert }
356*7f2fe78bSCy Schubert result = snprintf(pbuf, sizeof(pbuf), "%s/.k5login", pwd->pw_dir);
357*7f2fe78bSCy Schubert if (SNPRINTF_OVERFLOW(result, sizeof(pbuf))) {
358*7f2fe78bSCy Schubert fprintf(stderr, _("home directory path for %s too long\n"), luser);
359*7f2fe78bSCy Schubert exit (1);
360*7f2fe78bSCy Schubert }
361*7f2fe78bSCy Schubert
362*7f2fe78bSCy Schubert if (stat(pbuf, &st_temp)) { /* not accessible */
363*7f2fe78bSCy Schubert return 0;
364*7f2fe78bSCy Schubert }
365*7f2fe78bSCy Schubert
366*7f2fe78bSCy Schubert
367*7f2fe78bSCy Schubert /* open ~/.k5login */
368*7f2fe78bSCy Schubert if ((fp = fopen(pbuf, "r")) == NULL) {
369*7f2fe78bSCy Schubert return 0;
370*7f2fe78bSCy Schubert }
371*7f2fe78bSCy Schubert /*
372*7f2fe78bSCy Schubert * For security reasons, the .k5login file must be owned either by
373*7f2fe78bSCy Schubert * the user himself, or by root. Otherwise, don't grant access.
374*7f2fe78bSCy Schubert */
375*7f2fe78bSCy Schubert if (fstat(fileno(fp), &sbuf)) {
376*7f2fe78bSCy Schubert fclose(fp);
377*7f2fe78bSCy Schubert return 0;
378*7f2fe78bSCy Schubert }
379*7f2fe78bSCy Schubert if ((sbuf.st_uid != pwd->pw_uid) && sbuf.st_uid) {
380*7f2fe78bSCy Schubert fclose(fp);
381*7f2fe78bSCy Schubert return 0;
382*7f2fe78bSCy Schubert }
383*7f2fe78bSCy Schubert
384*7f2fe78bSCy Schubert /* check each line */
385*7f2fe78bSCy Schubert
386*7f2fe78bSCy Schubert
387*7f2fe78bSCy Schubert if( !(linebuf = (char *) calloc (BUFSIZ, sizeof(char)))) return ENOMEM;
388*7f2fe78bSCy Schubert
389*7f2fe78bSCy Schubert if (!(buf_out = (char **) malloc( CHUNK * sizeof(char *)))) return ENOMEM;
390*7f2fe78bSCy Schubert
391*7f2fe78bSCy Schubert while ( fgets(linebuf, BUFSIZ, fp) != NULL) {
392*7f2fe78bSCy Schubert /* null-terminate the input string */
393*7f2fe78bSCy Schubert linebuf[BUFSIZ-1] = '\0';
394*7f2fe78bSCy Schubert newline = NULL;
395*7f2fe78bSCy Schubert /* nuke the newline if it exists */
396*7f2fe78bSCy Schubert if ((newline = strchr(linebuf, '\n')))
397*7f2fe78bSCy Schubert *newline = '\0';
398*7f2fe78bSCy Schubert
399*7f2fe78bSCy Schubert buf_out[count] = linebuf;
400*7f2fe78bSCy Schubert count ++;
401*7f2fe78bSCy Schubert
402*7f2fe78bSCy Schubert if (count == (chunk_count * CHUNK -1)){
403*7f2fe78bSCy Schubert chunk_count ++;
404*7f2fe78bSCy Schubert if (!(buf_out = (char **) realloc(buf_out,
405*7f2fe78bSCy Schubert chunk_count * CHUNK * sizeof(char *)))){
406*7f2fe78bSCy Schubert return ENOMEM;
407*7f2fe78bSCy Schubert }
408*7f2fe78bSCy Schubert }
409*7f2fe78bSCy Schubert
410*7f2fe78bSCy Schubert /* clean up the rest of the line if necessary */
411*7f2fe78bSCy Schubert if (!newline)
412*7f2fe78bSCy Schubert while (((gobble = getc(fp)) != EOF) && gobble != '\n');
413*7f2fe78bSCy Schubert
414*7f2fe78bSCy Schubert if( !(linebuf = (char *) calloc (BUFSIZ, sizeof(char)))) return ENOMEM;
415*7f2fe78bSCy Schubert }
416*7f2fe78bSCy Schubert
417*7f2fe78bSCy Schubert buf_out[count] = NULL;
418*7f2fe78bSCy Schubert *princ_list = buf_out;
419*7f2fe78bSCy Schubert fclose(fp);
420*7f2fe78bSCy Schubert return 0;
421*7f2fe78bSCy Schubert }
422*7f2fe78bSCy Schubert
423*7f2fe78bSCy Schubert
424*7f2fe78bSCy Schubert
425*7f2fe78bSCy Schubert void
show_credential(context,cred,cc)426*7f2fe78bSCy Schubert show_credential(context, cred, cc)
427*7f2fe78bSCy Schubert krb5_context context;
428*7f2fe78bSCy Schubert krb5_creds *cred;
429*7f2fe78bSCy Schubert krb5_ccache cc;
430*7f2fe78bSCy Schubert {
431*7f2fe78bSCy Schubert krb5_error_code retval;
432*7f2fe78bSCy Schubert char *name, *sname, *flags;
433*7f2fe78bSCy Schubert int first = 1;
434*7f2fe78bSCy Schubert krb5_principal princ;
435*7f2fe78bSCy Schubert char * defname;
436*7f2fe78bSCy Schubert int show_flags =1;
437*7f2fe78bSCy Schubert
438*7f2fe78bSCy Schubert retval = krb5_unparse_name(context, cred->client, &name);
439*7f2fe78bSCy Schubert if (retval) {
440*7f2fe78bSCy Schubert com_err(prog_name, retval, _("while unparsing client name"));
441*7f2fe78bSCy Schubert return;
442*7f2fe78bSCy Schubert }
443*7f2fe78bSCy Schubert retval = krb5_unparse_name(context, cred->server, &sname);
444*7f2fe78bSCy Schubert if (retval) {
445*7f2fe78bSCy Schubert com_err(prog_name, retval, _("while unparsing server name"));
446*7f2fe78bSCy Schubert free(name);
447*7f2fe78bSCy Schubert return;
448*7f2fe78bSCy Schubert }
449*7f2fe78bSCy Schubert
450*7f2fe78bSCy Schubert if ((retval = krb5_cc_get_principal(context, cc, &princ))) {
451*7f2fe78bSCy Schubert com_err(prog_name, retval, _("while retrieving principal name"));
452*7f2fe78bSCy Schubert return;
453*7f2fe78bSCy Schubert }
454*7f2fe78bSCy Schubert if ((retval = krb5_unparse_name(context, princ, &defname))) {
455*7f2fe78bSCy Schubert com_err(prog_name, retval, _("while unparsing principal name"));
456*7f2fe78bSCy Schubert return;
457*7f2fe78bSCy Schubert }
458*7f2fe78bSCy Schubert
459*7f2fe78bSCy Schubert if (!cred->times.starttime)
460*7f2fe78bSCy Schubert cred->times.starttime = cred->times.authtime;
461*7f2fe78bSCy Schubert
462*7f2fe78bSCy Schubert printtime(cred->times.starttime);
463*7f2fe78bSCy Schubert putchar(' '); putchar(' ');
464*7f2fe78bSCy Schubert printtime(cred->times.endtime);
465*7f2fe78bSCy Schubert putchar(' '); putchar(' ');
466*7f2fe78bSCy Schubert
467*7f2fe78bSCy Schubert printf("%s\n", sname);
468*7f2fe78bSCy Schubert
469*7f2fe78bSCy Schubert if (strcmp(name, defname)) {
470*7f2fe78bSCy Schubert printf(_("\tfor client %s"), name);
471*7f2fe78bSCy Schubert first = 0;
472*7f2fe78bSCy Schubert }
473*7f2fe78bSCy Schubert
474*7f2fe78bSCy Schubert if (cred->times.renew_till) {
475*7f2fe78bSCy Schubert if (first)
476*7f2fe78bSCy Schubert fputs("\t",stdout);
477*7f2fe78bSCy Schubert else
478*7f2fe78bSCy Schubert fputs(", ",stdout);
479*7f2fe78bSCy Schubert fputs(_("renew until "), stdout);
480*7f2fe78bSCy Schubert printtime(cred->times.renew_till);
481*7f2fe78bSCy Schubert }
482*7f2fe78bSCy Schubert if (show_flags) {
483*7f2fe78bSCy Schubert flags = flags_string(cred);
484*7f2fe78bSCy Schubert if (flags && *flags) {
485*7f2fe78bSCy Schubert if (first)
486*7f2fe78bSCy Schubert fputs("\t",stdout);
487*7f2fe78bSCy Schubert else
488*7f2fe78bSCy Schubert fputs(", ",stdout);
489*7f2fe78bSCy Schubert printf(_("Flags: %s"), flags);
490*7f2fe78bSCy Schubert first = 0;
491*7f2fe78bSCy Schubert }
492*7f2fe78bSCy Schubert }
493*7f2fe78bSCy Schubert putchar('\n');
494*7f2fe78bSCy Schubert free(name);
495*7f2fe78bSCy Schubert free(sname);
496*7f2fe78bSCy Schubert }
497*7f2fe78bSCy Schubert
498*7f2fe78bSCy Schubert /* Create a random string suitable for a filename extension. */
499*7f2fe78bSCy Schubert krb5_error_code
gen_sym(krb5_context context,char ** sym_out)500*7f2fe78bSCy Schubert gen_sym(krb5_context context, char **sym_out)
501*7f2fe78bSCy Schubert {
502*7f2fe78bSCy Schubert krb5_error_code retval;
503*7f2fe78bSCy Schubert char bytes[6], *p, *sym;
504*7f2fe78bSCy Schubert krb5_data data = make_data(bytes, sizeof(bytes));
505*7f2fe78bSCy Schubert
506*7f2fe78bSCy Schubert *sym_out = NULL;
507*7f2fe78bSCy Schubert retval = krb5_c_random_make_octets(context, &data);
508*7f2fe78bSCy Schubert if (retval)
509*7f2fe78bSCy Schubert return retval;
510*7f2fe78bSCy Schubert sym = k5_base64_encode(data.data, data.length);
511*7f2fe78bSCy Schubert if (sym == NULL)
512*7f2fe78bSCy Schubert return ENOMEM;
513*7f2fe78bSCy Schubert /* Tweak the output alphabet just a bit. */
514*7f2fe78bSCy Schubert while ((p = strchr(sym, '/')) != NULL)
515*7f2fe78bSCy Schubert *p = '_';
516*7f2fe78bSCy Schubert while ((p = strchr(sym, '+')) != NULL)
517*7f2fe78bSCy Schubert *p = '-';
518*7f2fe78bSCy Schubert *sym_out = sym;
519*7f2fe78bSCy Schubert return 0;
520*7f2fe78bSCy Schubert }
521*7f2fe78bSCy Schubert
krb5_ccache_overwrite(context,ccs,cct,primary_principal)522*7f2fe78bSCy Schubert krb5_error_code krb5_ccache_overwrite(context, ccs, cct, primary_principal)
523*7f2fe78bSCy Schubert krb5_context context;
524*7f2fe78bSCy Schubert krb5_ccache ccs;
525*7f2fe78bSCy Schubert krb5_ccache cct;
526*7f2fe78bSCy Schubert krb5_principal primary_principal;
527*7f2fe78bSCy Schubert {
528*7f2fe78bSCy Schubert krb5_error_code retval=0;
529*7f2fe78bSCy Schubert krb5_principal temp_principal;
530*7f2fe78bSCy Schubert krb5_creds ** ccs_creds_arr = NULL;
531*7f2fe78bSCy Schubert int i=0;
532*7f2fe78bSCy Schubert
533*7f2fe78bSCy Schubert if (ks_ccache_is_initialized(context, ccs)) {
534*7f2fe78bSCy Schubert if ((retval = krb5_get_nonexp_tkts(context, ccs, &ccs_creds_arr))){
535*7f2fe78bSCy Schubert return retval;
536*7f2fe78bSCy Schubert }
537*7f2fe78bSCy Schubert }
538*7f2fe78bSCy Schubert
539*7f2fe78bSCy Schubert if (ks_ccache_is_initialized(context, cct)) {
540*7f2fe78bSCy Schubert if ((retval = krb5_cc_get_principal(context, cct, &temp_principal))){
541*7f2fe78bSCy Schubert return retval;
542*7f2fe78bSCy Schubert }
543*7f2fe78bSCy Schubert }else{
544*7f2fe78bSCy Schubert temp_principal = primary_principal;
545*7f2fe78bSCy Schubert }
546*7f2fe78bSCy Schubert
547*7f2fe78bSCy Schubert if ((retval = krb5_cc_initialize(context, cct, temp_principal))){
548*7f2fe78bSCy Schubert return retval;
549*7f2fe78bSCy Schubert }
550*7f2fe78bSCy Schubert
551*7f2fe78bSCy Schubert retval = krb5_store_all_creds(context, cct, ccs_creds_arr, NULL);
552*7f2fe78bSCy Schubert
553*7f2fe78bSCy Schubert if (ccs_creds_arr){
554*7f2fe78bSCy Schubert while (ccs_creds_arr[i]){
555*7f2fe78bSCy Schubert krb5_free_creds(context, ccs_creds_arr[i]);
556*7f2fe78bSCy Schubert i++;
557*7f2fe78bSCy Schubert }
558*7f2fe78bSCy Schubert }
559*7f2fe78bSCy Schubert
560*7f2fe78bSCy Schubert return retval;
561*7f2fe78bSCy Schubert }
562*7f2fe78bSCy Schubert
krb5_store_some_creds(context,cc,creds_def,creds_other,prst,stored)563*7f2fe78bSCy Schubert krb5_error_code krb5_store_some_creds(context, cc, creds_def, creds_other, prst,
564*7f2fe78bSCy Schubert stored)
565*7f2fe78bSCy Schubert krb5_context context;
566*7f2fe78bSCy Schubert krb5_ccache cc;
567*7f2fe78bSCy Schubert krb5_creds **creds_def;
568*7f2fe78bSCy Schubert krb5_creds **creds_other;
569*7f2fe78bSCy Schubert krb5_principal prst;
570*7f2fe78bSCy Schubert krb5_boolean *stored;
571*7f2fe78bSCy Schubert {
572*7f2fe78bSCy Schubert
573*7f2fe78bSCy Schubert int i = 0;
574*7f2fe78bSCy Schubert krb5_error_code retval = 0;
575*7f2fe78bSCy Schubert krb5_creds ** temp_creds= NULL;
576*7f2fe78bSCy Schubert krb5_boolean temp_stored = FALSE;
577*7f2fe78bSCy Schubert
578*7f2fe78bSCy Schubert
579*7f2fe78bSCy Schubert if ((creds_def == NULL) && (creds_other == NULL))
580*7f2fe78bSCy Schubert return 0;
581*7f2fe78bSCy Schubert
582*7f2fe78bSCy Schubert if ((creds_def == NULL) && (creds_other != NULL))
583*7f2fe78bSCy Schubert temp_creds = creds_other;
584*7f2fe78bSCy Schubert
585*7f2fe78bSCy Schubert if ((creds_def != NULL) && (creds_other == NULL))
586*7f2fe78bSCy Schubert temp_creds = creds_def;
587*7f2fe78bSCy Schubert
588*7f2fe78bSCy Schubert
589*7f2fe78bSCy Schubert if (temp_creds){
590*7f2fe78bSCy Schubert while(temp_creds[i]){
591*7f2fe78bSCy Schubert if (krb5_principal_compare(context,
592*7f2fe78bSCy Schubert temp_creds[i]->client,
593*7f2fe78bSCy Schubert prst)== TRUE) {
594*7f2fe78bSCy Schubert
595*7f2fe78bSCy Schubert if ((retval = krb5_cc_store_cred(context,
596*7f2fe78bSCy Schubert cc,temp_creds[i]))){
597*7f2fe78bSCy Schubert return retval;
598*7f2fe78bSCy Schubert }
599*7f2fe78bSCy Schubert temp_stored = TRUE;
600*7f2fe78bSCy Schubert }
601*7f2fe78bSCy Schubert
602*7f2fe78bSCy Schubert i++;
603*7f2fe78bSCy Schubert }
604*7f2fe78bSCy Schubert }
605*7f2fe78bSCy Schubert else { /* both arrays have elements in them */
606*7f2fe78bSCy Schubert return KRB5KRB_ERR_GENERIC;
607*7f2fe78bSCy Schubert }
608*7f2fe78bSCy Schubert
609*7f2fe78bSCy Schubert *stored = temp_stored;
610*7f2fe78bSCy Schubert return 0;
611*7f2fe78bSCy Schubert }
612*7f2fe78bSCy Schubert
krb5_ccache_filter(context,cc,prst)613*7f2fe78bSCy Schubert krb5_error_code krb5_ccache_filter (context, cc, prst)
614*7f2fe78bSCy Schubert krb5_context context;
615*7f2fe78bSCy Schubert krb5_ccache cc;
616*7f2fe78bSCy Schubert krb5_principal prst;
617*7f2fe78bSCy Schubert {
618*7f2fe78bSCy Schubert
619*7f2fe78bSCy Schubert int i=0;
620*7f2fe78bSCy Schubert krb5_error_code retval=0;
621*7f2fe78bSCy Schubert krb5_principal temp_principal;
622*7f2fe78bSCy Schubert krb5_creds ** cc_creds_arr = NULL;
623*7f2fe78bSCy Schubert const char * cc_name;
624*7f2fe78bSCy Schubert krb5_boolean stored;
625*7f2fe78bSCy Schubert
626*7f2fe78bSCy Schubert cc_name = krb5_cc_get_name(context, cc);
627*7f2fe78bSCy Schubert
628*7f2fe78bSCy Schubert if (ks_ccache_is_initialized(context, cc)) {
629*7f2fe78bSCy Schubert if (auth_debug) {
630*7f2fe78bSCy Schubert fprintf(stderr,"putting cache %s through a filter for -z option\n", cc_name);
631*7f2fe78bSCy Schubert }
632*7f2fe78bSCy Schubert
633*7f2fe78bSCy Schubert if ((retval = krb5_get_nonexp_tkts(context, cc, &cc_creds_arr))){
634*7f2fe78bSCy Schubert return retval;
635*7f2fe78bSCy Schubert }
636*7f2fe78bSCy Schubert
637*7f2fe78bSCy Schubert if ((retval = krb5_cc_get_principal(context, cc, &temp_principal))){
638*7f2fe78bSCy Schubert return retval;
639*7f2fe78bSCy Schubert }
640*7f2fe78bSCy Schubert
641*7f2fe78bSCy Schubert if ((retval = krb5_cc_initialize(context, cc, temp_principal))){
642*7f2fe78bSCy Schubert return retval;
643*7f2fe78bSCy Schubert }
644*7f2fe78bSCy Schubert
645*7f2fe78bSCy Schubert if ((retval = krb5_store_some_creds(context, cc, cc_creds_arr,
646*7f2fe78bSCy Schubert NULL, prst, &stored))){
647*7f2fe78bSCy Schubert return retval;
648*7f2fe78bSCy Schubert }
649*7f2fe78bSCy Schubert
650*7f2fe78bSCy Schubert if (cc_creds_arr){
651*7f2fe78bSCy Schubert while (cc_creds_arr[i]){
652*7f2fe78bSCy Schubert krb5_free_creds(context, cc_creds_arr[i]);
653*7f2fe78bSCy Schubert i++;
654*7f2fe78bSCy Schubert }
655*7f2fe78bSCy Schubert }
656*7f2fe78bSCy Schubert }
657*7f2fe78bSCy Schubert return 0;
658*7f2fe78bSCy Schubert }
659*7f2fe78bSCy Schubert
krb5_find_princ_in_cred_list(context,creds_list,princ)660*7f2fe78bSCy Schubert krb5_boolean krb5_find_princ_in_cred_list (context, creds_list, princ)
661*7f2fe78bSCy Schubert krb5_context context;
662*7f2fe78bSCy Schubert krb5_creds **creds_list;
663*7f2fe78bSCy Schubert krb5_principal princ;
664*7f2fe78bSCy Schubert {
665*7f2fe78bSCy Schubert
666*7f2fe78bSCy Schubert int i = 0;
667*7f2fe78bSCy Schubert krb5_boolean temp_stored = FALSE;
668*7f2fe78bSCy Schubert
669*7f2fe78bSCy Schubert if (creds_list){
670*7f2fe78bSCy Schubert while(creds_list[i]){
671*7f2fe78bSCy Schubert if (krb5_principal_compare(context,
672*7f2fe78bSCy Schubert creds_list[i]->client,
673*7f2fe78bSCy Schubert princ)== TRUE){
674*7f2fe78bSCy Schubert temp_stored = TRUE;
675*7f2fe78bSCy Schubert break;
676*7f2fe78bSCy Schubert }
677*7f2fe78bSCy Schubert
678*7f2fe78bSCy Schubert i++;
679*7f2fe78bSCy Schubert }
680*7f2fe78bSCy Schubert }
681*7f2fe78bSCy Schubert
682*7f2fe78bSCy Schubert return temp_stored;
683*7f2fe78bSCy Schubert }
684*7f2fe78bSCy Schubert
krb5_find_princ_in_cache(context,cc,princ,found)685*7f2fe78bSCy Schubert krb5_error_code krb5_find_princ_in_cache (context, cc, princ, found)
686*7f2fe78bSCy Schubert krb5_context context;
687*7f2fe78bSCy Schubert krb5_ccache cc;
688*7f2fe78bSCy Schubert krb5_principal princ;
689*7f2fe78bSCy Schubert krb5_boolean *found;
690*7f2fe78bSCy Schubert {
691*7f2fe78bSCy Schubert krb5_error_code retval;
692*7f2fe78bSCy Schubert krb5_creds ** creds_list = NULL;
693*7f2fe78bSCy Schubert
694*7f2fe78bSCy Schubert if (ks_ccache_is_initialized(context, cc)) {
695*7f2fe78bSCy Schubert if ((retval = krb5_get_nonexp_tkts(context, cc, &creds_list))){
696*7f2fe78bSCy Schubert return retval;
697*7f2fe78bSCy Schubert }
698*7f2fe78bSCy Schubert }
699*7f2fe78bSCy Schubert
700*7f2fe78bSCy Schubert *found = krb5_find_princ_in_cred_list(context, creds_list, princ);
701*7f2fe78bSCy Schubert return 0;
702*7f2fe78bSCy Schubert }
703*7f2fe78bSCy Schubert
704*7f2fe78bSCy Schubert krb5_boolean
ks_ccache_name_is_initialized(krb5_context context,const char * cctag)705*7f2fe78bSCy Schubert ks_ccache_name_is_initialized(krb5_context context, const char *cctag)
706*7f2fe78bSCy Schubert {
707*7f2fe78bSCy Schubert krb5_boolean result;
708*7f2fe78bSCy Schubert krb5_ccache cc;
709*7f2fe78bSCy Schubert
710*7f2fe78bSCy Schubert if (krb5_cc_resolve(context, cctag, &cc) != 0)
711*7f2fe78bSCy Schubert return FALSE;
712*7f2fe78bSCy Schubert result = ks_ccache_is_initialized(context, cc);
713*7f2fe78bSCy Schubert krb5_cc_close(context, cc);
714*7f2fe78bSCy Schubert
715*7f2fe78bSCy Schubert return result;
716*7f2fe78bSCy Schubert }
717*7f2fe78bSCy Schubert
718*7f2fe78bSCy Schubert krb5_boolean
ks_ccache_is_initialized(krb5_context context,krb5_ccache cc)719*7f2fe78bSCy Schubert ks_ccache_is_initialized(krb5_context context, krb5_ccache cc)
720*7f2fe78bSCy Schubert {
721*7f2fe78bSCy Schubert krb5_principal princ;
722*7f2fe78bSCy Schubert krb5_error_code retval;
723*7f2fe78bSCy Schubert
724*7f2fe78bSCy Schubert if (cc == NULL)
725*7f2fe78bSCy Schubert return FALSE;
726*7f2fe78bSCy Schubert
727*7f2fe78bSCy Schubert retval = krb5_cc_get_principal(context, cc, &princ);
728*7f2fe78bSCy Schubert if (retval == 0)
729*7f2fe78bSCy Schubert krb5_free_principal(context, princ);
730*7f2fe78bSCy Schubert
731*7f2fe78bSCy Schubert return retval == 0;
732*7f2fe78bSCy Schubert }
733