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 / other.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-10  |  5.5 KB  |  336 lines

  1. /*
  2. ** kernel/other.c    Low level kernel access functions for a number of
  3. **            similar Unixes (all more or less based of 4.2BSD)
  4. **
  5. ** This program is in the public domain and may be used freely by anyone
  6. ** who wants to. 
  7. **
  8. ** Last update: 17 March 1993
  9. **
  10. ** Please send bug fixes/bug reports to: Peter Eriksson <pen@lysator.liu.se>
  11. */
  12.  
  13. #include <stdio.h>
  14. #include <errno.h>
  15. #include <ctype.h>
  16. #include <nlist.h>
  17. #include <pwd.h>
  18. #include <signal.h>
  19. #include <syslog.h>
  20.  
  21. #ifdef HAVE_KVM
  22. #  include <kvm.h>
  23. #else
  24. #  include "kvm.h"
  25. #endif
  26.  
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <sys/param.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/socket.h>
  32.  
  33. #ifdef __convex__
  34. #  define _KERN_UTIL
  35. #endif
  36.  
  37. #include <sys/socketvar.h>
  38.  
  39. #ifdef __convex__
  40. #  undef _KERN_UTIL
  41. #endif
  42.  
  43. #define KERNEL
  44.  
  45. #if defined(SUNOS35) || defined(__alpha)
  46. #  define nfile SOME_OTHER_VARIABLE_NAME
  47. #  define _KERNEL
  48. #endif
  49.  
  50. #include <sys/file.h>
  51.  
  52. #if defined(SUNOS35) || defined(__alpha)
  53. #  undef nfile
  54. #  undef _KERNEL
  55. #endif
  56.  
  57. #include <fcntl.h>
  58.  
  59. #if defined(ultrix)
  60. #  include <sys/dir.h>
  61. #  undef KERNEL
  62. #endif
  63.  
  64. #if defined(SEQUENT)
  65. #  undef KERNEL
  66. #endif
  67.  
  68. #include <sys/user.h>
  69. #include <sys/wait.h>
  70.  
  71. #ifdef KERNEL  
  72. #  undef KERNEL
  73. #endif
  74.  
  75. #ifdef __convex__
  76. #  include <sys/mbuf.h>
  77. #endif
  78.  
  79. #include <net/if.h>
  80. #include <net/route.h>
  81. #include <netinet/in.h>
  82.  
  83. #ifdef __alpha
  84. #include <netinet/in_systm.h>
  85. #include <netinet/ip.h>
  86. #endif
  87.  
  88. #include <netinet/in_pcb.h>
  89.  
  90. #include <netinet/tcp.h>
  91. #include <netinet/ip_var.h>
  92.  
  93. #ifdef __alpha
  94. #include <netinet/tcpip.h>
  95. #endif
  96.  
  97. #include <netinet/tcp_timer.h>
  98. #include <netinet/tcp_var.h>
  99.  
  100. #include <arpa/inet.h>
  101.  
  102. #ifdef MIPS
  103. #  include <sysv/sys/var.h>
  104. extern int errno;
  105. #endif
  106.  
  107. #include "identd.h"
  108. #include "error.h"
  109.  
  110.  
  111. extern void *calloc();
  112. extern void *malloc();
  113.  
  114.  
  115. struct nlist nl[] =
  116. {
  117. #if (defined(ultrix) && defined(mips))
  118.    /* Ultrix on DEC's MIPS machines */
  119.  
  120. #define N_FILE  0  
  121. #define N_NFILE 1
  122. #define N_TCB   2
  123.  
  124.   { "file" },
  125.   { "nfile" },
  126.   { "tcb" },
  127.   
  128. #else
  129. #ifdef MIPS        /* MIPS RISC/OS */
  130.  
  131. #define N_FILE 0  
  132. #define N_V    1
  133. #define N_TCB  2
  134.  
  135.   { "_file" },
  136.   { "_v" },
  137.   { "_tcb" },
  138.  
  139. #else
  140.  
  141. #define N_FILE 0  
  142. #define N_NFILE 1
  143. #define N_TCB 2
  144.  
  145.   { "_file" },
  146.   { "_nfile" },
  147.   { "_tcb" },
  148.  
  149. #endif
  150. #endif
  151.   { "" }
  152. };
  153.  
  154. static kvm_t *kd;
  155.  
  156. static struct file *xfile;
  157.  
  158. #ifndef ultrix
  159. static int nfile;
  160. #endif
  161.  
  162. #if defined(MIPS)
  163. static struct var v;
  164. #endif
  165.  
  166. static struct inpcb tcb;
  167.  
  168.  
  169. int k_open()
  170. {
  171.   /*
  172.   ** Open the kernel memory device
  173.   */
  174.   if (!(kd = kvm_open(path_unix, path_kmem, NULL, O_RDONLY, NULL)))
  175.     ERROR("main: kvm_open");
  176.   
  177.   /*
  178.   ** Extract offsets to the needed variables in the kernel
  179.   */
  180.   if (kvm_nlist(kd, nl) != 0)
  181.     ERROR("main: kvm_nlist");
  182.  
  183.   return 0;
  184. }
  185.  
  186.  
  187. /*
  188. ** Get a piece of kernel memory with error handling.
  189. ** Returns 1 if call succeeded, else 0 (zero).
  190. */
  191. static int getbuf(addr, buf, len, what)
  192. #ifdef __alpha
  193.   caddr_t addr;
  194. #else
  195.   long addr;
  196. #endif
  197.   char *buf;
  198.   int len;
  199.   char *what;
  200. {
  201.  
  202.   if (kvm_read(kd, addr, buf, len) < 0)
  203.   {
  204.     if (syslog_flag)
  205.       syslog(LOG_ERR, "getbuf: kvm_read(%08x, %d) - %s : %m",
  206.          addr, len, what);
  207.  
  208.     return 0;
  209.   }
  210.   
  211.   return 1;
  212. }
  213.  
  214.  
  215.  
  216. /*
  217. ** Traverse the inpcb list until a match is found.
  218. ** Returns NULL if no match.
  219. */
  220. static struct socket *
  221.     getlist(pcbp, faddr, fport, laddr, lport)
  222.   struct inpcb *pcbp;
  223.   struct in_addr *faddr;
  224.   int fport;
  225.   struct in_addr *laddr;
  226.   int lport;
  227. {
  228.   struct inpcb *head;
  229.  
  230.   if (!pcbp)
  231.     return NULL;
  232.  
  233.   
  234.   head = pcbp->inp_prev;
  235.   do 
  236.   {
  237.     if ( pcbp->inp_faddr.s_addr == faddr->s_addr &&
  238.      pcbp->inp_laddr.s_addr == laddr->s_addr &&
  239.      pcbp->inp_fport        == fport &&
  240.      pcbp->inp_lport        == lport )
  241.       return pcbp->inp_socket;
  242.   } while (pcbp->inp_next != head &&
  243. #ifdef __alpha
  244.        getbuf((caddr_t) pcbp->inp_next,
  245. #else
  246.        getbuf((long) pcbp->inp_next,
  247. #endif
  248.           pcbp,
  249.           sizeof(struct inpcb),
  250.           "tcblist"));
  251.  
  252.   return NULL;
  253. }
  254.  
  255.  
  256.  
  257. /*
  258. ** Return the user number for the connection owner
  259. */
  260. int k_getuid(faddr, fport, laddr, lport, uid)
  261.   struct in_addr *faddr;
  262.   int fport;
  263.   struct in_addr *laddr;
  264.   int lport;
  265.   int *uid;
  266. {
  267. #ifdef __alpha
  268.   caddr_t addr;
  269. #else
  270.   long addr;
  271. #endif
  272.   struct socket *sockp;
  273.   int i;
  274.   struct ucred ucb;
  275.   
  276.   /* -------------------- FILE DESCRIPTOR TABLE -------------------- */
  277. #if defined(MIPS)
  278.   if (!getbuf(nl[N_V].n_value, &v, sizeof(v), "v"))
  279.     return -1;
  280.   
  281.   nfile = v.v_file;
  282.   addr = nl[N_FILE].n_value;
  283. #else /* not MIPS */
  284.   if (!getbuf(nl[N_NFILE].n_value, &nfile, sizeof(nfile), "nfile"))
  285.     return -1;
  286.  
  287.   if (!getbuf(nl[N_FILE].n_value, &addr, sizeof(addr), "&file"))
  288.     return -1;
  289. #endif
  290.   
  291.   xfile = (struct file *) calloc(nfile, sizeof(struct file));
  292.   if (!xfile)
  293.     ERROR2("k_getuid: calloc(%d,%d)", nfile, sizeof(struct file));
  294.   
  295.   if (!getbuf(addr, xfile, sizeof(struct file)*nfile, "file[]"))
  296.     return -1;
  297.   
  298.   /* -------------------- TCP PCB LIST -------------------- */
  299.   if (!getbuf(nl[N_TCB].n_value, &tcb, sizeof(tcb), "tcb"))
  300.     return -1;
  301.   
  302.   tcb.inp_prev = (struct inpcb *) nl[N_TCB].n_value;
  303.   sockp = getlist(&tcb, faddr, fport, laddr, lport);
  304.   
  305.   if (!sockp)
  306.     return -1;
  307.  
  308.   /*
  309.   ** Locate the file descriptor that has the socket in question
  310.   ** open so that we can get the 'ucred' information
  311.   */
  312.   for (i = 0; i < nfile; i++)
  313.   {
  314.     if (xfile[i].f_count == 0)
  315.       continue;
  316.     
  317.     if (xfile[i].f_type == DTYPE_SOCKET &&
  318.     (struct socket *) xfile[i].f_data == sockp)
  319.     {
  320. #ifdef __convex__
  321.       *uid = xfile[i].f_cred.cr_uid;
  322. #else
  323.       if (!getbuf(xfile[i].f_cred, &ucb, sizeof(ucb), "ucb"))
  324.     return -1;
  325.       
  326.       *uid = ucb.cr_ruid;
  327. #endif
  328.  
  329.       return 0;
  330.     }
  331.   }
  332.   
  333.   return -1;
  334. }
  335.  
  336.