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 / next_mach.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-11  |  4.4 KB  |  220 lines

  1. /*
  2. ** kernel/next_mach.c        Low level kernel access functions for 4.3BSD
  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 <sys/types.h>
  16. #include <sys/param.h>
  17. #include <netdb.h>
  18. #include <nlist.h>
  19. #include <pwd.h>
  20. #include <signal.h>
  21.  
  22. #include "kvm.h"
  23. #include "paths.h"
  24.  
  25. #include <sys/socket.h>
  26. #include <sys/socketvar.h>
  27.  
  28. #include <sys/ioctl.h>
  29.  
  30. #ifndef NeXT31
  31. #  define KERNEL
  32. #  define KERNEL_FEATURES 
  33. #else
  34. #  define KERNEL_FILE
  35. #endif
  36.  
  37. #include <sys/file.h>
  38.  
  39. #ifndef NeXT31
  40. #  undef KERNEL
  41. #  undef KERNEL_FEATURES 
  42. #else
  43. #  undef KERNEL_FILE
  44. #endif
  45.  
  46. #include <sys/user.h>
  47.  
  48. #include <net/if.h>
  49. #include <net/route.h>
  50. /* 
  51. #include <netinet/in_systm.h>
  52. #include <netinet/ip.h>
  53. */
  54. #include <netinet/tcp.h>
  55. #include <netinet/tcp_timer.h>
  56. #include <netinet/tcp_var.h>
  57. #include <netinet/in.h>
  58. #include <netinet/in_pcb.h>
  59. #include <netinet/ip_var.h>
  60.  
  61. #include "identd.h"
  62. #include "error.h"
  63.  
  64.  
  65. extern void *calloc();
  66. extern void *malloc();
  67.  
  68.  
  69. struct nlist nl[] =
  70. {
  71. #define N_FILE 0  
  72. #define N_NFILE 1
  73. #define N_TCB 2
  74.  
  75.    { {"_file_list"} },
  76.    { {"_max_file"} },
  77.    { {"_tcb"} },
  78.    { {""} }
  79. };
  80.  
  81. static kvm_t *kd;
  82.  
  83.  
  84. int k_open()
  85. {
  86.   /*
  87.   ** Open the kernel memory device
  88.   */
  89.   if (!(kd = kvm_open(path_unix, path_kmem, NULL, O_RDONLY, NULL)))
  90.     ERROR("main: kvm_open");
  91.   
  92.   /*
  93.   ** Extract offsets to the needed variables in the kernel
  94.   */
  95.   if (kvm_nlist(kd, nl) != 0)
  96.     ERROR("main: kvm_nlist");
  97.  
  98.   return 0;
  99. }
  100.  
  101.  
  102. /*
  103. ** Get a piece of kernel memory with error handling.
  104. ** Returns 1 if call succeeded, else 0 (zero).
  105. */
  106. static int getbuf(addr, buf, len, what)
  107.   long addr;
  108.   char *buf;
  109.   int len;
  110.   char *what;
  111. {
  112.   if (kvm_read(kd, addr, buf, len) < 0)
  113.   {
  114.     if (syslog_flag)
  115.       syslog(LOG_ERR, "getbuf: kvm_read(%08x, %d) - %s : %m",
  116.          addr, len, what);
  117.  
  118.     return 0;
  119.   }
  120.   
  121.   return 1;
  122. }
  123.  
  124.  
  125.  
  126. /**
  127. ***
  128. *** Traverse the inpcb list until a match is found 
  129. *** Returns NULL if no match.
  130. *** 
  131. **/
  132.  
  133. static
  134. struct socket *getlist(struct inpcb *pcbp,
  135.                struct in_addr *faddr,
  136.                int fport,
  137.                struct in_addr *laddr,
  138.                int lport)
  139. {
  140.    struct inpcb *head;
  141.   
  142.    if (!pcbp)
  143.       return NULL;        /* Someone gave us a duff one here */
  144.   
  145.    head = pcbp->inp_prev;
  146.    do {
  147.       if (pcbp->inp_faddr.s_addr == faddr->s_addr &&
  148.       pcbp->inp_laddr.s_addr == laddr->s_addr &&
  149.       pcbp->inp_fport        == fport &&
  150.       pcbp->inp_lport        == lport )
  151.      return pcbp->inp_socket;
  152.  
  153.    } while (pcbp->inp_next != head &&
  154.         getbuf((long) pcbp->inp_next, 
  155.                    pcbp, 
  156.                    sizeof(struct inpcb), 
  157.            "tcblist"));
  158.  
  159.    return NULL;            /* Not found */
  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.    static struct ucred ucb;
  174.    struct socket *sockp;
  175.    int nfile;
  176.    struct inpcb tcb;        /*  */
  177.    struct file file_entry;
  178.    void * addr;
  179.  
  180.    /* -------------------- TCP PCB LIST -------------------- */
  181.    if (!getbuf(nl[N_TCB].n_value, &tcb, sizeof(tcb), "tcb"))
  182.       return -1;
  183.       
  184.    tcb.inp_prev = (struct inpcb *) nl[N_TCB].n_value;
  185.    sockp = getlist(&tcb, faddr, fport, laddr, lport);
  186.  
  187.    if (!sockp)
  188.       return -1;
  189.  
  190.    /* -------------------- FILE DESCRIPTOR TABLE -------------------- */
  191.    /* So now we hit the fun Mach kernel structures */
  192.    if (!getbuf(nl[N_FILE].n_value, &addr, sizeof(addr), "&file_table"))
  193.       return -1;
  194.       
  195.    /* We only use nfile as a failsafe in case something goes wrong! */
  196.    if (!getbuf(nl[N_NFILE].n_value, &nfile, sizeof(nfile), "nfile"))
  197.       return -1;
  198.       
  199.    file_entry.links.next = addr;
  200.    /* ------------------- SCAN FILE TABLE ------------------------ */
  201.    do {
  202.       if (!getbuf((unsigned long) file_entry.links.next, &file_entry, 
  203.               sizeof(file_entry), "struct file"))
  204.          return -1;
  205.      
  206.       if (file_entry.f_count &&
  207.       file_entry.f_type == DTYPE_SOCKET) {
  208.      if ((void *) file_entry.f_data == (void *) sockp) {
  209.         if (!getbuf((unsigned long) file_entry.f_cred, 
  210.                     &ucb, sizeof(ucb), "ucb"))
  211.            return -1;
  212.            
  213.         *uid = ucb.cr_ruid;
  214.         return 0;
  215.      }
  216.       }
  217.    } while ((file_entry.links.next != addr) && (--nfile));
  218.    return -1;
  219. }
  220.