home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 3 / 3298 / testauthd.c < prev   
C/C++ Source or Header  |  1991-05-06  |  3KB  |  118 lines

  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/file.h>
  4. #ifdef sun
  5. #include <sys/fcntl.h>
  6. #endif
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <arpa/inet.h>
  10. #include <pwd.h>
  11. #include <errno.h>
  12. extern int errno;
  13. #include "authuser.h"
  14.  
  15. main()
  16. {
  17.  int s;
  18.  int t;
  19.  int u;
  20.  struct sockaddr_in sa;
  21.  int salen;
  22.  int localport;
  23.  unsigned long in;
  24.  unsigned short local;
  25.  unsigned short remote;
  26.  char *user;
  27.  struct passwd *pw;
  28.  int ok;
  29.  
  30.  ok = 1;
  31.  
  32. #define ZOT(x) { fprintf(stderr,"test: fatal: %s\n",x); exit(37); }
  33.  
  34.  printf("connecting to myself through loopback (127.1)...\n");
  35.  
  36.  s = socket(AF_INET,SOCK_STREAM,0);
  37.  if (s == -1)
  38.    ZOT("cannot create server socket")
  39.  
  40.  localport = 0; /* meaning pick a port, we don't care which */
  41.  
  42.  sa.sin_family = AF_INET;
  43.  sa.sin_port = htons(localport);
  44.  sa.sin_addr.s_addr = INADDR_ANY;
  45.  
  46.  if (bind(s,&sa,sizeof(sa)) == -1)
  47.    ZOT("cannot bind server socket")
  48.  
  49.  salen = sizeof(sa);
  50.  if (getsockname(s,&sa,&salen) == -1)
  51.    ZOT("cannot get name of server socket")
  52.  
  53.  if (listen(s,5) == -1)
  54.    ZOT("cannot listen for connections") /* XXX: impossible */
  55.  
  56.  u = socket(AF_INET,SOCK_STREAM,0);
  57.  if (u == -1)
  58.    ZOT("cannot create client socket")
  59.  
  60.  /* we could bind now if we wanted to pick a local port, or to know */
  61.  /* our local port before connection */
  62.  
  63.  /* if connect() blocked then we'd never get a chance to accept() */
  64.  if (fcntl(u,F_SETFL,O_NDELAY) == -1) /* XXX: FNDELAY? */
  65.    ZOT("cannot set client socket to non-blocking mode")
  66.  
  67.  /* sa is already initialized above to the address we want to connect to */
  68.  if (connect(u,&sa,sizeof(sa)) != -1)
  69.    ; /* XXX: this is slightly screwy, though common, behavior */
  70.  else
  71.    if (errno != EINPROGRESS)
  72.      ZOT("connect failed") /* XXX: should retry if EINTR */
  73.  
  74.  salen = sizeof(sa);
  75.  if ((t = accept(s,&sa,&salen)) == -1)
  76.    ZOT("cannot accept connection")
  77.  
  78.  printf("system says host is %s\n",inet_ntoa(sa.sin_addr));
  79.  
  80.  /* now let's see what the server can figure out about the client */
  81.  
  82.  if (auth_fd(t,&in,&local,&remote) == -1)
  83.    ZOT("authuser cannot figure out connection information")
  84.  
  85.  if (sa.sin_addr.s_addr != in)
  86.   {
  87.    ok = 0;
  88.    sa.sin_addr.s_addr = in;
  89.   }
  90.  printf("authuser says host is %s\n",inet_ntoa(sa.sin_addr));
  91.  
  92.  pw = getpwuid(getuid());
  93.  if (pw)
  94.    printf("system says username is %s\n",pw->pw_name);
  95.  else
  96.   {
  97.    ok = 0;
  98.    printf("system cannot figure out your username\n");
  99.   }
  100.  
  101.  user = auth_tcpuser(in,local,remote);
  102.  if (user)
  103.    printf("authd says username is %s\n",user);
  104.  else
  105.   {
  106.    ok = 0;
  107.    printf("authd cannot figure out your username\n");
  108.   }
  109.  
  110.  if (ok)
  111.    if (!strcmp(user,pw->pw_name))
  112.      printf("Everything looks okay to me.\n");
  113.    else
  114.      ok = 1;
  115.  
  116.  exit(!ok);
  117. }
  118.