Back to index

glibc  2.9
nss-default.c
Go to the documentation of this file.
00001 /* Copyright (C) 1996, 2001, 2004, 2006, 2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <ctype.h>
00020 #include <errno.h>
00021 #include <stdio.h>
00022 #include <stdio_ext.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <bits/libc-lock.h>
00026 
00027 #include <libnsl.h>
00028 
00029 
00030 /* Path of the file.  */
00031 static const char default_nss[] = "/etc/default/nss";
00032 
00033 /* Flags once read from the file.  */
00034 static int default_nss_flags;
00035 
00036 /* Code to make sure we call 'init' once.  */
00037 __libc_once_define (static, once);
00038 
00039 /* Table of the recognized variables.  */
00040 static const struct
00041 {
00042   char name[23];
00043   unsigned int len;
00044   int flag;
00045 } vars[] =
00046   {
00047 #define STRNLEN(s) s, sizeof (s) - 1
00048     { STRNLEN ("NETID_AUTHORITATIVE"), NSS_FLAG_NETID_AUTHORITATIVE },
00049     { STRNLEN ("SERVICES_AUTHORITATIVE"), NSS_FLAG_SERVICES_AUTHORITATIVE },
00050     { STRNLEN ("SETENT_BATCH_READ"), NSS_FLAG_SETENT_BATCH_READ }
00051   };
00052 #define nvars (sizeof (vars) / sizeof (vars[0]))
00053 
00054 
00055 static void
00056 init (void)
00057 {
00058   int saved_errno = errno;
00059   FILE *fp = fopen (default_nss, "rc");
00060   if (fp != NULL)
00061     {
00062       char *line = NULL;
00063       size_t linelen = 0;
00064 
00065       __fsetlocking (fp, FSETLOCKING_BYCALLER);
00066 
00067       while (!feof_unlocked (fp))
00068        {
00069          ssize_t n = getline (&line, &linelen, fp);
00070          if (n <= 0)
00071            break;
00072 
00073          /* Recognize only
00074 
00075               <THE-VARIABLE> = TRUE
00076 
00077             with arbitrary white spaces.  */
00078          char *cp = line;
00079          while (isspace (*cp))
00080            ++cp;
00081 
00082          /* Recognize comment lines.  */
00083          if (*cp == '#')
00084            continue;
00085 
00086          int idx;
00087          for (idx = 0; idx < nvars; ++idx)
00088            if (strncmp (cp, vars[idx].name, vars[idx].len) == 0)
00089              break;
00090          if (idx == nvars)
00091            continue;
00092 
00093          cp += vars[idx].len;
00094          while (isspace (*cp))
00095            ++cp;
00096          if (*cp++ != '=')
00097            continue;
00098          while (isspace (*cp))
00099            ++cp;
00100 
00101          if (strncmp (cp, "TRUE", 4) != 0)
00102            continue;
00103          cp += 4;
00104 
00105          while (isspace (*cp))
00106            ++cp;
00107 
00108          if (*cp == '\0')
00109            default_nss_flags |= vars[idx].flag;
00110        }
00111 
00112       free (line);
00113 
00114       fclose (fp);
00115     }
00116   __set_errno (saved_errno);
00117 }
00118 
00119 
00120 int
00121 _nsl_default_nss (void)
00122 {
00123   /* If we have not yet read the file yet do it now.  */
00124   __libc_once (once, init);
00125 
00126   return default_nss_flags;
00127 }