home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume22 / bigb / part01 / find_term.c < prev    next >
C/C++ Source or Header  |  1991-09-13  |  2KB  |  74 lines

  1. /* This routine tries to find which terminal a login attempt was made on.
  2.  * I freely admit that it is a disgusting hack - there does not seem to be
  3.  * any reliable way of finding out on which terminal a failed login
  4.  * attempt was made.  This routine just goes through all terminals in
  5.  * turn and compares the uid and time of the last suitable login attempt.
  6.  * As if that was not bad enough the times in general do not match exactly
  7.  * so I look for times that match to within 5 seconds.  Also 60 seconds
  8.  * after a failed login the terminal times out, this updates the time flag
  9.  * so I also check for a terminal failure 60+-5 seconds after the login
  10.  * failure!
  11. */
  12.  
  13. #include "bigb.h"
  14. #include <string.h>
  15. #include <time.h>
  16.  
  17. /* Return the data is this buffer */
  18. PRIVATE char    devname[20];
  19.  
  20. /* Declare the local routine */
  21. PRIVATE int check_term_for_match(const struct pr_term *, int, int, time_t);
  22.  
  23. PUBLIC char *find_relevant_terminal(int successful,int uid,time_t ttime)
  24. {
  25.     struct pr_term  *term;
  26.     int             res = FALSE;
  27.     
  28.     setprtcent();
  29.     while ((term=getprtcent()) != (struct pr_term *) NULL) {
  30.         if ((res=check_term_for_match(term,successful,uid,ttime)) == TRUE)
  31.             break;
  32.     };
  33.     endprtcent();
  34.  
  35.     if (res == TRUE)
  36.         strcpy(devname,term->ufld.fd_devname);  /* Found it */
  37.     else
  38.         strcpy(devname,"unknown terminal");     /* No match */
  39.  
  40.     return(devname);
  41. }
  42.  
  43. PRIVATE int check_term_for_match(const struct pr_term *term,
  44.                                  int successful, int uid, time_t ttime)
  45. {
  46.     struct t_field  *data;
  47.     int             t_uid;
  48.     time_t          t_time;
  49.     time_t          time_diff;
  50.  
  51.     data  = (struct t_field *) &(term->ufld);
  52.  
  53.     /* Get the last uid and time data from the terminal database */
  54.     if (successful == TRUE) {
  55.         t_uid  = data->fd_uid;
  56.         t_time = data->fd_slogin;
  57.     } else {
  58.         t_uid  = data->fd_uuid;
  59.         t_time = data->fd_ulogin;
  60.     }
  61.  
  62.     /* Check if the uid matches ( or matches 0 after a timeout ) */
  63.     if (uid != t_uid && t_uid != 0)
  64.         return(FALSE);
  65.  
  66.     /* Check if the time matches ( or +60 seconds after a timeout ) */
  67.     time_diff = ttime - t_time;
  68.     if ((abs(time_diff) > 5) && (abs(time_diff+60) > 5))
  69.         return(FALSE);
  70.     
  71.     /* Not certain but this looks likely */
  72.     return(TRUE);
  73. }
  74.