"Fossies" - the Fresh Open Source Software Archive

Member "ident2-v1.07_FINAL/sys/m_lsof.c" (10 Apr 2005, 3152 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 "m_lsof.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  * Copyright (C) 2005 Greg Schenzel
    6  *
    7  * This program is free software; you can redistribute it and/or
    8  * modify it under the terms of the GNU General Public License
    9  * as published by the Free Software Foundation; either version 2
   10  * of the License, or (at your option) any later version.
   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.  See the
   15  * GNU General Public License for more details.
   16  *
   17  * You should have received a copy of the GNU General Public License
   18  * along with this program; if not, write to the Free Software
   19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   20  *
   21  * Please view the file README for program information.
   22  */
   23 
   24 /*
   25  *  Support for all OSes with lsof installed.
   26  *  Added 3/27/05 by Greg Schenzel (inittab@netizenweb.com)
   27  */
   28 
   29 #include "ident2.h"
   30 
   31 #define PID_FILE "/var/run/ident2.pid"
   32 
   33     /**
   34      ** drop to the lowest permission level
   35      ** possible. 'nobody' is ideal for most
   36      **/
   37 int
   38 m_reduce_rights (void)
   39 {
   40     struct passwd *pw;
   41 
   42     if ((geteuid() && getuid())
   43     || Dont_Change_Uid == TRUE)
   44         return 0;
   45 
   46     if ((pw = getpwnam ("nobody")) == NULL) {
   47         syslog (LOG_ERR, "error: getpwnam(nobody): %s",
   48                 strerror (errno));
   49         return -1;
   50     } 
   51     if (setuid (pw->pw_uid) == -1) {
   52         syslog (LOG_ERR, "error: setuid(%d): %s",
   53             pw->pw_uid, strerror (errno));
   54         return -1;
   55     }
   56     return 0;
   57 }
   58 
   59 
   60     /**
   61      ** find what user belongs to the connection
   62      ** described by LPORT, RPORT, RADDR, and LADDR.
   63      ** return the uid.
   64      **/    
   65 int
   66 m_get_uid (struct in_addr *laddr, u_short lp,
   67     struct in_addr *raddr, u_short rp)
   68 {
   69     FILE    *fp;
   70     char    cmd[256]; //check for overflow
   71 
   72     unsigned char *locad = (unsigned char *)&(laddr->s_addr);
   73     unsigned char *remad = (unsigned char *)&(raddr->s_addr);
   74     int uid, pid;
   75     char concmd[45];
   76     
   77     sprintf(cmd, "%s -i @%hhu.%hhu.%hhu.%hhu:%hu -n -l -P | grep %hhu.%hhu.%hhu.%hhu:%hu", LSOF_LOCATION,
   78             locad[0], locad[1], locad[2], locad[3], lp,
   79              remad[0], remad[1], remad[2], remad[3], rp);
   80     
   81 //  syslog(LOG_ERR, "about to run: %s", cmd);
   82 
   83     if ((fp = popen (cmd, "r")) == NULL) {
   84         syslog (LOG_ERR, "error reading from lsof: %s",
   85                 strerror (errno));
   86         return -1;
   87     }
   88 
   89 
   90     //concmd and pid are useless placeholders for us. ignore the rest
   91     if (fscanf(fp, "%s %d %d",
   92         concmd, &pid, &uid) < 1) {
   93         uid = -1;
   94     }
   95     
   96     pclose(fp);
   97     return uid;
   98 }
   99 
  100     /*
  101      *  records the pid for service management purposes.
  102      *  example: under Red Hat,Debian,etc pid is written to
  103      *  /var/run/identd.pid
  104      *  PID support suggested (and previously implemented)
  105      *  by Alexander Reelsen.
  106      */
  107 int
  108 m_register_pid (void)
  109 {
  110 #ifdef HAS_VAR_RUN
  111     FILE    *fp;
  112 
  113     if ((fp = fopen (PID_FILE, "w")) == NULL) {
  114         syslog (LOG_WARNING, "couldn't record pid in %s: %s -- "
  115             "automatic shutdown with system not available",
  116             PID_FILE, strerror (errno));
  117         return -1;
  118     }
  119     fprintf (fp, "%u\n", (unsigned int)getpid()); 
  120     fclose (fp);
  121 #endif  
  122     return 0;
  123 }