1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
|
/*
* tktlist.c
*
* Handle all actions of the Kerberos ticket list.
*
* Copyright 1994 by the Massachusetts Institute of Technology.
*
* For copying and distribution information, please see the file
* <mit-copyright.h>.
*/
#if !defined(KRB5) && !defined(KRB4)
#define KRB5 1
#endif
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <assert.h>
#include <malloc.h>
#include <string.h>
#include <time.h>
#ifdef KRB4
#include "mit-copyright.h"
#include "kerberos.h"
#endif
#ifdef KRB5
#include "winsock.h"
#include "krb5.h"
#include "com_err.h"
#endif
#include "cns.h"
#include "tktlist.h"
/*
* Ticket information for a list line
*/
typedef struct {
BOOL ticket; /* TRUE if this is a real ticket */
time_t issue_time; /* time_t of issue */
long lifetime; /* Lifetime for ticket in 5 minute intervals */
char buf[0]; /* String to display */
} TICKETINFO, *LPTICKETINFO;
/*
* Function: Returns a standard ctime date with day of week and year
* removed.
*
* Parameters:
* t - time_t date to convert
*
* Returns: A pointer to the adjusted time value.
*/
static char *
short_date (time_t t)
{
static char buf[26 - 4];
char *p;
p = ctime(&t);
assert(p != NULL);
strcpy (buf, p + 4);
buf[12] = '\0';
return buf;
}
/*+
* Function: Initializes and populates the ticket list with all existing
* Kerberos tickets.
*
* Parameters:
* hwnd - the window handle of the ticket window.
*
* Returns: Number of elements in the list or -1 on error
*/
int
ticket_init_list (HWND hwnd)
{
int ncred;
LRESULT rc;
int l;
LPTICKETINFO lpinfo;
char buf[26+2 + 26+2 + ANAME_SZ+1 + INST_SZ+1 + REALM_SZ + 22];
#ifdef KRB4
int i;
time_t expiration;
char service[ANAME_SZ];
char instance[INST_SZ];
char realm[REALM_SZ];
CREDENTIALS c;
#endif
#ifdef KRB5
krb5_cc_cursor cursor;
krb5_error_code code;
krb5_creds c;
krb5_flags flags;
char *sname; /* Name of the service */
char *flags_string(krb5_creds *cred);
#endif
SetWindowRedraw(hwnd, FALSE);
rc = ListBox_GetCount(hwnd);
assert(rc != LB_ERR);
if (rc > 0)
ticket_destroy(hwnd);
while (--rc >= 0)
ListBox_DeleteString(hwnd, rc);
#ifdef KRB4
ncred = krb_get_num_cred();
for (i = 1; i <= ncred; i++) {
krb_get_nth_cred(service, instance, realm, i);
krb_get_cred(service, instance, realm, &c);
strcpy(buf, " ");
strncat(buf, short_date(c.issue_date - kwin_get_epoch()),
sizeof(buf) - 1 - strlen(buf));
expiration = c.issue_date - kwin_get_epoch() + (long) c.lifetime * 5L * 60L;
strncat(buf, " ", sizeof(buf) - 1 - strlen(buf));
strncat(buf, short_date(expiration), sizeof(buf) - 1 - strlen(buf));
strncat(buf, " ", sizeof(buf) - 1 - strlen(buf));
l = strlen(buf);
sprintf(&buf[l], "%s%s%s%s%s (%d)",
c.service, (c.instance[0] ? "." : ""), c.instance,
(c.realm[0] ? "@" : ""), c.realm, c.kvno);
l = strlen(buf);
lpinfo = (LPTICKETINFO) malloc(sizeof(TICKETINFO) + l + 1);
assert(lpinfo != NULL);
if (lpinfo == NULL)
return -1;
lpinfo->ticket = TRUE;
lpinfo->issue_time = c.issue_date - kwin_get_epoch(); /* back to system time */
lpinfo->lifetime = (long) c.lifetime * 5L * 60L;
strcpy(lpinfo->buf, buf);
rc = ListBox_AddItemData(hwnd, lpinfo);
assert(rc >= 0);
if (rc < 0)
return -1;
}
#endif
#ifdef KRB5
ncred = 0;
flags = 0;
if (code = krb5_cc_set_flags(k5_context, k5_ccache, flags)) {
if (code != KRB5_FCC_NOFILE) {
return -1;
}
} else {
if (code = krb5_cc_start_seq_get(k5_context, k5_ccache, &cursor)) {
return -1;
}
while (1) {
code = krb5_cc_next_cred(k5_context, k5_ccache, &cursor, &c);
if (code != 0)
break;
ncred++;
strcpy (buf, " ");
strncat(buf, short_date (c.times.starttime - kwin_get_epoch()),
sizeof(buf) - 1 - strlen(buf));
strncat(buf, " ", sizeof(buf) - 1 - strlen(buf));
strncat(buf, short_date (c.times.endtime - kwin_get_epoch()),
sizeof(buf) - 1 - strlen(buf));
strncat(buf, " ", sizeof(buf) - 1 - strlen(buf));
/* Add ticket service name and realm */
code = krb5_unparse_name (k5_context, c.server, &sname);
if (code) {
com_err (NULL, code, "while unparsing server name");
break;
}
strncat (buf, sname, sizeof(buf) - 1 - strlen(buf));
strncat (buf, flags_string (&c), sizeof(buf) - 1 - strlen(buf)); /* Add flag info */
l = strlen(buf);
lpinfo = (LPTICKETINFO) malloc(sizeof(TICKETINFO) + l + 1);
assert(lpinfo != NULL);
if (lpinfo == NULL)
return -1;
lpinfo->ticket = TRUE;
lpinfo->issue_time = c.times.starttime - kwin_get_epoch();
lpinfo->lifetime = c.times.endtime - c.times.starttime;
strcpy(lpinfo->buf, buf);
rc = ListBox_AddItemData(hwnd, lpinfo);
assert(rc >= 0);
if (rc < 0)
return -1;
}
if (code == KRB5_CC_END) { /* End of ccache */
if (code = krb5_cc_end_seq_get(k5_context, k5_ccache, &cursor)) {
return -1;
}
flags = KRB5_TC_OPENCLOSE; /* turns on OPENCLOSE mode */
if (code = krb5_cc_set_flags(k5_context, k5_ccache, flags)) {
return -1;
}
} else {
return -1;
}
}
#endif
if (ncred <= 0) {
strcpy(buf, " No Tickets");
lpinfo = (LPTICKETINFO) malloc(sizeof(TICKETINFO) + strlen(buf) + 1);
assert(lpinfo != NULL);
if (lpinfo == NULL)
return -1;
lpinfo->ticket = FALSE;
strcpy (lpinfo->buf, buf);
rc = ListBox_AddItemData(hwnd, lpinfo);
assert(rc >= 0);
}
SetWindowRedraw(hwnd, TRUE);
return ncred;
}
/*
* Function: Destroy the ticket list. Make sure to delete all
* ticket entries created during ticket initialization.
*
* Parameters:
* hwnd - the window handle of the ticket window.
*/
void
ticket_destroy (
HWND hwnd)
{
int i;
int n;
LRESULT rc;
n = ListBox_GetCount(hwnd);
for (i = 0; i < n; i++) {
rc = ListBox_GetItemData(hwnd, i);
assert(rc != LB_ERR);
if (rc != LB_ERR)
free ((void *) rc);
}
}
/*
* Function: Respond to the WM_MEASUREITEM message for the ticket list
* by setting each list item up at 1/4 inch hight.
*/
void
ticket_measureitem(HWND hwnd, MEASUREITEMSTRUCT *lpmi)
{
int logpixelsy;
HDC hdc;
if (lpmi->CtlID != IDD_TICKET_LIST)
return;
hdc = GetDC(HWND_DESKTOP);
logpixelsy = GetDeviceCaps(hdc, LOGPIXELSY);
ReleaseDC(HWND_DESKTOP, hdc);
lpmi->itemHeight = logpixelsy / 4; /* 1/4 inch */
}
/*
* Function: Respond to the WM_DRAWITEM message for the ticket list
* by displaying a single list item.
*/
void
ticket_drawitem(HWND hwnd, const DRAWITEMSTRUCT *lpdi)
{
BOOL rc;
COLORREF bkcolor;
HBRUSH hbrush;
UINT textheight;
UINT alignment;
int left, top;
BOOL b;
LPTICKETINFO lpinfo;
HICON hicon;
#if 0
COLORREF textcolor;
COLORREF orgbkcolor;
COLORREF orgtextcolor;
#endif
SIZE Size;
if (lpdi->CtlID != IDD_TICKET_LIST)
return;
lpinfo = (LPTICKETINFO) lpdi->itemData;
if (lpdi->itemAction == ODA_FOCUS)
return;
#if 0
if (lpdi->itemState & ODS_SELECTED) {
textcolor = GetSysColor(COLOR_HIGHLIGHTTEXT);
bkcolor = GetSysColor(COLOR_HIGHLIGHT);
orgtextcolor = SetTextColor(lpdi->hDC, textcolor);
assert(textcolor != 0x80000000);
orgbkcolor = SetBkColor(lpdi->hDC, bkcolor);
assert(bkcolor != 0x80000000);
}
else
#endif
bkcolor = GetBkColor(lpdi->hDC);
hbrush = CreateSolidBrush(bkcolor);
assert(hbrush != NULL);
FillRect(lpdi->hDC, &(lpdi->rcItem), hbrush);
DeleteObject(hbrush);
/*
* Display the appropriate icon
*/
if (lpinfo->ticket) {
hicon = kwin_get_icon(lpinfo->issue_time + lpinfo->lifetime);
left = lpdi->rcItem.left - (32 - ICON_WIDTH) / 2;
top = lpdi->rcItem.top;
top += (lpdi->rcItem.bottom - lpdi->rcItem.top - 32) / 2;
b = DrawIcon(lpdi->hDC, left, top, hicon);
assert(b);
}
/*
* Display centered string
*/
#ifdef _WIN32
GetTextExtentPoint32(lpdi->hDC, "X", 1, &Size);
#else
GetTextExtentPoint(lpdi->hDC, "X", 1, &Size);
#endif
textheight = Size.cy;
alignment = SetTextAlign(lpdi->hDC, TA_TOP | TA_LEFT);
if (lpinfo->ticket)
left = lpdi->rcItem.left + ICON_WIDTH;
else
left = lpdi->rcItem.left;
top = lpdi->rcItem.top;
top += (lpdi->rcItem.bottom - lpdi->rcItem.top - textheight) / 2;
rc = TextOut(lpdi->hDC, left, top, (LPSTR) lpinfo->buf,
strlen((LPSTR) lpinfo->buf));
assert(rc);
alignment = SetTextAlign(lpdi->hDC, alignment);
#if 0
if (lpdi->itemState & ODS_SELECTED) {
textcolor = SetTextColor(lpdi->hDC, orgtextcolor);
assert(textcolor != 0x80000000);
bkcolor = SetBkColor(lpdi->hDC, orgbkcolor);
assert(bkcolor != 0x80000000);
}
#endif
}
#ifdef KRB5
/*
*
* Flags_string
*
* Return buffer with the current flags for the credential
*
*/
char *
flags_string(krb5_creds *cred) {
static char buf[32];
int i = 0;
buf[i++] = ' ';
buf[i++] = '(';
if (cred->ticket_flags & TKT_FLG_FORWARDABLE)
buf[i++] = 'F';
if (cred->ticket_flags & TKT_FLG_FORWARDED)
buf[i++] = 'f';
if (cred->ticket_flags & TKT_FLG_PROXIABLE)
buf[i++] = 'P';
if (cred->ticket_flags & TKT_FLG_PROXY)
buf[i++] = 'p';
if (cred->ticket_flags & TKT_FLG_MAY_POSTDATE)
buf[i++] = 'D';
if (cred->ticket_flags & TKT_FLG_POSTDATED)
buf[i++] = 'd';
if (cred->ticket_flags & TKT_FLG_INVALID)
buf[i++] = 'i';
if (cred->ticket_flags & TKT_FLG_RENEWABLE)
buf[i++] = 'R';
if (cred->ticket_flags & TKT_FLG_INITIAL)
buf[i++] = 'I';
if (cred->ticket_flags & TKT_FLG_HW_AUTH)
buf[i++] = 'H';
if (cred->ticket_flags & TKT_FLG_PRE_AUTH)
buf[i++] = 'A';
buf[i++] = ')';
buf[i] = '\0';
if (i <= 3)
buf[0] = '\0';
return(buf);
}
#endif /* KRB5 */
|