"Fossies" - the Fresh Open Source Software Archive

Member "ident2-v1.07_FINAL/ident2.c" (22 Jul 2005, 7399 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 "ident2.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 #include "ident2.h"
   25 
   26 #ifdef HAS_GETOPT_LONG
   27 #include <getopt.h> /* getopt() is declared in unistd.h */
   28 #endif
   29 
   30     /*
   31      *  determine if we're running as a child of inetd
   32      *  or if we were started via a command line (or from
   33      *  a process not inetd)
   34      */
   35 static int
   36 inetd_child (void)
   37 {
   38     struct sockaddr_in sin;
   39     int sinsize = sizeof (struct sockaddr_in);
   40     
   41         /* if child of inetd, 0 would be a socket */
   42     if (getsockname (0, (struct sockaddr *)&sin, (int *)&sinsize) == -1) {
   43         if (errno == ENOTSOCK)
   44             return 0;   /* must be a filedescriptor */
   45         else {
   46             syslog (LOG_ERR, "getsockname: %s\n", strerror (errno));
   47             exit (-1);
   48         }
   49     }
   50     return 1;
   51 }
   52 
   53 #define print_header()  printf ("Ident2 version %s, Copyright (C) 1999-2004 Michael Bacarella\n", ID_VERSION)
   54 
   55 static void
   56 print_usage (void)
   57 {
   58     print_header();
   59     puts ("Example command lines:");
   60 puts ("     ident2 -r       sends random replies, always");
   61 puts ("     ident2 -i -y .reply allows user replies, which are read\n"
   62       "                 from the file ~/.reply");
   63 puts ("     ident2 -m 5 -o 10   ultra secure mode. no more than\n"
   64       "                 5 connections at once, each connection\n"
   65       "                 is killed after 10 seconds regardless.");
   66 puts ("     ident2 -s       stop ident2 from trying to change it's\n"
   67       "                 userid if it discovers that it's running\n"
   68       "                 as a root process.");
   69 }
   70 
   71 static void
   72 print_license (void)
   73 {
   74     print_header();
   75     puts(
   76 "ident2 is free software, and you are welcome to redistribute it\n"
   77 "under certain conditions; ident2 comes with ABSOLUTELY NO WARRANTY;\n"
   78 "for details, go to http://www.gnu.org/\n");
   79 }
   80 
   81 static void
   82 print_help (void)
   83 {
   84     print_header();
   85 #ifdef HAS_GETOPT_LONG
   86     puts (  
   87 "usage: ident2 [options]\n"
   88 "options:\n"
   89 "\n"
   90 "these parameters apply to all incarnations of ident2\n"
   91 "\n"
   92 "   -h  --help          this command line information\n"
   93 "   -u  --usage         shows example command lines\n"
   94 "   -v  --version       show version information\n"
   95 "   -l  --license       show licensing information\n"
   96 "   -a  --force-inetd       force inetd mode\n"
   97 "   -d  --force-daemon      force standalone daemon\n"
   98 "   -i  --use-user-ident    allow user defined ident replies\n"
   99 "   -y  --user-reply-file   file in user's homedir for replies\n"
  100 "   -n  --allow-noident-file    don't reply if user has a ~/NOIDENT file\n"
  101 "   -o  --client-timeout    clients timeout after this many secs\n"
  102 "   -s  --dont-change-uid   don't try to change uid (to nobody)\n"
  103 "   -r  --always-random     always send a random reply\n"
  104 "\n"
  105 "these parameters only apply to the daemon ident2\n"
  106 "\n"
  107 "   -m  --daemon-maxclients accept no more than this many clients\n"
  108 "   -p  --daemon-port       bind this port, instead of 'auth'\n"
  109 );
  110 #else
  111     puts(
  112 "usage: ident2 [options]\n"
  113 "options:\n"
  114 "\n"
  115 "these parameters apply to all incarnations of ident2\n"
  116 "\n"
  117 "   -h      this command line information\n"
  118 "   -u      shows example command lines\n"
  119 "   -v      show version information\n"
  120 "   -l      show licensing information\n"
  121 "   -a      force inetd mode\n"
  122 "   -d      force standalone daemon\n"
  123 "   -i      allow user defined ident replies\n"
  124 "   -y      file in user's homedir for replies\n"
  125 "   -n      don't reply if user has a ~/NOIDENT file\n"
  126 "   -o      clients timeout after this many secs\n"
  127 "   -s      don't try to change uid (ex: to nobody)\n"
  128 "   -r      always send a random reply, even to bad requests\n"
  129 "\n"
  130 "these parameters only apply to the daemon ident2\n"
  131 "\n"
  132 "   -m      accept no more than this many clients\n"
  133 "   -p      bind this port, instead of 'auth'\n"
  134 );
  135 #endif
  136 }
  137 
  138         /*
  139          *      print version info
  140          */
  141 static void
  142 print_version (void)
  143 {
  144         printf ("Ident2 version %s, maintainer %s\n\n",
  145                 ID_VERSION, ID_MAINTAINER);
  146 }
  147 
  148 
  149 static void
  150 command_line (int argc, char *argv[])
  151 {
  152     int c;
  153             /* getopt_long support implemented
  154              * by Alexander Reelsen <ar@rhwd.net> */
  155 #ifdef HAS_GETOPT_LONG
  156     while (1) {
  157 /* int this_option_optind = optind ? optind : 1; */ /*unused?*/
  158         int option_index = 0;
  159         static struct option long_options[] = {
  160             {"allow-noident-file", 0, 0, 'n'},
  161             {"help", 0, 0, 'h'},
  162             {"usage", 0, 0, 'u'},
  163             {"version", 0, 0, 'v'},
  164             {"license", 0, 0, 'l'},
  165             {"force-inetd", 0, 0, 'a'},
  166             {"force-daemon", 0, 0, 'd'},
  167             {"use-user-ident", 0, 0, 'i'},
  168             {"dont-change-uid", 0, 0, 's'},
  169             {"always-random", 0, 0, 'r'},
  170             {"user-reply-file", 1, 0, 'y'},
  171             {"client-timeout", 1, 0, 'o'},
  172             {"daemon-maxclients", 1, 0, 'm'},
  173             {"daemon-port", 1, 0, 'p'},
  174             {0, 0, 0, 0},
  175         };
  176 
  177         c = getopt_long (argc, argv, "nhuvrliady:o:ftTsm:p:",
  178             long_options, &option_index);
  179         if (c == -1) {
  180             break;
  181         }   
  182 #else
  183         while ((c = getopt (argc, argv, "nhuvrliady:o:ftTsm:p:")) != -1) {
  184 #endif
  185         switch (c) {
  186             case 'n':
  187                 Allow_NOIDENT = TRUE;
  188                 break;
  189             case 'h':
  190                 print_help ();
  191                 exit (0);
  192             case 'u':
  193                 print_usage ();
  194                 exit (0);   
  195             case 'v':
  196                 print_version ();
  197                 exit (0);
  198             case 'l':
  199                 print_license ();
  200                 exit (0);
  201             case 'a':
  202                 Service_Type = INETD;
  203                 break;
  204             case 'd':
  205                 Service_Type = DAEMON;
  206                 break;
  207             case 'i':
  208                 Use_User_Ident = TRUE;
  209                 break;
  210             case 's':
  211                 Dont_Change_Uid = TRUE;
  212                 break;
  213             case 'r':
  214                 Reply_Always_Random = TRUE;
  215                 break;
  216             case 'y':
  217                 User_Ident_File = optarg;
  218                 break;
  219             case 'o':
  220                 if ((Client_Timeout = atol (optarg)) == 0) {
  221                     fprintf (stderr, "ERROR: Ident2: bad "
  222                         "value for arguement `o'\n");
  223                     exit (1);
  224                 }
  225                 break;
  226             case 'm':
  227                 if ((Max_Connections = atol (optarg)) == 0) {
  228                     fprintf (stderr, "ERROR: Ident2: bad"
  229                         " value for arguement `m'\n");
  230                     exit (1);
  231                 }
  232                 break;
  233             case 'p':
  234                 if ((Ident_Port = atoi (optarg)) == 0) { 
  235                     fprintf (stderr, "ERROR: Ident2: bad"
  236                         " value for arguement `p'\n");
  237                     exit (1);
  238                 }
  239                 break;
  240             case '?':
  241             default:
  242 #ifdef HAS_GETOPT_LONG
  243                 fprintf (stderr, "ident2: bad command line:\n"
  244                 "\t``ident2 --help | more'' for help\n"
  245                 "\t``ident2 --usage'' for some examples\n\n");
  246 #else
  247                 fprintf (stderr, "ident2: bad command line:\n"
  248                 "\t``ident2 -h | more'' for help\n"
  249                 "\t``ident2 -u'' for some examples\n\n");
  250 #endif
  251                 exit (1);
  252         }
  253     }
  254 }
  255 
  256 
  257 int
  258 main (int argc, char *argv[])
  259 {   
  260     INIT_GLOBALS();
  261     command_line (argc, argv);
  262         
  263             /* try to determine the service type if nothing
  264              * has been forced (by commandline) */
  265     if (Service_Type == NO_TYPE) {
  266         if (inetd_child ())
  267             Service_Type = INETD;
  268         else
  269             Service_Type = DAEMON;
  270     }
  271 
  272     if (Service_Type == INETD) {
  273         openlog ("in.ident2", LOG_PID, LOG_AUTH);
  274         child_service (STDIN_FILENO, STDOUT_FILENO);
  275         closelog ();
  276     }
  277     else
  278         daemon_service ();
  279 
  280     return 0;
  281 }
  282