home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / N / TCPIP / NETKIT-A.06 / NETKIT-A / NetKit-A-0.06 / pidentd-2.2 / src / kernel / unicos6.c < prev   
Encoding:
C/C++ Source or Header  |  1993-03-17  |  3.4 KB  |  197 lines

  1. /*
  2. ** kernel/unicos6.c    Low level kernel access functions for Cray UNICOS 6
  3. **
  4. ** This program is in the public domain and may be used freely by anyone
  5. ** who wants to. 
  6. **
  7. ** Last update: 17 March 1993
  8. **
  9. ** Please send bug fixes/bug reports to: Peter Eriksson <pen@lysator.liu.se>
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <errno.h>
  14. #include <ctype.h>
  15. #include <nlist.h>
  16. #include <pwd.h>
  17. #include <signal.h>
  18. #include <syslog.h>
  19.  
  20. #include "kvm.h"
  21.  
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/param.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/socket.h>
  27.  
  28. #define IP_SECURITY
  29.  
  30. #include <sys/socketvar.h>
  31.  
  32. #define KERNEL
  33.  
  34. #include <sys/file.h>
  35.  
  36. #include <fcntl.h>
  37.  
  38. #include <sys/param.h>
  39. #include <sys/sysmacros.h>
  40.  
  41. #include <sys/wait.h>
  42.   
  43. #undef KERNEL
  44.  
  45. #include <net/if.h>
  46. #include <net/route.h>
  47. #include <netinet/in.h>
  48.  
  49. #include <netinet/in_pcb.h>
  50.  
  51. #include <netinet/tcp.h>
  52. #include <netinet/ip_var.h>
  53. #include <netinet/tcp_timer.h>
  54. #include <netinet/tcp_var.h>
  55.  
  56. #include <arpa/inet.h>
  57.  
  58.  
  59. #include "identd.h"
  60. #include "error.h"
  61.  
  62.  
  63. extern void *calloc();
  64. extern void *malloc();
  65.  
  66.  
  67. struct nlist nl[] =
  68. {
  69. #define N_TCB 0
  70.  
  71.    { "tcb" },
  72.    { "" }
  73. };
  74.  
  75. static kvm_t *kd;
  76.  
  77. static struct file *xfile;
  78. static int nfile;
  79.  
  80. static struct inpcb tcb;
  81.  
  82.  
  83. int k_open()
  84. {
  85.   /*
  86.   ** Open the kernel memory device
  87.   */
  88.   if (!(kd = kvm_open(path_unix, path_kmem, NULL, O_RDONLY, NULL)))
  89.     ERROR("main: kvm_open");
  90.   
  91.   /*
  92.   ** Extract offsets to the needed variables in the kernel
  93.   */
  94.   if (kvm_nlist(kd, nl) != 0)
  95.     ERROR("main: kvm_nlist");
  96.  
  97.   return 0;
  98. }
  99.  
  100.  
  101. /*
  102. ** Get a piece of kernel memory with error handling.
  103. ** Returns 1 if call succeeded, else 0 (zero).
  104. */
  105. static int getbuf(addr, buf, len, what)
  106.   long addr;
  107.   char *buf;
  108.   int len;
  109.   char *what;
  110. {
  111.   if (kvm_read(kd, addr, buf, len) < 0)
  112.   {
  113.     if (syslog_flag)
  114.       syslog(LOG_ERR, "getbuf: kvm_read(%08x, %d) - %s : %m",
  115.          addr, len, what);
  116.  
  117.     return 0;
  118.   }
  119.   
  120.   return 1;
  121. }
  122.  
  123.  
  124.  
  125. /*
  126. ** Traverse the inpcb list until a match is found.
  127. ** Returns NULL if no match.
  128. */
  129. static struct socket *
  130.     getlist(pcbp, faddr, fport, laddr, lport)
  131.   struct inpcb *pcbp;
  132.   struct in_addr *faddr;
  133.   int fport;
  134.   struct in_addr *laddr;
  135.   int lport;
  136. {
  137.   struct inpcb *head;
  138.  
  139.   if (!pcbp)
  140.     return NULL;
  141.  
  142.   
  143.   head = pcbp->inp_prev;
  144.   do 
  145.   {
  146.     if ( pcbp->inp_faddr.s_addr == faddr->s_addr &&
  147.      pcbp->inp_laddr.s_addr == laddr->s_addr &&
  148.      pcbp->inp_fport        == fport &&
  149.      pcbp->inp_lport        == lport )
  150.       return pcbp->inp_socket;
  151.  
  152.   } while (wtob((long) pcbp->inp_next) != (long) head &&
  153.        getbuf(wtob((long) pcbp->inp_next), /* inp_next is a word ptr */
  154.           pcbp,
  155.           sizeof(struct inpcb),
  156.           "tcblist"));
  157.  
  158.   return NULL;
  159. }
  160.  
  161.  
  162.  
  163. /*
  164. ** Return the user number for the connection owner
  165. */
  166. int k_getuid(faddr, fport, laddr, lport, uid)
  167.   struct in_addr *faddr;
  168.   int fport;
  169.   struct in_addr *laddr;
  170.   int lport;
  171.   int *uid;
  172. {
  173.   long addr;
  174.   struct socket *sockp;
  175.   int i;
  176.   struct socket sockbuf;
  177.   
  178.   /* -------------------- TCP PCB LIST -------------------- */
  179.   if (!getbuf(nl[N_TCB].n_value, &tcb, sizeof(tcb), "tcb"))
  180.     return -1;
  181.   
  182.   tcb.inp_prev = (struct inpcb *) nl[N_TCB].n_value;
  183.   sockp = getlist(&tcb, faddr, fport, laddr, lport);
  184.   
  185.   if (!sockp)
  186.     return -1;
  187.  
  188.   /*
  189.     Creator of socket's uid is stored in the socket structure!!!
  190.     No need to walk the file table...
  191.   */
  192.   if(!getbuf(wtob(sockp), &sockbuf, sizeof(sockbuf), "socket"))
  193.        return -1;
  194.   *uid = sockbuf.so_uid;
  195.   return 0;
  196. }
  197.