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 / hpux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-16  |  4.7 KB  |  261 lines

  1. /*
  2. ** kernel/hpux.c    Low level kernel access functions for HP-UX 7 & 8
  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. #include <sys/socketvar.h>
  29. #include <sys/file.h>
  30.  
  31. #include <fcntl.h>
  32.  
  33. #include <sys/user.h>
  34. #include <sys/wait.h>
  35.   
  36. #include <net/if.h>
  37. #include <net/route.h>
  38. #include <netinet/in.h>
  39.  
  40. #include <netinet/in_pcb.h>
  41.  
  42. #include <netinet/tcp.h>
  43. #include <netinet/ip_var.h>
  44. #include <netinet/tcp_timer.h>
  45. #include <netinet/tcp_var.h>
  46.  
  47. #ifndef HPUX7
  48. #  include <arpa/inet.h>
  49. #endif
  50.  
  51. #include "identd.h"
  52. #include "error.h"
  53.  
  54.  
  55. extern void *calloc();
  56. extern void *malloc();
  57.  
  58.  
  59. struct nlist nl[] =
  60. {
  61. #if ((defined(hp9000s800) || defined(__hp9000s800)) && !defined(HPUX7))
  62.  
  63.   /* HP9000 Series 800, HP-UX 8.0 */
  64.  
  65. #define N_FILE  0  
  66. #define N_NFILE 1
  67. #define N_TCB   2
  68.  
  69.   { "file" },
  70.   { "nfile" },
  71.   { "tcb" },
  72.   
  73. #else
  74. #if ((defined(hp9000s800) || defined(__hp9000s800)) && defined(HPUX7))
  75.       
  76.   /* HP9000 Series 800, HP-UX 7.0 */
  77.       
  78. #define N_FILE  0  
  79. #define N_NFILE 1
  80. #define N_TCB   2
  81.       
  82.   { "file" },
  83.   { "nfile" },
  84.   { "tcp_cb" },
  85. #else
  86. #ifdef HPUX7        /* HP-UX 7.0 */
  87.  
  88. #define N_FILE  0  
  89. #define N_NFILE 1
  90. #define N_TCB   2
  91.  
  92.   { "_file" },
  93.   { "_nfile" },
  94.   { "_tcp_cb" },
  95.  
  96. #else         /* SUN or old BSD or other systems */      
  97.  
  98. #define N_FILE 0  
  99. #define N_NFILE 1
  100. #define N_TCB 2
  101.  
  102.   { "_file" },
  103.   { "_nfile" },
  104.   { "_tcb" },
  105.  
  106. #endif
  107. #endif
  108. #endif
  109.   { "" }
  110. };
  111.  
  112. static kvm_t *kd;
  113.  
  114. static struct file *xfile;
  115. static int nfile;
  116.  
  117. static struct inpcb tcb;
  118.  
  119.  
  120. int k_open()
  121. {
  122.   /*
  123.   ** Open the kernel memory device
  124.   */
  125.   if (!(kd = kvm_open(path_unix, path_kmem, NULL, O_RDONLY, NULL)))
  126.     ERROR("main: kvm_open");
  127.   
  128.   /*
  129.   ** Extract offsets to the needed variables in the kernel
  130.   */
  131.   if (kvm_nlist(kd, nl) != 0)
  132.     ERROR("main: kvm_nlist");
  133.  
  134.   return 0;
  135. }
  136.  
  137.  
  138. /*
  139. ** Get a piece of kernel memory with error handling.
  140. ** Returns 1 if call succeeded, else 0 (zero).
  141. */
  142. static int getbuf(addr, buf, len, what)
  143.   long addr;
  144.   char *buf;
  145.   int len;
  146.   char *what;
  147. {
  148.   if (kvm_read(kd, addr, buf, len) < 0)
  149.   {
  150.     if (syslog_flag)
  151.       syslog(LOG_ERR, "getbuf: kvm_read(%08x, %d) - %s : %m",
  152.          addr, len, what);
  153.  
  154.     return 0;
  155.   }
  156.   
  157.   return 1;
  158. }
  159.  
  160.  
  161.  
  162. /*
  163. ** Traverse the inpcb list until a match is found.
  164. ** Returns NULL if no match.
  165. */
  166. static struct socket *
  167.     getlist(pcbp, faddr, fport, laddr, lport)
  168.   struct inpcb *pcbp;
  169.   struct in_addr *faddr;
  170.   int fport;
  171.   struct in_addr *laddr;
  172.   int lport;
  173. {
  174.   struct inpcb *head;
  175.  
  176.   if (!pcbp)
  177.     return NULL;
  178.  
  179.   
  180.   head = pcbp->inp_prev;
  181.   do 
  182.   {
  183.     if ( pcbp->inp_faddr.s_addr == faddr->s_addr &&
  184.      pcbp->inp_laddr.s_addr == laddr->s_addr &&
  185.      pcbp->inp_fport        == fport &&
  186.      pcbp->inp_lport        == lport )
  187.       return pcbp->inp_socket;
  188.   } while (pcbp->inp_next != head &&
  189.        getbuf((long) pcbp->inp_next,
  190.           pcbp,
  191.           sizeof(struct inpcb),
  192.           "tcblist"));
  193.  
  194.   return NULL;
  195. }
  196.  
  197.  
  198.  
  199. /*
  200. ** Return the user number for the connection owner
  201. */
  202. int k_getuid(faddr, fport, laddr, lport, uid)
  203.   struct in_addr *faddr;
  204.   int fport;
  205.   struct in_addr *laddr;
  206.   int lport;
  207.   int *uid;
  208. {
  209.   long addr;
  210.   struct socket *sockp;
  211.   int i;
  212.   struct ucred ucb;
  213.   
  214.   /* -------------------- FILE DESCRIPTOR TABLE -------------------- */
  215.   if (!getbuf(nl[N_NFILE].n_value, &nfile, sizeof(nfile), "nfile"))
  216.     return -1;
  217.   
  218.   if (!getbuf(nl[N_FILE].n_value, &addr, sizeof(addr), "&file"))
  219.     return -1;
  220.  
  221.   xfile = (struct file *) calloc(nfile, sizeof(struct file));
  222.   if (!xfile)
  223.     ERROR2("k_getuid: calloc(%d,%d)", nfile, sizeof(struct file));
  224.   
  225.   if (!getbuf(addr, xfile, sizeof(struct file)*nfile, "file[]"))
  226.     return -1;
  227.   
  228.   /* -------------------- TCP PCB LIST -------------------- */
  229.   if (!getbuf(nl[N_TCB].n_value, &tcb, sizeof(tcb), "tcb"))
  230.     return -1;
  231.   
  232.   tcb.inp_prev = (struct inpcb *) nl[N_TCB].n_value;
  233.   sockp = getlist(&tcb, faddr, fport, laddr, lport);
  234.   
  235.   if (!sockp)
  236.     return -1;
  237.  
  238.   /*
  239.   ** Locate the file descriptor that has the socket in question
  240.   ** open so that we can get the 'ucred' information
  241.   */
  242.   for (i = 0; i < nfile; i++)
  243.   {
  244.     if (xfile[i].f_count == 0)
  245.       continue;
  246.     
  247.     if (xfile[i].f_type == DTYPE_SOCKET &&
  248.     (struct socket *) xfile[i].f_data == sockp)
  249.     {
  250.       if (!getbuf(xfile[i].f_cred, &ucb, sizeof(ucb), "ucb"))
  251.     return -1;
  252.       
  253.       *uid = ucb.cr_ruid;
  254.       return 0;
  255.     }
  256.   }
  257.   
  258.   return -1;
  259. }
  260.  
  261.