home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume22 / auth / part01 / authd.c < prev    next >
C/C++ Source or Header  |  1990-04-29  |  6KB  |  191 lines

  1. /*
  2. authd.c: Authentication Server daemon
  3. */
  4.  
  5. static char authdauthor[] =
  6. "authd was written by Daniel J. Bernstein.\n\
  7. Internet address: brnstnd@acf10.nyu.edu.\n";
  8.  
  9. static char authdversion[] = 
  10. "authd version 2.1, April 18, 1990.\n\
  11. Copyright (c) 1990, Daniel J. Bernstein.\n\
  12. All rights reserved.\n";
  13.  
  14. static char authdcopyright[] =
  15. "authd version 2.1, April 18, 1990.\n\
  16. Copyright (c) 1990, Daniel J. Bernstein.\n\
  17. All rights reserved.\n\
  18. \n\
  19. Until January 1, 1995, you are granted the following rights: A. To make\n\
  20. copies of this work in original form, so long as (1) the copies are exact\n\
  21. and complete; (2) the copies include the copyright notice, this paragraph,\n\
  22. and the disclaimer of warranty in their entirety. B. To distribute this\n\
  23. work, or copies made under the provisions above, so long as (1) this is\n\
  24. the original work and not a derivative form; (2) you do not charge a fee\n\
  25. for copying or for distribution; (3) you ensure that the distributed form\n\
  26. includes the copyright notice, this paragraph, and the disclaimer of\n\
  27. warranty in their entirety. These rights are temporary and revocable upon\n\
  28. written, oral, or other notice by Daniel J. Bernstein. These rights are\n\
  29. automatically revoked on January 1, 1995. This copyright notice shall be\n\
  30. governed by the laws of the state of New York.\n\
  31. \n\
  32. If you have questions about authd or about this copyright notice,\n\
  33. or if you would like additional rights beyond those granted above,\n\
  34. please feel free to contact the author at brnstnd@acf10.nyu.edu\n\
  35. on the Internet.\n";
  36.  
  37. static char authdwarranty[] =
  38. "To the extent permitted by applicable law, Daniel J. Bernstein disclaims\n\
  39. all warranties, explicit or implied, including but not limited to the\n\
  40. implied warranties of merchantability and fitness for a particular purpose.\n\
  41. Daniel J. Bernstein is not and shall not be liable for any damages,\n\
  42. incidental or consequential, arising from the use of this program, even\n\
  43. if you inform him of the possibility of such damages. This disclaimer\n\
  44. shall be governed by the laws of the state of New York.\n\
  45. \n\
  46. In other words, use this program at your own risk.\n\
  47. \n\
  48. If you have questions about authd or about this disclaimer of warranty,\n\
  49. please feel free to contact the author at brnstnd@acf10.nyu.edu\n\
  50. on the Internet.\n";
  51.  
  52. static char authdusage[] =
  53. "Usage: authd [ -ACHUVW ] \n\
  54. Help:  authd -H\n";
  55.  
  56. static char authdhelp[] =
  57. "authd provides TCP authentication information to other Internet hosts. \n\
  58. \n\
  59. authd -A: print authorship notice\n\
  60. authd -C: print copyright notice\n\
  61. authd -H: print this notice\n\
  62. authd -U: print short usage summary\n\
  63. authd -V: print version number\n\
  64. authd -W: print disclaimer of warranty\n\
  65. \n\
  66. authd: provide authentication information as per RFC 931\n\
  67. \n\
  68. authd should be run under a TCP connection server, such as\n\
  69. inetd(8) or attachport(1).\n\
  70. \n\
  71. If you have questions about or suggestions for authd, please feel free\n\
  72. to contact the author, Daniel J. Bernstein, at brnstnd@acf10.nyu.edu\n\
  73. on the Internet.\n";
  74.  
  75. #include <stdio.h>
  76. extern int errno;
  77. #include <sys/types.h>
  78. #include <netinet/in.h>
  79. #include <sys/file.h>
  80. #ifdef BSD
  81. #include <limits.h>
  82. #endif
  83. extern int getopt();
  84. extern char *optarg; /* these should be in getopt.h! */
  85. extern int optind;
  86. #include <ctype.h>
  87. #include "djberr.h"
  88.  
  89. #ifndef AUTHDIR
  90. #define AUTHDIR "/usr/etc/auth"
  91. #endif
  92.  
  93. #define ERR(num) { printf("%s, %s: ERROR: UNKNOWN-ERROR\r\n",\
  94.                           localport,remoteport); exit(num); }
  95.  
  96. main(argc,argv,envp)
  97. int argc;
  98. char *argv[];
  99. char *envp[];
  100. {
  101.  int ch;
  102.  char localport[10];
  103.  int localportlen = 0;
  104.  char remoteport[10];
  105.  int remoteportlen = 0;
  106.  int loop = 0;
  107.  struct sockaddr_in sa;
  108.  int salen;
  109.  int authfd;
  110.  char authfn[sizeof(AUTHDIR) + 30];
  111.  int lockfd;
  112.  char lockfn[sizeof(AUTHDIR) + 30]; /* for new locking protocol */
  113.  char lockbuf[32]; /* 5 pid, 1 :, 10 I, 1 ., 5 R, 1 \n, 8 U, 1 \0 */
  114.  unsigned long lockin;
  115.  unsigned short lockport;
  116.  int r;
  117.  char buf[9];
  118.  int opt;
  119.  
  120.  while ((opt = getopt(argc,argv,"ACHUVW")) != EOF)
  121.    switch(opt)
  122.     {
  123.      case 'A': (void) err(authdauthor); exit(1);
  124.      case 'C': (void) err(authdcopyright); exit(1);
  125.      case 'H': (void) err(authdhelp); exit(1);
  126.      case 'U': (void) err(authdusage); exit(1);
  127.      case 'V': (void) err(authdversion); exit(1);
  128.      case 'W': (void) err(authdwarranty); exit(1);
  129.      case '?': (void) err(authdusage); exit(1);
  130.     }
  131.  argv += optind, argc -= optind;
  132.  if (*argv)
  133.   {
  134.    (void) err(authdusage); exit(1);
  135.   }
  136.  
  137.  while ((ch = getchar()) != ',')
  138.   {
  139.    if (isascii(ch) && isdigit(ch))
  140.      localport[localportlen++] = ch;
  141.    if (localportlen == 6) /* tough luck! */
  142.      exit(2);
  143.    if ((++loop) > 1000) /* tough luck! */
  144.      exit(3);
  145.   }
  146.  
  147.  while ((ch = getchar()) != '\n')
  148.   {
  149.    if (isascii(ch) && isdigit(ch))
  150.      remoteport[remoteportlen++] = ch;
  151.    if (remoteportlen == 6) /* tough luck! */
  152.      exit(4);
  153.    if ((++loop) > 1000) /* tough luck! */
  154.      exit(5);
  155.   }
  156.  
  157.  /* Now we'll be nice enough to respond. */
  158.  
  159.  salen = sizeof(sa);
  160.  if (getpeername(0,&sa,&salen) == -1) ERR(6)
  161.  
  162.  /* Now we have enough information to look up answer. */
  163.  
  164.  (void) sprintf(lockfn,"%s/tcp/lock.%s",AUTHDIR,localport);
  165.  if ((lockfd = open(lockfn,O_RDONLY)) == -1) ERR(7)
  166.  (void) flock(lockfd,LOCK_EX); /* can't fail */
  167.  if ((r = read(lockfd,lockbuf,31)) <= 0) ERR(8)
  168.  lockbuf[r] = '\0';
  169.  if (lockbuf[0] == '!') ERR(12) /* ding ding ding! security alert! */
  170.  
  171.  (void) sprintf(authfn,"%s/tcp/%D.%s.%s",AUTHDIR,sa.sin_addr.s_addr,
  172.                 localport,remoteport);
  173.  if ((authfd = open(authfn,O_RDONLY)) == -1)
  174.   {
  175.    /* maybe it's authtcp and username file isn't there yet */
  176.    if (sscanf(lockbuf,"%*d:%D.%hd %8s",&lockin,&lockport,buf) < 3) ERR(9)
  177.    if ((lockin != sa.sin_addr.s_addr) || (lockport != atoi(remoteport))) ERR(10)
  178.    /* bingo! */
  179.   }
  180.  else
  181.   {
  182.    if ((r = read(authfd,buf,8)) <= 0) ERR(11)
  183.    buf[r] = '\0';
  184.   }
  185.  
  186.  printf("%s, %s: USERID: UNIX: %s\r\n",localport,remoteport,buf);
  187.  
  188.  (void) flock(lockfd,LOCK_UN); /* unnecessary */
  189.  exit(0);
  190. }
  191.