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

  1. /*
  2. ** kernel/sunos5.c                 SunOS 5 kernel access functions
  3. **
  4. ** This program is in the public domain and may be used freely by anyone
  5. ** who wants to. 
  6. **
  7. ** Author: Casper Dik <casper@fwi.uva.nl>
  8. **
  9. ** Last update: 21 Apr 1993
  10. **
  11. ** Please send bug fixes/bug reports to: Peter Eriksson <pen@lysator.liu.se>
  12. */
  13.  
  14.  
  15. #define _KMEMUSER
  16. #define _KERNEL
  17.  
  18. /* some definition conflicts. but we must define _KERNEL */
  19.  
  20. #define exit         kernel_exit
  21. #define strsignal    kernel_strsignal
  22.  
  23. #include <sys/types.h>
  24. #include <sys/socket.h>
  25. #include <sys/signal.h>
  26. #include <sys/param.h>
  27. #include <netinet/in.h>
  28.  
  29. #include <stdio.h>
  30. #include <kvm.h>
  31. #include <nlist.h>
  32. #include <math.h>
  33. #include <sys/fcntl.h>
  34. #include <sys/cred.h>
  35. #include <sys/file.h>
  36. #include <sys/stream.h>
  37. #include <inet/common.h>
  38. #include <inet/ip.h>
  39.  
  40. #undef exit
  41. #undef strsignal
  42.  
  43. #include <unistd.h>
  44. #include <string.h>
  45. #include <stddef.h>
  46.  
  47. #include "identd.h"
  48. #include "error.h"
  49.  
  50. #define N_FANOUT 0
  51. #define N_FILE     1
  52.  
  53. struct nlist nl[] = {
  54.     { "ipc_tcp_fanout" },
  55.     { "file" },
  56.     { 0 },
  57. };
  58.  
  59.  
  60. static kvm_t *kd;
  61.  
  62.  
  63. int k_open()
  64. {
  65.   /*
  66.   ** Open the kernel memory device
  67.   */
  68.   if (!(kd = kvm_open(path_unix, path_kmem, NULL, O_RDONLY, NULL)))
  69.     ERROR("main: kvm_open");
  70.   
  71.   /*
  72.   ** Extract offsets to the needed variables in the kernel
  73.   */
  74.   if (kvm_nlist(kd, nl) != 0)
  75.     ERROR("main: kvm_nlist");
  76.  
  77.   return 0;
  78. }
  79.  
  80.  
  81. /*
  82. ** Get a piece of kernel memory with error handling.
  83. ** Returns 1 if call succeeded, else 0 (zero).
  84. */
  85. static int getbuf(addr, buf, len, what)
  86.   long addr;
  87.   char *buf;
  88.   int len;
  89.   char *what;
  90. {
  91.  
  92.   if (kvm_read(kd, addr, buf, len) < 0)
  93.   {
  94.     if (syslog_flag)
  95.       syslog(LOG_ERR, "getbuf: kvm_read(%08x, %d) - %s : %m",
  96.          addr, len, what);
  97.  
  98.     return 0;
  99.   }
  100.   
  101.   return 1;
  102. }
  103.  
  104.  
  105. /*
  106. ** Return the user number for the connection owner
  107. */
  108. int k_getuid(faddr, fport, laddr, lport, uid)
  109.   struct in_addr *faddr;
  110.   int fport;
  111.   struct in_addr *laddr;
  112.   int lport;
  113.   int *uid;
  114. {
  115.     queue_t *qp, *pq, sqr;
  116.     ipc_t ic, *icp;
  117.     unsigned short uslp, usfp;
  118.     unsigned int offset;
  119.     unsigned long fp;
  120.     file_t tf;
  121.     unsigned long zero = 0;
  122.     
  123.     usfp = fport;
  124.     uslp = lport;
  125.  
  126.     offset = usfp ^ uslp;
  127.     offset ^= (unsigned) faddr->S_un.S_un_b.s_b4 ^ (offset >> 8);
  128.     offset %= 256;
  129.     
  130.     if (!getbuf(nl[N_FANOUT].n_value + sizeof(ipc_t *) * offset,
  131.         (char *) &icp,
  132.         sizeof(ipc_t *),
  133.         "ipc_tcp_fanout[offset]"))
  134.     return -1;
  135.     
  136.     if (icp == 0) {
  137.     syslog(LOG_INFO, "k_getuid: Hash miss");
  138.     return -1;
  139.     }
  140.     
  141.     while (icp) {
  142.     if (!getbuf((unsigned long) icp,
  143.             (char *) &ic,
  144.             sizeof(ic),
  145.             "hash entry"))
  146.         return -1;
  147.  
  148.     if (usfp == ic.ipc_tcp_addr[4] && /* remote port */
  149.         uslp == ic.ipc_tcp_addr[5] && /* local port */
  150. #if 0
  151.         memcmp(&laddr->s_addr, &ic.ipc_tcp_addr[0], 4) == 0 && /* local */
  152. #else
  153.          (memcmp(&laddr->s_addr, &ic.ipc_tcp_addr[0], 4) == 0 ||
  154.          /* In SunOS 5.3, the local part can be all zeros */
  155.           memcmp(&zero, &ic.ipc_tcp_addr[0], 4) == 0) /* local */ &&
  156. #endif
  157.         memcmp(&faddr->s_addr, &ic.ipc_tcp_addr[2], 4) == 0)
  158.         break;
  159.     icp = ic.ipc_hash_next;
  160.     }
  161.     if (!icp) {
  162.     syslog(LOG_INFO, "k_getuid: Port not found");
  163.     return -1;
  164.     }
  165.     
  166.     if (!getbuf((unsigned long) ic.ipc_rq+offsetof(queue_t, q_stream),
  167.         (char *) &sqr.q_stream,
  168.         sizeof(sqr.q_stream),
  169.         "queue.q_stream"))
  170.     return -1;
  171.  
  172.     /* at this point sqr.q_stream holds the pointer to the stream we're
  173.        interested in. Now we're going to find the file pointer
  174.        that refers to the vnode that refers to this stream stream */
  175.  
  176.     fp = nl[N_FILE].n_value;
  177.     for (;fp;fp = (unsigned long) tf.f_next) {
  178.     vnode_t vp;
  179.  
  180.     if (!getbuf(fp, (char *) &tf, sizeof(file_t),"file pointer"))
  181.         return -1;
  182.  
  183.     if (!tf.f_vnode)
  184.         continue;
  185.  
  186.     if (!getbuf((unsigned long) tf.f_vnode + offsetof(vnode_t,v_stream),
  187.             (char *) &vp.v_stream,
  188.             sizeof(vp.v_stream),"vnode.v_stream"))
  189.         return -1;
  190.  
  191.     if (vp.v_stream == sqr.q_stream) {
  192.         cred_t cr;
  193.         if (!getbuf((unsigned long) tf.f_cred + offsetof(cred_t, cr_ruid),
  194.             (char *) &cr.cr_ruid,
  195.             sizeof(cr.cr_ruid),
  196.             "cred.cr_ruid"))
  197.         return -1;
  198.         *uid = cr.cr_ruid;
  199.         return 0;
  200.     }
  201.     }
  202.     return -1;
  203. }
  204.