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 / aux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-25  |  4.1 KB  |  229 lines

  1. /*
  2. ** kernel/aux.c        Low level kernel access functions for A/UX
  3. **
  4. ** This program is in the public domain and may be used freely by anyone
  5. ** who wants to. 
  6. **
  7. ** Last update: 20 April 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. #include <sys/config.h>
  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/user.h>
  39. #include <sys/wait.h>
  40.  
  41. #undef KERNEL
  42.  
  43. #include <net/if.h>
  44. #include <net/route.h>
  45. #include <netinet/in.h>
  46.  
  47. #include <netinet/in_pcb.h>
  48.  
  49. #include <netinet/tcp.h>
  50. #include <netinet/ip_var.h>
  51. #include <netinet/tcp_timer.h>
  52. #include <netinet/tcp_var.h>
  53.  
  54. #include <arpa/inet.h>
  55.  
  56. #include "identd.h"
  57. #include "error.h"
  58.  
  59.  
  60. extern void *calloc();
  61. extern void *malloc();
  62.  
  63.  
  64. struct nlist nl[3];
  65.  
  66. static kvm_t *kd;
  67.  
  68. static struct file *xfile;
  69.  
  70. static int nfile;
  71.  
  72. static struct inpcb tcb;
  73.  
  74.  
  75. int k_open()
  76. {
  77.   /*
  78.   ** Open the kernel memory device
  79.   */
  80.   if (!(kd = kvm_open(path_unix, path_kmem, NULL, O_RDONLY, NULL)))
  81.     ERROR("main: kvm_open");
  82.  
  83.   /* Kludge - have to do it here or A/UX 3 will barf */
  84. #define N_FILE 0  
  85. #define N_TCB 1
  86. #ifdef n_name
  87.   strcpy(nl[N_FILE].n_name,"file");
  88.   strcpy(nl[N_TCB].n_name,"tcb");
  89.   strcpy(nl[2].n_name,"");
  90. #else
  91.   nl[N_FILE].n_name = "file";
  92.   nl[N_TCB].n_name  = "tcb";
  93.   nl[2].n_name = "";
  94. #endif
  95.   
  96.   /*
  97.   ** Extract offsets to the needed variables in the kernel
  98.   */
  99.   if (kvm_nlist(kd, nl) != 0)
  100.     ERROR("main: kvm_nlist");
  101.  
  102.   return 0;
  103. }
  104.  
  105.  
  106. /*
  107. ** Get a piece of kernel memory with error handling.
  108. ** Returns 1 if call succeeded, else 0 (zero).
  109. */
  110. static int getbuf(addr, buf, len, what)
  111.   long addr;
  112.   char *buf;
  113.   int len;
  114.   char *what;
  115. {
  116.  
  117.   if (kvm_read(kd, addr, buf, len) < 0)
  118.   {
  119.     if (syslog_flag)
  120.       syslog(LOG_ERR, "getbuf: kvm_read(%08x, %d) - %s : %m",
  121.          addr, len, what);
  122.  
  123.     return 0;
  124.   }
  125.   
  126.   return 1;
  127. }
  128.  
  129.  
  130.  
  131. /*
  132. ** Traverse the inpcb list until a match is found.
  133. ** Returns NULL if no match.
  134. */
  135. static struct socket *
  136.     getlist(pcbp, faddr, fport, laddr, lport)
  137.   struct inpcb *pcbp;
  138.   struct in_addr *faddr;
  139.   int fport;
  140.   struct in_addr *laddr;
  141.   int lport;
  142. {
  143.   struct inpcb *head;
  144.  
  145.   if (!pcbp)
  146.     return NULL;
  147.  
  148.   
  149.   head = pcbp->inp_prev;
  150.   do 
  151.   {
  152.     if ( pcbp->inp_faddr.s_addr == faddr->s_addr &&
  153.      pcbp->inp_laddr.s_addr == laddr->s_addr &&
  154.      pcbp->inp_fport        == fport &&
  155.      pcbp->inp_lport        == lport )
  156.       return pcbp->inp_socket;
  157.   } while (pcbp->inp_next != head &&
  158.        getbuf((long) pcbp->inp_next,
  159.           pcbp,
  160.           sizeof(struct inpcb),
  161.           "tcblist"));
  162.  
  163.   return NULL;
  164. }
  165.  
  166.  
  167.  
  168. /*
  169. ** Return the user number for the connection owner
  170. */
  171. int k_getuid(faddr, fport, laddr, lport, uid)
  172.   struct in_addr *faddr;
  173.   int fport;
  174.   struct in_addr *laddr;
  175.   int lport;
  176.   int *uid;
  177. {
  178.   long addr;
  179.   struct socket *sockp;
  180.   int i;
  181.   struct ucred ucb;
  182.   
  183.   /* -------------------- FILE DESCRIPTOR TABLE -------------------- */
  184.   nfile = NFILE;
  185.  
  186.   if (!getbuf(nl[N_FILE].n_value, &addr, sizeof(addr), "&file"))
  187.     return -1;
  188.   
  189.   xfile = (struct file *) calloc(nfile, sizeof(struct file));
  190.   if (!xfile)
  191.     ERROR2("k_getuid: calloc(%d,%d)", nfile, sizeof(struct file));
  192.   
  193.   if (!getbuf(addr, xfile, sizeof(struct file)*nfile, "file[]"))
  194.     return -1;
  195.   
  196.   /* -------------------- TCP PCB LIST -------------------- */
  197.   if (!getbuf(nl[N_TCB].n_value, &tcb, sizeof(tcb), "tcb"))
  198.     return -1;
  199.   
  200.   tcb.inp_prev = (struct inpcb *) nl[N_TCB].n_value;
  201.   sockp = getlist(&tcb, faddr, fport, laddr, lport);
  202.   
  203.   if (!sockp)
  204.     return -1;
  205.  
  206.   /*
  207.   ** Locate the file descriptor that has the socket in question
  208.   ** open so that we can get the 'ucred' information
  209.   */
  210.   for (i = 0; i < nfile; i++)
  211.   {
  212.     if (xfile[i].f_count == 0)
  213.       continue;
  214.     
  215.     if (xfile[i].f_type == DTYPE_SOCKET &&
  216.     (struct socket *) xfile[i].f_data == sockp)
  217.     {
  218.       if (!getbuf(xfile[i].f_cred, &ucb, sizeof(ucb), "ucb"))
  219.     return -1;
  220.       
  221.       *uid = ucb.cr_ruid;
  222.       return 0;
  223.     }
  224.   }
  225.   
  226.   return -1;
  227. }
  228.  
  229.