home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2684 / test.c < prev   
Encoding:
C/C++ Source or Header  |  1991-02-07  |  2.6 KB  |  116 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.  s = socket(AF_INET,SOCK_STREAM,0);
  35.  if (s == -1)
  36.    ZOT("cannot create server socket")
  37.  
  38.  localport = 0; /* meaning pick a port, we don't care which */
  39.  
  40.  sa.sin_family = AF_INET;
  41.  sa.sin_port = htons(localport);
  42.  sa.sin_addr.s_addr = INADDR_ANY;
  43.  
  44.  if (bind(s,&sa,sizeof(sa)) == -1)
  45.    ZOT("cannot bind server socket")
  46.  
  47.  salen = sizeof(sa);
  48.  if (getsockname(s,&sa,&salen) == -1)
  49.    ZOT("cannot get name of server socket")
  50.  
  51.  if (listen(s,5) == -1)
  52.    ZOT("cannot listen for connections") /* XXX: impossible */
  53.  
  54.  u = socket(AF_INET,SOCK_STREAM,0);
  55.  if (u == -1)
  56.    ZOT("cannot create client socket")
  57.  
  58.  /* we could bind now if we wanted to pick a local port, or to know */
  59.  /* our local port before connection */
  60.  
  61.  /* if connect() blocked then we'd never get a chance to accept() */
  62.  if (fcntl(u,F_SETFL,O_NDELAY) == -1) /* XXX: FNDELAY? */
  63.    ZOT("cannot set client socket to non-blocking mode")
  64.  
  65.  /* sa is already initialized above to the address we want to connect to */
  66.  if (connect(u,&sa,sizeof(sa)) != -1)
  67.    ; /* XXX: this is slightly screwy, though common, behavior */
  68.  else
  69.    if (errno != EINPROGRESS)
  70.      ZOT("connect failed") /* XXX: should retry if EINTR */
  71.  
  72.  salen = sizeof(sa);
  73.  if ((t = accept(s,&sa,&salen)) == -1)
  74.    ZOT("cannot accept connection")
  75.  
  76.  printf("system says host is %s\n",inet_ntoa(sa.sin_addr));
  77.  
  78.  /* now let's see what the server can figure out about the client */
  79.  
  80.  if (auth_fd(t,&in,&local,&remote) == -1)
  81.    ZOT("authuser cannot figure out connection information")
  82.  
  83.  if (sa.sin_addr.s_addr != in)
  84.   {
  85.    ok = 0;
  86.    sa.sin_addr.s_addr = in;
  87.   }
  88.  printf("authuser says host is %s\n",inet_ntoa(sa.sin_addr));
  89.  
  90.  pw = getpwuid(getuid());
  91.  if (pw)
  92.    printf("system says username is %s\n",pw->pw_name);
  93.  else
  94.   {
  95.    ok = 0;
  96.    printf("system cannot figure out your username\n");
  97.   }
  98.  
  99.  user = auth_tcpuser(in,local,remote);
  100.  if (user)
  101.    printf("authd says username is %s\n",user);
  102.  else
  103.   {
  104.    ok = 0;
  105.    printf("authd cannot figure out your username\n");
  106.   }
  107.  
  108.  if (ok)
  109.    if (!strcmp(user,pw->pw_name))
  110.      printf("Everything looks okay to me.\n");
  111.    else
  112.      ok = 1;
  113.  
  114.  exit(!ok);
  115. }
  116.