source: trunk/athena/bin/dash/klist.c @ 12537

Revision 12537, 5.0 KB checked in by danw, 26 years ago (diff)
from mwhitson: don't assume sizeof(int) == sizeof(void *)
Line 
1/*
2 * $Id: klist.c,v 1.3 1999-02-22 18:21:34 danw Exp $
3 *
4 * Copyright 1990, 1991 by the Massachusetts Institute of Technology.
5 *
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
8 *
9 */
10
11#if  (!defined(lint))  &&  (!defined(SABER))
12static char *rcsid =
13"$Id: klist.c,v 1.3 1999-02-22 18:21:34 danw Exp $";
14#endif
15
16#include "mit-copyright.h"
17#include <stdio.h>
18#include <errno.h>
19#include <string.h>
20#include <sys/types.h>
21#include <sys/file.h>
22#include <sys/stat.h>
23#include <krb.h>
24#include <Jets/Jets.h>
25#include <Jets/Button.h>
26#include <Jets/warn.h>
27
28
29char   *tkt_string();
30char   *getenv();
31
32#ifndef TICKET_GRANTING_TICKET
33#define TICKET_GRANTING_TICKET  "krbtgt"
34#endif
35
36#define WARN1_TIME 15
37#define WARN2_TIME 5
38
39extern Jet root;
40
41static Warning *old_warn = NULL;
42static int ok();
43
44void checkTkts(info, id)
45     int info, id;
46{
47  char pname[ANAME_SZ];
48  char pinst[INST_SZ];
49  char prealm[REALM_SZ];
50  int k_errno;
51  CREDENTIALS c;
52  char *file;
53  int ret = 0;
54  static int old_ret = 0;
55  static time_t mod_time = 0;
56  static long exp_time;
57  struct stat statbuf;
58  int diff;
59  unsigned int timeout = 5*60*1000;     /* 5 minutes... */
60  char line1[100];
61  char *line2 = "Type `renew' to re-authenticate.";
62
63  line1[0] = '\0';
64
65  if ((file = getenv("KRBTKFILE")) == NULL)
66    file = TKT_FILE;
67
68
69  if (stat(file, &statbuf))
70    {
71      sprintf(line1, "%s: `%s'", strerror(errno), file);
72
73      ret = 1;
74      goto done;
75    }
76
77  if (statbuf.st_mtime != mod_time)
78    mod_time = statbuf.st_mtime;
79  else  if (exp_time - time(0) > 15 * 60)
80    {
81      ret = 0;
82      goto done;
83    }
84
85  /*
86   * Since krb_get_tf_realm will return a ticket_file error,
87   * we will call tf_init and tf_close first to filter out
88   * things like no ticket file.  Otherwise, the error that
89   * the user would see would be
90   * klist: can't find realm of ticket file: No ticket file (tf_util)
91   * instead of
92   * klist: No ticket file (tf_util)
93   */
94
95  /* Open ticket file */
96  if (k_errno = tf_init(file, R_TKT_FIL))
97    {
98      sprintf(line1, "%s", krb_err_txt[k_errno]);
99      ret = 1;
100      goto done;
101    }
102  /* Close ticket file */
103  (void) tf_close();
104
105  /*
106   * We must find the realm of the ticket file here before calling
107   * tf_init because since the realm of the ticket file is not
108   * really stored in the principal section of the file, the
109   * routine we use must itself call tf_init and tf_close.
110   */
111  if ((k_errno = krb_get_tf_realm(file, prealm)) != KSUCCESS)
112    {
113      sprintf(line1, "can't find realm of ticket file: %s",
114              krb_err_txt[k_errno]);
115      ret = 1;
116      goto done;
117    }
118
119  /* Open ticket file, get principal name and instance */
120  if ((k_errno = tf_init(file, R_TKT_FIL)) ||
121      (k_errno = tf_get_pname(pname)) ||
122      (k_errno = tf_get_pinst(pinst)))
123    {
124      sprintf(line1, "%s", krb_err_txt[k_errno]);
125      ret = 1;
126      goto done;
127    }
128
129  /*
130   * You may think that this is the obvious place to get the
131   * realm of the ticket file, but it can't be done here as the
132   * routine to do this must open the ticket file.  This is why
133   * it was done before tf_init.
134   */
135       
136  while ((k_errno = tf_get_cred(&c)) == KSUCCESS)
137    {
138      if (!strcmp(c.service, TICKET_GRANTING_TICKET) &&
139          !strcmp(c.instance, prealm))
140        {
141          exp_time = c.issue_date + ((unsigned char) c.lifetime) * 5 * 60;
142          diff = exp_time - time(0);
143
144          if (diff < 0)
145            {
146              strcpy(line1, "Your authentication has expired.");
147              ret = 3;                  /* has expired */
148              goto done;
149            }
150
151          if (diff < WARN1_TIME * 60)   /* inside of 15 minutes? */
152            {
153              char *expire_str =
154                "Your authentication will expire in less than %d minutes.";
155              timeout = 60*1000;        /* set timeout to 1 minute... */
156
157              if (diff < WARN2_TIME * 60)       /* inside of 5 minutes? */
158                {
159                  sprintf(line1, expire_str, WARN2_TIME);
160                  ret = 2;
161                  goto done;
162                }
163              sprintf(line1, expire_str, WARN1_TIME);
164              ret = 1;
165              goto done;
166            }
167
168          ret = 0;                      /* tgt hasn't expired */
169          goto done;
170        }
171      continue;                 /* not a tgt */
172    }
173
174  strcpy(line1, "You have no authentication.");
175  ret = 1;                      /* no tgt found */
176
177
178
179 done:
180  if (ret  &&  (old_ret != ret))
181    {
182      Warning *w;
183
184      /* Destroy last warning if user hasn't clicked it away already */
185      if (old_warn != NULL)
186        XjCallCallbacks((caddr_t) old_warn,
187                        old_warn->button->button.activateProc, NULL);
188
189      w = (Warning *)XjMalloc((unsigned) sizeof(Warning));
190
191      w->me.next = NULL;
192      w->me.argType = argPtr;
193      w->me.passPtr = w;
194      w->me.proc = ok;
195
196      w->l1 = XjNewString(line1);
197      w->l2 = XjNewString(line2);
198
199      old_warn = XjUserWarning(root, w, True, line1, line2);
200    }
201  old_ret = ret;
202  (void) tf_close();
203  XjAddWakeup(checkTkts, NULL, timeout);
204  /*  return ret;  */
205}
206
207static int ok(who, w, data)
208     Jet who;
209     Warning *w;
210     caddr_t data;
211{
212  Display *dpy;
213
214  dpy = w->top->core.display;   /* save off the display before */
215                                /* destroying the Jet */
216  XjDestroyJet(w->top);
217  XFlush(dpy);
218
219  XjFree(w->l1);
220  XjFree(w->l2);
221  XjFree((char *) w);
222  old_warn = NULL;
223  return 0;
224}
Note: See TracBrowser for help on using the repository browser.