"Fossies" - the Fresh Open Source Software Archive

Member "ident2-v1.07_FINAL/machine.c" (22 Jul 2005, 3169 Bytes) of package /linux/privat/old/ident2-v1.07_FINAL.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 "machine.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Ident-2 - an Identity server for UNIX
    3  * Copyright (C) 1998-2001 Michael Bacarella
    4  * Copyright (C) 2003 Netgraft Corporation
    5  *
    6  * This program is free software; you can redistribute it and/or
    7  * modify it under the terms of the GNU General Public License
    8  * as published by the Free Software Foundation; either version 2
    9  * of the License, or (at your option) any later version.
   10  *
   11  * This program is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License
   17  * along with this program; if not, write to the Free Software
   18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   19  *
   20  * Please view the file README for program information.
   21  */
   22 
   23 /*
   24  *  Support for Linux machine dependancies
   25  */
   26 
   27 #include "ident2.h"
   28 
   29 #define PID_FILE "/var/run/ident2.pid"
   30 
   31     /**
   32      ** drop to the lowest permission level
   33      ** possible. 'nobody' is ideal for linux
   34      **/
   35 int
   36 m_reduce_rights (void)
   37 {
   38     struct passwd *pw;
   39 
   40     if ((geteuid() && getuid())
   41     || Dont_Change_Uid == TRUE)
   42         return 0;
   43 
   44     if ((pw = getpwnam ("nobody")) == NULL) {
   45         syslog (LOG_ERR, "error: getpwnam(nobody): %s",
   46                 strerror (errno));
   47         return -1;
   48     } 
   49     if (setuid (pw->pw_uid) == -1) {
   50         syslog (LOG_ERR, "error: setuid(%d): %s",
   51             pw->pw_uid, strerror (errno));
   52         return -1;
   53     }
   54     return 0;
   55 }
   56 
   57 
   58     /**
   59      ** find what user belongs to the connection
   60      ** described by LPORT, RPORT, RADDR, and LADDR.
   61      ** return the uid.
   62      **/    
   63 int
   64 m_get_uid (struct in_addr *laddr, u_short lp,
   65     struct in_addr *raddr, u_short rp)
   66 {
   67     FILE    *fp;
   68     char    buf[150];
   69 
   70     if ((fp = fopen ("/proc/net/tcp", "r")) == NULL) {
   71         syslog (LOG_ERR, "error reading /proc/net/tcp: %s",
   72                 strerror (errno));
   73         return -1;
   74     }
   75 
   76     fgets (buf, 149, fp);       /* eat header!)*$ */
   77 
   78     while (fgets (buf, 149, fp)) {
   79         unsigned long local_addr, remote_addr;
   80         unsigned long tx_queue, rx_queue, tm_when;
   81         int sl, uid, retrnsmt, st, tr, local_port, remote_port;
   82         
   83         if (sscanf (buf, "%d: %lX:%x %lX:%x %x %lX:%lX %x:%lX %x %d",
   84 
   85             &sl, &local_addr, &local_port, &remote_addr,
   86             &remote_port, &st, &tx_queue, &rx_queue,
   87             &tr, &tm_when, &retrnsmt, &uid) == 12) {
   88 
   89             if (lp == local_port && rp == remote_port
   90             && remote_addr == raddr->s_addr) {
   91                 if (laddr == NULL) {
   92                     fclose (fp);
   93                     return uid;
   94                 }
   95                 else if (laddr->s_addr
   96                 == local_addr) {
   97                     fclose (fp);
   98                     return uid;
   99                 }
  100             }
  101         }
  102     }
  103     fclose (fp);
  104     return -1;
  105 }
  106 
  107     /*
  108      *  records the pid for service management purposes.
  109      *  example: under Red Hat,Debian,etc pid is written to
  110      *  /var/run/identd.pid
  111      *  PID support suggested (and previously implemented)
  112      *  by Alexander Reelsen.
  113      */
  114 int
  115 m_register_pid (void)
  116 {
  117 #ifdef HAS_VAR_RUN
  118     FILE    *fp;
  119 
  120     if ((fp = fopen (PID_FILE, "w")) == NULL) {
  121         syslog (LOG_WARNING, "couldn't record pid in %s: %s -- "
  122             "automatic shutdown with system not available",
  123             PID_FILE, strerror (errno));
  124         return -1;
  125     }
  126     fprintf (fp, "%u\n", getpid ()); 
  127     fclose (fp);
  128 #endif  
  129     return 0;
  130 }