home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3299 / authd.c next >
Encoding:
C/C++ Source or Header  |  1991-05-07  |  4.3 KB  |  169 lines

  1. /* History:
  2. 5/3/91 DJB authd version 3.5 alpha.
  3. 5/3/91 DJB modified to not compile without HAVE_UCRED
  4. 5/1/91 DJB baseline public domain
  5. Derived from authd 3.01, DJB.
  6. */
  7.  
  8. #include "confhaveucred.h"
  9. #ifndef HAVE_UCRED
  10. error! error! error! XXX
  11. authd will not work on a system without struct ucred.
  12. #endif
  13.  
  14. #include <stdio.h>
  15. #ifdef USE_SYSLOG
  16. #include <syslog.h>
  17. #endif
  18. #include "structfile.h"
  19. #include "structucred.h"
  20. #include "structinpcb.h"
  21. #include "getfcred.h"
  22. #include "filetable.h"
  23. #include "netinp.h"
  24. #include "auread.h"
  25. #include "username.h"
  26.  
  27. int flagpwnam = 0;
  28. int flagauthd = 0;
  29. char localport[10];
  30. char remoteport[10];
  31.  
  32. zap(err,argv0)
  33. char *err;
  34. char *argv0;
  35. {
  36.  if (flagauthd)
  37.    /* Reporting errors honestly to a remote host could damage security. */
  38.    printf("%s, %s: ERROR: UNKNOWN-ERROR\r\n",localport,remoteport);
  39.  else
  40.    fprintf(stderr,"%s: fatal: %s\n",argv0,err);
  41.  exit(37); /*XXX*/
  42. }
  43.  
  44. #define ZAP(err) zap(err,argv[0])
  45.  
  46. int loc4[4];
  47. int rem4[4];
  48. #define l1 loc4[0]
  49. #define l2 loc4[1]
  50. #define l3 loc4[2]
  51. #define l4 loc4[3]
  52. #define r1 rem4[0]
  53. #define r2 rem4[1]
  54. #define r3 rem4[2]
  55. #define r4 rem4[3]
  56.  
  57. int doit(fp)
  58. register struct file *fp;
  59. {
  60.  register struct ucred *uc;
  61.  register int uid;
  62.  char *un;
  63.  
  64.  uc = getfcred(fp);
  65.  if (!uc)
  66.    return -1;
  67.  uid = (int) uc->cr_ruid;
  68.  if (flagpwnam)
  69.   {
  70.    if (uid2username(uid,&un) == 1)
  71.      /* XXX: We don't give out userids that don't have usernames. */
  72.      return -1;
  73.    if (flagauthd)
  74.    /* UNIX is a trademark of AT&T. :-) */
  75.    /* XXX: We could try to report UNIX variants here. */
  76.      printf("%s, %s: USERID: %s: %s\r\n",localport,remoteport,"UNIX",un);
  77.    else
  78.      printf("%s\n",un);
  79.   }
  80.  else
  81.    printf("%d\n",uid);
  82.  return 0;
  83. }
  84.  
  85. main(argc,argv)
  86. int argc;
  87. char *argv[];
  88. {
  89.  register struct file *xfile;
  90.  register struct file *fp;
  91.  register struct inpcb *inp;
  92.  int lp;
  93.  int rp;
  94.  
  95.  if ((!strcmp(argv[0],"authd"))
  96.   || ((strlen(argv[0]) >= 6)
  97.    && (!strcmp(argv[0] + strlen(argv[0]) - 6,"/authd"))))
  98.    flagauthd = flagpwnam = 1;
  99.  else
  100.    if ((!strcmp(argv[0],"tcpuname"))
  101.     || ((strlen(argv[0]) >= 9)
  102.      && (!strcmp(argv[0] + strlen(argv[0]) - 9,"/tcpuname"))))
  103.      flagpwnam = 1;
  104.  
  105.  if (flagauthd)
  106.   {
  107.    if (readlr(localport,remoteport,loc4,rem4,&lp,&rp) == -1)
  108.      exit(1); /*XXX*/
  109. #ifdef USE_SYSLOG
  110.  /* This isn't worth the time for the procedure call, but if you want... */
  111.    syslog(LOG_DEBUG,"authd: checking up on %d.%d.%d.%d %d %d.%d.%d.%d %d\n",
  112.      r1,r2,r3,r4,rp,l1,l2,l3,l4,lp);
  113. #endif
  114.   }
  115.  else
  116.   {
  117.    if (argc < 4)
  118.      ZAP("need four arguments");
  119.    if (sscanf(argv[1],"%d.%d.%d.%d",&r1,&r2,&r3,&r4) < 4)
  120.      ZAP("arg 1 must be a.b.c.d");
  121.    if (sscanf(argv[2],"%d",&rp) < 1)
  122.      ZAP("arg 2 must be integer");
  123.    if (sscanf(argv[3],"%d.%d.%d.%d",&l1,&l2,&l3,&l4) < 4)
  124.      ZAP("arg 3 must be a.b.c.d");
  125.    if (sscanf(argv[4],"%d",&lp) < 1)
  126.      ZAP("arg 4 must be integer");
  127.   }
  128.  
  129.  if (netinpinit(r1,r2,r3,r4,rp) == -1)
  130.    ZAP("cannot init netstat");
  131.  if (filetableinit() == -1)
  132.    ZAP("cannot init space for file table");
  133.  
  134.  xfile = getfiletable();
  135.  if (!xfile)
  136.    ZAP("cannot get file table");
  137.  
  138.  while (inp = nextnetinp())
  139.   {
  140. /* Cursed be Convex and the other manufacturers who make this code */
  141. /* nearly impossible to write with any pretense of portability. */
  142.    if((((char *) &inp->inp_faddr)[3] == (char) r4)
  143.     &&(((char *) &inp->inp_laddr)[3] == (char) l4)
  144.     &&(((char *) &inp->inp_faddr)[2] == (char) r3)
  145.     &&(((char *) &inp->inp_laddr)[2] == (char) l3)
  146.     &&(((char *) &inp->inp_faddr)[1] == (char) r2)
  147.     &&(((char *) &inp->inp_laddr)[1] == (char) l2)
  148.     &&(((char *) &inp->inp_faddr)[0] == (char) r1)
  149.     &&(((char *) &inp->inp_laddr)[0] == (char) l1)
  150.     &&(inp->inp_fport == htons((unsigned short) rp))
  151.     &&(inp->inp_lport == htons((unsigned short) lp))
  152.    )
  153.    /* Is it worth snarfing the socket and checking that it points back */
  154.    /* to inp? No, because then we have to worry about sys/socketvar.h, */
  155.    /* including sys/mbuf.h on a Convex and maybe other machines, etc. */
  156.    /* Sometimes portability gets in the way of everything else. */
  157.      for (fp = xfile;fp < xfile + mynfile;++fp)
  158.        if (fp->f_count && (fp->f_type == DTYPE_SOCKET))
  159.          if ((char *) fp->f_data == (char *) inp->inp_socket)
  160.       {
  161.        if (doit(fp) == -1)
  162.          ZAP("cannot get userid");
  163.        exit(0);
  164.       }
  165.   }
  166.  ZAP("no such TCP connection");
  167.  exit(1);
  168. }
  169.