"Fossies" - the Fresh Open Source Software Archive

Member "pidentd-3.0.19/src/k_irix5.c" (20 Jan 1999, 4165 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_irix5.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** k_irix.c - SGI IRIX -
    3 **
    4 ** Copyright (c) 1997      Peter Eriksson <pen@lysator.liu.se>
    5 **
    6 ** This program is free software; you can redistribute it and/or
    7 ** modify it as you wish - as long as you don't claim that you wrote
    8 ** it.
    9 **
   10 ** This program is distributed in the hope that it will be useful,
   11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   13 */
   14 /*
   15 ** =========================================================================
   16 ** Luc Chouinard, lucc@sgi.com
   17 ** =========================================================================
   18 */
   19 
   20 #include "config.h"
   21 
   22 #ifdef _POSIX_C_SOURCE /* Ugly hack below - pen */
   23 
   24 #define __EXTENSIONS__ 1
   25 #define _SGI_SOURCE 1
   26 
   27 #include <standards.h>
   28 
   29 #undef _XOPEN4UX
   30 #define _XOPEN4UX 1
   31 #undef _SGIAPI
   32 #define _SGIAPI 1
   33 
   34 #endif /* End ugly hack */
   35 
   36 
   37 #include <time.h>
   38 #include <netdb.h>
   39 #include <paths.h>
   40 #include <fcntl.h>
   41 #include <bstring.h>
   42 #include <mntent.h>
   43 #include <sys/select.h>
   44 #include <stdlib.h>
   45 #include <unistd.h>
   46 
   47 #include <sys/types.h>
   48 #include <sys/utssys.h>
   49 #include <sys/var.h>
   50 #include <netinet/in.h>
   51 #include <sys/cred.h>
   52 #include <sys/vfs.h>
   53 #include <sys/sysmp.h>
   54 #include <sys/param.h>
   55 #include <sys/socket.h>
   56 #include <sys/procfs.h>
   57 
   58 #include "pidentd.h"
   59 
   60 
   61 typedef struct {
   62     f_user_t *pl, *pr;
   63 } kpriv_t;
   64 
   65 /* initialize and check os version current-vs-compiled
   66    This version of the program runs on any machine that will 
   67    allow it to run */
   68 int
   69 ka_init(void)
   70 {
   71     return 0;
   72 }
   73 
   74 /*
   75  * allocate enough memory for the maximum number of procs.
   76  */
   77 ka_open(void **misc)
   78 {
   79     kpriv_t *kp;
   80     off_t vaddr;
   81     int kfd;
   82 
   83     kp = s_malloc(sizeof(kpriv_t));
   84     vaddr = (off_t) sysmp(MP_KERNADDR, MPKA_VAR);
   85     if ((kfd = open(_PATH_KMEM, O_RDONLY)) >= 0)
   86     {
   87     if (lseek(kfd, vaddr, SEEK_SET) == vaddr)
   88     {
   89         struct var v;
   90         
   91         if (read(kfd, &v, sizeof(v)) == sizeof(v))
   92         {
   93         kp->pl = s_malloc(v.v_proc * sizeof(f_user_t));
   94         kp->pr = s_malloc(v.v_proc * sizeof(f_user_t));
   95         *misc = (void *) kp;
   96         return 0;
   97         }
   98     }
   99     close(kfd);
  100     }
  101 }
  102 
  103 /*
  104  * Get a list of process using a addr/port couple
  105  */
  106 static int
  107 getusers(struct sockaddr_in *sin, f_user_t *u)
  108 {
  109     struct fid fid;
  110     f_anonid_t fa;
  111     int n;
  112 
  113     fid.fid_len = sizeof(struct sockaddr_in);
  114     sin->sin_family = AF_INET;
  115     memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
  116     memcpy(fid.fid_data, sin, sizeof(*sin));
  117     fid.fid_data[sizeof(*sin)] = 7;
  118     fa.fa_fid = &fid;
  119     (void) strncpy(fa.fa_fsid, FSID_SOCKET, sizeof fa.fa_fsid);
  120     if ((n = utssys(&fa, F_ANONYMOUS, UTS_FUSERS, u)) <= 0)
  121     return 0;
  122     return n;
  123 }
  124 
  125 /*
  126  * set status and return
  127  * if possible get cmd and args also through procfs
  128  */
  129 static int
  130 stval(f_user_t *u, struct kernel *kp)
  131 {
  132     char pname[256];
  133     int pfd;
  134 
  135 #ifdef IRIX4
  136     s_snprintf(pname, sizeof(pname), "/debug/%05d", (int)u->fu_pid);
  137 #else
  138     s_snprintf(pname, sizeof(pname), "/proc/pinfo/%d", (int)u->fu_pid);
  139 #endif
  140 
  141     kp->cmd = NULL;
  142     kp->argv = NULL;
  143     kp->ruid = kp->euid=u->fu_uid;
  144     if ((pfd = s_open(pname, O_RDONLY)) >= 0)
  145     {
  146     prpsinfo_t info;
  147     prcred_t cr;
  148     
  149     if (ioctl(pfd, PIOCPSINFO, &info) >= 0)
  150     {
  151         kp->cmd = s_malloc(strlen(info.pr_fname) + 1);
  152         strcpy(kp->cmd, info.pr_fname);
  153         kp->argv = s_malloc(strlen(info.pr_psargs) + 1);
  154         strcpy(kp->argv, info.pr_psargs);
  155     }
  156     if (ioctl(pfd, PIOCCRED, &cr) >= 0)
  157     {
  158         kp->euid=cr.pr_euid;
  159         kp->ruid=cr.pr_ruid;
  160     }
  161     close(pfd);
  162     }
  163     kp->status = 1;
  164     kp->pid = u->fu_pid;
  165     return 1;
  166 }
  167 
  168 /*
  169 ** Return the user number for the connection owner
  170 */
  171 int 
  172 ka_lookup(void *vp, struct kernel *kp)
  173 {
  174     int nr, nl;
  175     kpriv_t *pr = (kpriv_t *)vp;
  176 
  177     kp->status = -1;
  178     nl = getusers(&kp->local, pr->pl);
  179     nr = getusers(&kp->remote, pr->pr);
  180     if (nr != 0 && nl != 0)
  181     return stval(&pr->pl[0], kp);
  182     else
  183     {
  184     if (nl != 0)
  185     {
  186         int i, j;
  187 
  188         for (i = 0; i < nr; i++)
  189         for (j = 0; j < nl; j++)
  190             if (pr->pr[i].fu_pid == pr->pl[j].fu_pid)
  191             return stval(&pr->pr[i], kp);
  192 
  193         return stval(&pr->pl[0], kp);
  194     }
  195     else
  196         return stval(&pr->pr[0], kp);
  197     }
  198     return 0;
  199 }
  200