"Fossies" - the Fresh Open Source Software Archive

Member "pidentd-3.0.19/src/k_aix42.c" (25 Dec 1999, 5385 Bytes) of package /linux/misc/old/pidentd-3.0.19.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "k_aix42.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** k_aix42.c - IBM AIX 4.2 kernel access functions.
    3 **
    4 ** Copyright (c) 1997   Peter Eriksson <pen@lysator.liu.se>
    5 **       1994   Harlan Stenn <harlan@pfcs.com>
    6 **       1992   Charles M. Hannum
    7 **
    8 ** This program is free software; you can redistribute it and/or
    9 ** modify it as you wish - as long as you don't claim that you wrote
   10 ** it.
   11 **
   12 ** This program is distributed in the hope that it will be useful,
   13 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   14 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   15 */
   16 
   17 #include "config.h"
   18 
   19 #include <sys/types.h>
   20 
   21 #ifndef _AIX4
   22 #define _AIX4 1
   23 #endif
   24 #ifndef _AIX42
   25 #define _AIX42 1
   26 #endif
   27 
   28 #if defined(__GNUC__) && defined(_AIX42)
   29 
   30 typedef long long   aligned_off64_t __attribute__ ((aligned (8)));
   31 typedef long long   aligned_offset_t  __attribute__ ((aligned (8)));
   32 #define off64_t     aligned_off64_t
   33 #define offset_t    aligned_offset_t
   34 
   35 #endif
   36 
   37 #include <unistd.h>
   38 #include <fcntl.h>
   39 #include <stdlib.h>
   40 #include <errno.h>
   41 #include <pwd.h>
   42 #include <stdio.h>
   43 #include <syslog.h>
   44 
   45 #include <sys/domain.h>
   46 #include <sys/protosw.h>
   47 #include <sys/socket.h>
   48 #include <sys/socketvar.h>
   49 #include <sys/statfs.h>
   50 #include <sys/vfs.h>
   51 #include <net/route.h>
   52 #include <netinet/in.h>
   53  
   54 #if defined(_AIX4)
   55 #include <netinet/ip.h>
   56 #endif  /* defined(_AIX4) */
   57  
   58 #include <netinet/in_pcb.h>
   59 #include <arpa/inet.h>
   60 
   61 #define _KERNEL 1
   62 #include <sys/file.h>
   63 #undef  _KERNEL
   64 #include <procinfo.h>
   65  
   66 #if defined(_AIX4)
   67 #define u_maxofile  U_maxofile
   68 #define u_ufd   U_ufd
   69 #endif  /* defined(_AIX4) */
   70 
   71 #include "pidentd.h"
   72 
   73 struct kainfo
   74 {
   75     int kd;
   76 };
   77 
   78 
   79 int
   80 ka_init(void)
   81 {
   82     return 0;
   83 }
   84 
   85 
   86 int
   87 ka_open(void **misc)
   88 {
   89     struct kainfo *kp;
   90 
   91 
   92     kp = s_malloc(sizeof(*kp));
   93     
   94     if ((kp->kd = open("/dev/kmem", O_RDONLY)) == -1)
   95     {
   96     syslog(LOG_ERR, "open(\"/dev/kmem\"): %m");
   97     s_free(kp);
   98     return -1;
   99     }
  100 
  101     *misc = (void *) kp;
  102     return 0;
  103 }
  104 
  105 
  106 static int
  107 kread (int kmem,
  108        off_t addr,
  109        char *buf,
  110        int len)
  111 {
  112     int br;
  113     
  114     if (lseek (kmem, addr, SEEK_SET) == (off_t) -1)
  115     return (-1);
  116     
  117     br = read(kmem, buf, len);
  118     
  119     return ((br == len) ? 0 : 1);
  120 }
  121 
  122 
  123 
  124 int
  125 ka_lookup(void *vp, struct kernel *kp)
  126 {
  127     int max_procs = 64,
  128     num_procs = 0, fd;
  129     struct procinfo *procinfo = NULL, *pp;
  130     struct user *user;
  131     struct file *filep, file;
  132     struct socket *socketp, socket;
  133     struct protosw *protoswp, protosw;
  134     struct domain *domainp, domain;
  135     struct inpcb *inpcbp, inpcb;
  136     
  137     struct in_addr *faddr;
  138     int fport;
  139     struct in_addr *laddr;
  140     int lport;
  141     
  142     struct kainfo *kip;
  143     
  144     kip = (struct kainfo *) vp;
  145     
  146     faddr = &kp->remote.sin_addr;
  147     laddr = &kp->local.sin_addr;
  148     fport = kp->remote.sin_port;
  149     lport = kp->local.sin_port;
  150 
  151 
  152     while ((procinfo = (struct procinfo *)
  153         s_malloc ((size_t) (max_procs * sizeof (*procinfo)))) &&
  154        (num_procs = getproc (procinfo, max_procs,
  155                  sizeof (*procinfo))) == -1 &&
  156        errno == ENOSPC)
  157     {
  158     max_procs <<= 1;
  159     s_free (procinfo);
  160     }
  161     
  162 
  163     user = s_malloc((size_t) sizeof(*user));
  164     
  165     pp = procinfo;
  166     for (; num_procs != 0; num_procs--, pp++)
  167     {
  168     if (pp->pi_stat == 0 || pp->pi_stat == SZOMB)
  169         continue;
  170     
  171     if (getuser (pp, sizeof (*pp), user, sizeof (*user)))
  172         continue;
  173     
  174     for (fd = 0; fd < user->u_maxofile; fd++)
  175     {
  176         if ((filep = user->u_ufd[fd].fp) == NULL)
  177         continue;
  178         
  179         if (kread (kip->kd, (off_t) filep, (char *) &file, sizeof (file)))
  180         {
  181         syslog (LOG_ERR, "can not read file struct from %#x",
  182                    (unsigned) filep);
  183         goto Fail;
  184         }
  185 
  186         if (file.f_type != DTYPE_SOCKET)
  187         continue;
  188         
  189         if ((socketp = (struct socket *) file.f_data) == NULL)
  190         continue;
  191         
  192         if (kread (kip->kd,(off_t) socketp, (char *) &socket,
  193                sizeof (socket)))
  194         {
  195         syslog (LOG_ERR, "can not read socket struct from %#x",
  196             (unsigned) socketp);
  197         goto Fail;
  198         }
  199 
  200         if ((protoswp = socket.so_proto) == NULL)
  201         continue;
  202 
  203         if (kread (kip->kd, (off_t) protoswp, (char *) &protosw,
  204                sizeof (protosw)))
  205         {
  206         syslog (LOG_ERR, "can not read protosw struct from %#x",
  207                (unsigned) protoswp);
  208         goto Fail;
  209         }
  210 
  211         if (protosw.pr_protocol != IPPROTO_TCP)
  212         continue;
  213         
  214         if ((domainp = protosw.pr_domain) == NULL)
  215         continue;
  216 
  217         if (kread (kip->kd, (off_t) domainp, (char *) &domain,
  218                sizeof (domain)))
  219         {
  220         syslog (LOG_ERR, "can not read domain struct from %#x",
  221             (unsigned) domainp);
  222         goto Fail;
  223         }
  224 
  225         if (domain.dom_family != AF_INET
  226 #ifdef AF_INET6
  227         && domain.dom_family != AF_INET6
  228 #endif
  229         )
  230         continue;
  231         
  232         if ((inpcbp = (struct inpcb *) socket.so_pcb) == NULL)
  233         continue;
  234 
  235         if (kread (kip->kd, (off_t) inpcbp, (char *) &inpcb,
  236                sizeof (inpcb)))
  237         {
  238         syslog (LOG_ERR, "can not read inpcb struct from %#x",
  239             (unsigned) inpcbp);
  240         goto Fail;
  241         }
  242 
  243         if (socketp != inpcb.inp_socket)
  244         continue;
  245         
  246         if (inpcb.inp_faddr.s_addr != faddr->s_addr ||
  247         inpcb.inp_fport != fport ||
  248         inpcb.inp_laddr.s_addr != laddr->s_addr ||
  249         inpcb.inp_lport != lport)
  250         continue;
  251         
  252         kp->ruid = pp->pi_uid;
  253         kp->euid = pp->pi_suid;
  254         kp->pid  = pp->pi_pid;
  255 
  256         s_free(user);
  257         s_free(procinfo);
  258 
  259         return 1;
  260     }
  261     }
  262 
  263     s_free(user);
  264     s_free(procinfo);
  265     return 0;
  266 
  267   Fail:
  268     s_free(user);
  269     s_free(procinfo);
  270     return -1;
  271 }
  272