1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
|
/*
nsswitch.c - functions for parsing /etc/nsswitch.conf
Copyright (C) 2011-2015 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include "common.h"
#include "log.h"
/* the cached value of whether shadow lookups use LDAP in nsswitch.conf */
#define NSSWITCH_FILE "/etc/nsswitch.conf"
#define CACHED_UNKNOWN 22
static int cached_shadow_uses_ldap = CACHED_UNKNOWN;
static time_t cached_shadow_lastcheck = 0;
#define CACHED_SHADOW_TIMEOUT (60)
static time_t nsswitch_mtime = 0;
/* the maximum line length supported of nsswitch.conf */
#define MAX_LINE_LENGTH 4096
/* check whether /etc/nsswitch.conf should be related to update
cached_shadow_uses_ldap */
void nsswitch_check_reload(void)
{
struct stat buf;
time_t t;
if ((cached_shadow_uses_ldap != CACHED_UNKNOWN) &&
((t = time(NULL)) > (cached_shadow_lastcheck + CACHED_SHADOW_TIMEOUT)))
{
cached_shadow_lastcheck = t;
if (stat(NSSWITCH_FILE, &buf))
{
log_log(LOG_ERR, "stat(%s) failed: %s", NSSWITCH_FILE, strerror(errno));
/* trigger a recheck anyway */
cached_shadow_uses_ldap = CACHED_UNKNOWN;
return;
}
/* trigger a recheck if file changed */
if (buf.st_mtime != nsswitch_mtime)
{
nsswitch_mtime = buf.st_mtime;
cached_shadow_uses_ldap = CACHED_UNKNOWN;
}
}
}
/* see if the line is a service definition for db and return a pointer to
the beginning of the services list if it is */
static const char *find_db(const char *line, const char *db)
{
int i;
i = strlen(db);
/* the line should begin with the db we're looking for */
if (strncmp(line, db, i) != 0)
return NULL;
/* followed by a : */
while (isspace(line[i]))
i++;
if (line[i] != ':')
return NULL;
i++;
while (isspace(line[i]))
i++;
return line + i;
}
/* check to see if the list of services contains the specified service */
static int has_service(const char *services, const char *service,
const char *filename, int lnr)
{
int i = 0, l;
if (services == NULL)
return 0;
l = strlen(service);
while (services[i] != '\0')
{
/* skip spaces */
while (isspace(services[i]))
i++;
/* check if this is the service */
if ((strncmp(services + i, service, l) == 0) && (!isalnum(services[i + l])))
return 1;
/* skip service name and spaces */
i++;
while (isalnum(services[i]))
i++;
while (isspace(services[i]))
i++;
/* skip action mappings */
if (services[i] == '[')
{
i++; /* skip [ */
while ((services[i] != ']') && (services[i] != '\0'))
i++;
if (services[i] != ']')
{
log_log(LOG_WARNING, "%s: error parsing line %d", filename, lnr);
return 0; /* parse error */
}
i++; /* skip ] */
}
}
return 0;
}
static int shadow_uses_ldap(void)
{
FILE *fp;
int lnr = 0;
char linebuf[MAX_LINE_LENGTH];
const char *services;
int shadow_found = 0;
int passwd_has_ldap = 0;
/* open config file */
if ((fp = fopen(NSSWITCH_FILE, "r")) == NULL)
{
log_log(LOG_ERR, "cannot open %s: %s", NSSWITCH_FILE, strerror(errno));
return 0;
}
/* read file and parse lines */
while (fgets(linebuf, sizeof(linebuf), fp) != NULL)
{
lnr++;
/* see if we have a shadow line */
services = find_db(linebuf, "shadow");
if (services != NULL)
{
shadow_found = 1;
if (has_service(services, MODULE_NAME, NSSWITCH_FILE, lnr))
{
fclose(fp);
return 1;
}
}
/* see if we have a passwd line */
services = find_db(linebuf, "passwd");
if (services != NULL)
passwd_has_ldap = has_service(services, MODULE_NAME, NSSWITCH_FILE, lnr);
}
fclose(fp);
if (shadow_found)
return 0;
return passwd_has_ldap;
}
/* check whether shadow lookups are configured to use ldap */
int nsswitch_shadow_uses_ldap(void)
{
if (cached_shadow_uses_ldap == CACHED_UNKNOWN)
{
log_log(LOG_INFO, "(re)loading %s", NSSWITCH_FILE);
cached_shadow_uses_ldap = shadow_uses_ldap();
cached_shadow_lastcheck = time(NULL);
}
return cached_shadow_uses_ldap;
}
|