"Fossies" - the Fresh Open Source Software Archive

Member "pidentd-3.0.19/src/k_linux.c" (8 Jan 2003, 3335 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_linux.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2 ** k_linux.c - Linux 0.99.13q or later kernel access functions
    3 **
    4 ** Copyright (c) 1997-1998 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 #include "config.h"
   16 
   17 #include <stdio.h>
   18 #include <syslog.h>
   19 #include <errno.h>
   20 
   21 #include <sys/types.h>
   22 #include <sys/param.h>
   23 #include <sys/socket.h>
   24 #include <netinet/in.h>
   25 #include <arpa/inet.h>
   26 
   27 #include "pidentd.h"
   28 
   29 /*
   30 ** Make sure we are running on a supported OS version
   31 */
   32 int
   33 ka_init(void)
   34 {
   35     return 0; /* We always succeed */
   36 }
   37 
   38 
   39 int
   40 ka_open(void **misc)
   41 {
   42     FILE *fp;
   43 
   44     
   45     while ((fp = fopen("/proc/net/tcp", "r")) == NULL && errno == EINTR)
   46     ;
   47     
   48     if (fp == NULL)
   49     {
   50     syslog(LOG_ERR, "fopen(\"/proc/net/tcp\", \"r\"): %m");
   51     return -1;
   52     }
   53 
   54     *misc = (void *) fp;
   55     return 0;
   56 }
   57 
   58 
   59 int 
   60 ka_lookup(void *vp, struct kernel *kp)
   61 {
   62     FILE *fp;
   63     long dummy;
   64     char buf[512];
   65     long r_laddr, r_raddr, myladdr, myraddr;
   66     int r_lport, r_rport, mylport, myrport;
   67     int euid;
   68     int nra;
   69     
   70 /*
   71  * PSz 11 Dec 02
   72  * 
   73  * We have observed Debian identd (on a fairly busy dual-CPU machine)
   74  * sometimes reporting 'root' for connections belonging to "real" users.
   75  * File /proc/net/tcp is written mostly in routine get__sock in
   76  * /usr/src/kernel-source-2.2.19/net/ipv4/proc.c :
   77  * 
   78  *  sprintf(tmpbuf, "%4d: %08lX:%04X %08lX:%04X"
   79  *      " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu",
   80  *      i, src, srcp, dest, destp, sp->state, 
   81  *      (tw_bucket ?
   82  *       0 :
   83  *       (format == 0) ?
   84  *       tp->write_seq-tp->snd_una : atomic_read(&sp->wmem_alloc)),
   85  *      (tw_bucket ?
   86  *       0 :
   87  *       (format == 0) ?
   88  *       tp->rcv_nxt-tp->copied_seq: atomic_read(&sp->rmem_alloc)),
   89  *      timer_active, timer_expires-jiffies,
   90  *      (tw_bucket ? 0 : tp->retransmits),
   91  *      (!tw_bucket && sp->socket) ? sp->socket->inode->i_uid : 0,
   92  *      (!tw_bucket && timer_active) ? sp->timeout : 0,
   93  *      (!tw_bucket && sp->socket) ? sp->socket->inode->i_ino : 0);
   94  * 
   95  * Should we skip lines with just ino, or both uid and ino, zero?
   96  */
   97     unsigned long int ino;
   98 
   99     
  100     r_rport = ntohs(kp->remote.sin_port);
  101     r_lport = ntohs(kp->local.sin_port);
  102     r_raddr = kp->remote.sin_addr.s_addr;
  103     r_laddr = kp->local.sin_addr.s_addr;
  104 
  105     fp = (FILE *) vp;
  106 
  107     kp->ruid = NO_UID;
  108     rewind(fp);
  109 
  110     /* eat header */
  111     if (fgets(buf, sizeof(buf)-1,fp) == NULL)
  112     return -1;
  113 
  114     while (fgets(buf, sizeof(buf)-1, fp) != NULL)
  115     {
  116     nra = sscanf(buf, "%d: %lX:%x %lX:%x %x %lX:%lX %x:%lX %lx %d %ld %lu",
  117              &dummy, &myladdr, &mylport, &myraddr, &myrport,
  118              &dummy, &dummy, &dummy, &dummy, &dummy, &dummy,
  119              &euid, &dummy, &ino);
  120     if (nra >= 12)
  121     {
  122         if (myladdr == r_laddr && mylport == r_lport &&
  123         myraddr == r_raddr && myrport == r_rport)
  124         {
  125         if (nra >= 14 && euid == 0 && ino == 0) {
  126           /*
  127            * Both uid and ino are zero: not even a socket?
  128            * Skip (continue), probably fail; or fail (break)
  129            * straight away? Hopefully we retry later.
  130            */
  131           continue;
  132         }
  133         kp->euid = euid;
  134         return 1;
  135         }
  136     }
  137     }
  138 
  139     return -1;
  140 }
  141