"Fossies" - the Fresh Open Source Software Archive

Member "cgiwrap-4.1/getusershell.c" (16 Jun 2008, 4039 Bytes) of package /linux/www/old/cgiwrap-4.1.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.

    1 /*
    2  * Copyright (c) 1985 Regents of the University of California.
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms are permitted
    6  * provided that: (1) source distributions retain this entire copyright
    7  * notice and comment, and (2) distributions including binaries display
    8  * the following acknowledgement:  ``This product includes software
    9  * developed by the University of California, Berkeley and its contributors''
   10  * in the documentation or other materials provided with the distribution
   11  * and in all advertising materials mentioning features or use of this
   12  * software. Neither the name of the University nor the names of its
   13  * contributors may be used to endorse or promote products derived
   14  * from this software without specific prior written permission.
   15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
   16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
   17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
   18  */
   19 
   20 #if defined(LIBC_SCCS) && !defined(lint)
   21 static char sccsid[] = "@(#)$Id: getusershell.c 280 2004-09-23 00:48:14Z nneul $";
   22 
   23 #endif /* LIBC_SCCS and not lint */
   24 
   25 #ifndef _AIX
   26 
   27 #include "config.h"
   28 #include <sys/types.h>
   29 #include <sys/param.h>
   30 #include <sys/file.h>
   31 #include <sys/stat.h>
   32 #include <stdlib.h>
   33 #include <ctype.h>
   34 #include <stdio.h>
   35 
   36 
   37 #define SHELLS "/etc/shells"
   38 
   39 /*
   40  * Do not add local shells here.  They should be added in /etc/shells
   41  */
   42 static char *okshells[] =
   43 {"/bin/sh", "/bin/csh", 0};
   44 
   45 static char **shells,
   46  *strings;
   47 static char **curshell = NULL;
   48 static char **initshells();
   49 
   50 /*
   51  * Get a list of shells from SHELLS, if it exists.
   52  */
   53 char *
   54 #ifdef __STDC__
   55 getusershell(void)
   56 #else
   57 getusershell()
   58 #endif
   59 {
   60     char *ret;
   61 
   62     if (curshell == NULL)
   63         curshell = initshells();
   64     ret = *curshell;
   65     if (ret != NULL)
   66         curshell++;
   67     return(ret);
   68 }
   69 
   70 void
   71 #ifdef __STDC__
   72 endusershell(void)
   73 #else
   74 endusershell()
   75 #endif
   76 {
   77     if (shells != NULL)
   78         free((char *) shells);
   79     shells = NULL;
   80     if (strings != NULL)
   81         free(strings);
   82     strings = NULL;
   83     curshell = NULL;
   84 }
   85 
   86 void
   87 #ifdef __STDC__
   88 setusershell(void)
   89 #else
   90 setusershell()
   91 #endif
   92 {
   93     curshell = initshells();
   94 }
   95 
   96 static char **
   97 #ifdef __STDC__
   98 initshells(void)
   99 #else
  100 initshells()
  101 #endif
  102 {
  103     register char **sp,
  104      *cp;
  105     register FILE *fp;
  106     struct stat statb;
  107 
  108     if (shells != NULL)
  109         free((char *) shells);
  110     shells = NULL;
  111     if (strings != NULL)
  112         free(strings);
  113     strings = NULL;
  114     if ((fp = fopen(SHELLS, "r")) == (FILE *) 0)
  115         return (okshells);
  116     if (fstat(fileno(fp), &statb) == -1) {
  117         (void) fclose(fp);
  118         return (okshells);
  119     }
  120     if ((strings = (char *) malloc((unsigned) statb.st_size + 1)) == NULL) {
  121         (void) fclose(fp);
  122         return (okshells);
  123     }
  124     shells = (char **) calloc((unsigned) statb.st_size / 3, sizeof(char *));
  125 
  126     if (shells == NULL) {
  127         (void) fclose(fp);
  128         free(strings);
  129         strings = NULL;
  130         return (okshells);
  131     }
  132     sp = shells;
  133     cp = strings;
  134     while (fgets(cp, MAXPATHLEN + 1, fp) != NULL) {
  135         while (*cp != '#' && *cp != '/' && *cp != '\0')
  136             cp++;
  137         if (*cp == '#' || *cp == '\0')
  138             continue;
  139         *sp++ = cp;
  140         while (!isspace(*cp) && *cp != '#' && *cp != '\0')
  141             cp++;
  142         *cp++ = '\0';
  143     }
  144     *sp = (char *) 0;
  145     (void) fclose(fp);
  146     return (shells);
  147 }
  148 
  149 #else /* it is AIX */
  150          
  151 
  152 /* emulate getusershell for AIX */
  153 
  154 #include <userconf.h>
  155 #include <usersec.h>
  156 
  157 static int GETUSERSHELL_opened=0;
  158 static char **GETUSERSHELL_shells;
  159 static int GETUSERSHELL_current;
  160               
  161 
  162 char *getusershell()
  163 {       
  164     static char *val;
  165     static char *list;
  166     static char *retVal;
  167     int n;
  168                     
  169     if (!GETUSERSHELL_opened)
  170     {
  171         if(getconfattr(SC_SYS_LOGIN,SC_SHELLS,(void *)&val,SEC_LIST))
  172         {
  173             return(NULL);
  174         }
  175         GETUSERSHELL_opened = 1;
  176         GETUSERSHELL_current = 0;
  177         list = val;
  178     }      
  179                    
  180     if ( (list != NULL) && (*list != NULL) )
  181     {
  182     while (list && *list)
  183         list++;
  184         
  185     *list = '\0';       
  186     
  187     retVal = val;
  188     list++; 
  189     val = list;
  190     }
  191     else
  192         retVal = NULL;
  193     
  194     return(retVal);
  195     
  196 }
  197 
  198 void setusershell()
  199 {
  200     GETUSERSHELL_opened = 0;
  201 }
  202 
  203 void endusershell()
  204 {
  205     GETUSERSHELL_opened = 0;
  206 }
  207 
  208 #endif