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 / sunos4.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-17  |  4.1 KB  |  232 lines

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