|
logindefs
logindefs
/* Copyright (C) 2003, 2004 Thorsten Kukuk
Author: Thorsten Kukuk
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
#ifdef HAVE_CONFIG_H
#include
#endif
#define _GNU_SOURCE
#include
#include
#include
#include
#include
#include
#include "logindefs.h"
struct item {
char *name; /* name of the option. */
char *value; /* value of the option. */
struct item *next; /* pointer to next option. */
};
static struct item *list = NULL;
/* Add a new entry to the list. */
static void
store (const char *name, const char *value)
{
struct item *new = malloc (sizeof (struct item));
if (new == NULL)
abort ();
if (name == NULL)
abort ();
new->name = strdup (name);
new->value = strdup (value?:"");
new->next = list;
list = new;
}
/* search a special entry in the list and return the value. */
static const char *
search (const char *name)
{
struct item *ptr;
ptr = list;
while (ptr != NULL)
{
if (strcasecmp (name, ptr->name) == 0)
return ptr->value;
ptr = ptr->next;
}
return NULL;
}
/* Load the login.defs file (/etc/login.defs) */
static void
load_defaults_internal (const char *filename)
{
FILE *fp;
char *buf = NULL;
size_t buflen = 0;
fp = fopen (filename, "r");
if (NULL == fp)
return;
while (!feof (fp))
{
char *tmp, *cp;
#if defined(HAVE_GETLINE)
ssize_t n = getline (&buf, &buflen, fp);
#elif defined (HAVE_GETDELIM)
ssize_t n = getdelim (&buf, &buflen, '\n', fp);
#else
ssize_t n;
if (buf == NULL)
{
buflen = 8096;
buf = malloc (buflen);
}
buf[0] = '\0';
fgets (buf, buflen - 1, fp);
if (buf != NULL)
n = strlen (buf);
else
n = 0;
#endif /* HAVE_GETLINE / HAVE_GETDELIM */
cp = buf;
if (n < 1)
break;
tmp = strchr (cp, '#'); /* remove comments */
if (tmp)
*tmp = '\0';
while (isspace ((int)*cp)) /* remove spaces and tabs */
++cp;
if (*cp == '\0') /* ignore empty lines */
continue;
if (cp[strlen (cp) - 1] == '\n')
cp[strlen (cp) - 1] = '\0';
tmp = strsep (&cp, " \t=");
if (cp != NULL)
while (isspace ((int)*cp) || *cp == '=')
++cp;
store (tmp, cp);
}
fclose (fp);
if (buf)
free (buf);
}
static void
load_defaults (void)
{
load_defaults_internal ("/etc/default/passwd");
load_defaults_internal ("/etc/login.defs");
}
int
getlogindefs_bool (const char *name, int dflt)
{
const char *val;
if (list == NULL)
load_defaults ();
val = search (name);
if (val == NULL)
return dflt;
return (strcasecmp (val, "yes") == 0);
}
long
getlogindefs_num (const char *name, long dflt)
{
const char *val;
char *cp;
long retval;
if (list == NULL)
load_defaults ();
val = search (name);
if (val == NULL)
return dflt;
retval = strtol (val, &cp, 0);
if (*cp != '\0' ||
((retval == LONG_MAX || retval == LONG_MIN) && errno == ERANGE))
{
fprintf (stderr,
"login.defs: %s contains invalid numerical value: %s!\n",
name, val);
retval = dflt;
}
return retval;
}
unsigned long
getlogindefs_unum (const char *name, unsigned long dflt)
{
const char *val;
char *cp;
unsigned long retval;
if (list == NULL)
load_defaults ();
val = search (name);
if (val == NULL)
return dflt;
retval = strtoul (val, &cp, 0);
if (*cp != '\0' || (retval == ULONG_MAX && errno == ERANGE))
{
fprintf (stderr,
"login.defs: %s contains invalid numerical value: %s!\n",
name, val);
retval = dflt;
}
return retval;
}
const char *
getlogindefs_str (const char *name, const char *dflt)
{
const char *retval;
if (list == NULL)
load_defaults ();
retval = search (name);
return retval ?: dflt;
}
#if defined(TEST)
int
main ()
{
printf ("CYPT=%s\n", getlogindefs_str ("cRypt", "no"));
printf ("LOG_UNKFAIL_ENAB=%s\n", getlogindefs_str ("log_unkfail_enab",""));
printf ("DOESNOTEXIST=%s\n", getlogindefs_str ("DOESNOTEXIST","yes"));
return 0;
}
#endif
JSP Web Hosting
/*
* Copyright (c) 2004 Thorsten Kukuk
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, and the entire permission notice in its entirety,
* including the disclaimer of warranties.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* ALTERNATIVELY, this product may be distributed under the terms of
* the GNU Public License, in which case the provisions of the GPL are
* required INSTEAD OF the above restrictions. (This clause is
* necessary due to a potential bad interaction between the GPL and
* the restrictions contained in a BSD-style copyright.)
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
extern char *utf8_to_locale (const char *str);
extern char *locale_to_utf8 (const char *str);
Page:
1
2
3
4
5
6
7
8
9
10
JSP Web Host
Cheap JSP Hosting
JSP Web Site Hosting
JSP Web Site Hosting
|