2007-10-07 17:14:02 +05:30
|
|
|
/*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-FileCopyrightText: 1989 - 1994, Julianne Frances Haugh
|
|
|
|
* SPDX-FileCopyrightText: 1996 - 1999, Marek Michałkiewicz
|
|
|
|
* SPDX-FileCopyrightText: 2003 - 2006, Tomasz Kłoczko
|
|
|
|
* SPDX-FileCopyrightText: 2007 - 2010, Nicolas François
|
2007-10-07 17:14:02 +05:30
|
|
|
*
|
2021-12-05 21:05:27 +05:30
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Separated from setup.c. --marekm
|
|
|
|
* Resource limits thanks to Cristian Gafton.
|
2010-03-19 04:50:24 +05:30
|
|
|
* Enhancements of resource limit code by Thomas Orgis <thomas@orgis.org>
|
2007-10-07 17:14:02 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2007-10-07 17:16:07 +05:30
|
|
|
#ifndef USE_PAM
|
|
|
|
|
2007-11-11 05:16:11 +05:30
|
|
|
#ident "$Id$"
|
2007-10-07 17:17:01 +05:30
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdio.h>
|
2010-03-19 04:50:15 +05:30
|
|
|
#include <ctype.h>
|
2007-10-07 17:14:02 +05:30
|
|
|
#include "prototypes.h"
|
|
|
|
#include "defines.h"
|
|
|
|
#include <pwd.h>
|
|
|
|
#include "getdef.h"
|
2021-11-29 05:07:53 +05:30
|
|
|
#include "shadowlog.h"
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef HAVE_SYS_RESOURCE_H
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#define LIMITS
|
|
|
|
#endif
|
|
|
|
#ifdef LIMITS
|
|
|
|
#ifndef LIMITS_FILE
|
|
|
|
#define LIMITS_FILE "/etc/limits"
|
|
|
|
#endif
|
|
|
|
#define LOGIN_ERROR_RLIMIT 1
|
|
|
|
#define LOGIN_ERROR_LOGIN 2
|
|
|
|
/* Set a limit on a resource */
|
|
|
|
/*
|
|
|
|
* rlimit - RLIMIT_XXXX
|
|
|
|
* value - string value to be read
|
|
|
|
* multiplier - value*multiplier is the actual limit
|
|
|
|
*/
|
2010-03-19 04:50:24 +05:30
|
|
|
static int setrlimit_value (unsigned int resource,
|
|
|
|
const char *value,
|
|
|
|
unsigned int multiplier)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
struct rlimit rlim;
|
2010-03-19 04:50:15 +05:30
|
|
|
rlim_t limit;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2010-03-19 04:50:15 +05:30
|
|
|
/* The "-" is special, not belonging to a strange negative limit.
|
2010-03-19 04:50:24 +05:30
|
|
|
* It is infinity, in a controlled way.
|
|
|
|
*/
|
|
|
|
if ('-' == value[0]) {
|
2010-03-19 04:50:15 +05:30
|
|
|
limit = RLIM_INFINITY;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2010-03-19 04:50:15 +05:30
|
|
|
else {
|
|
|
|
/* We cannot use getlong here because it fails when there
|
2010-03-19 04:50:24 +05:30
|
|
|
* is more to the value than just this number!
|
|
|
|
* Also, we are limited to base 10 here (hex numbers will not
|
|
|
|
* work with the limit string parser as is anyway)
|
|
|
|
*/
|
2010-03-19 04:50:15 +05:30
|
|
|
char *endptr;
|
2010-03-19 04:50:24 +05:30
|
|
|
long longlimit = strtol (value, &endptr, 10);
|
2010-03-19 04:50:15 +05:30
|
|
|
if ((0 == longlimit) && (value == endptr)) {
|
|
|
|
/* No argument at all. No-op.
|
2010-03-19 04:50:24 +05:30
|
|
|
* FIXME: We could instead throw an error, though.
|
|
|
|
*/
|
2010-03-19 04:50:15 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
longlimit *= multiplier;
|
|
|
|
limit = (rlim_t)longlimit;
|
2010-03-19 04:50:24 +05:30
|
|
|
if (longlimit != limit)
|
2010-03-19 04:50:15 +05:30
|
|
|
{
|
2010-03-19 04:50:24 +05:30
|
|
|
/* FIXME: Again, silent error handling...
|
|
|
|
* Wouldn't screaming make more sense?
|
|
|
|
*/
|
2010-03-19 04:50:15 +05:30
|
|
|
return 0;
|
|
|
|
}
|
2009-04-25 04:26:42 +05:30
|
|
|
}
|
2010-03-19 04:50:15 +05:30
|
|
|
|
|
|
|
rlim.rlim_cur = limit;
|
|
|
|
rlim.rlim_max = limit;
|
2009-04-25 04:26:42 +05:30
|
|
|
if (setrlimit (resource, &rlim) != 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return LOGIN_ERROR_RLIMIT;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
static int set_prio (const char *value)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2009-04-29 00:42:48 +05:30
|
|
|
long prio;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2009-04-29 00:42:48 +05:30
|
|
|
if ( (getlong (value, &prio) == 0)
|
|
|
|
|| (prio != (int) prio)) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return 0;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2009-04-29 00:42:48 +05:30
|
|
|
if (setpriority (PRIO_PROCESS, 0, (int) prio) != 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return LOGIN_ERROR_RLIMIT;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
static int set_umask (const char *value)
|
2007-10-07 17:14:08 +05:30
|
|
|
{
|
2009-04-25 04:26:42 +05:30
|
|
|
unsigned long int mask;
|
2007-10-07 17:14:08 +05:30
|
|
|
|
2009-04-25 04:26:42 +05:30
|
|
|
if ( (getulong (value, &mask) == 0)
|
|
|
|
|| (mask != (mode_t) mask)) {
|
2007-10-07 17:14:08 +05:30
|
|
|
return 0;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2009-04-25 04:26:42 +05:30
|
|
|
|
|
|
|
(void) umask ((mode_t) mask);
|
2007-10-07 17:14:08 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/* Counts the number of user logins and check against the limit */
|
2007-10-07 17:15:23 +05:30
|
|
|
static int check_logins (const char *name, const char *maxlogins)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2009-04-29 00:42:48 +05:30
|
|
|
#ifdef USE_UTMPX
|
2007-10-07 17:15:23 +05:30
|
|
|
struct utmpx *ut;
|
2009-04-29 00:42:48 +05:30
|
|
|
#else /* !USE_UTMPX */
|
2007-10-07 17:14:02 +05:30
|
|
|
struct utmp *ut;
|
2009-04-29 00:42:48 +05:30
|
|
|
#endif /* !USE_UTMPX */
|
2009-04-25 04:26:42 +05:30
|
|
|
unsigned long limit, count;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2009-04-25 04:26:42 +05:30
|
|
|
if (getulong (maxlogins, &limit) == 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
return 0;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-16 03:29:41 +05:30
|
|
|
if (0 == limit) { /* maximum 0 logins ? */
|
2007-10-07 17:15:23 +05:30
|
|
|
SYSLOG ((LOG_WARN, "No logins allowed for `%s'\n", name));
|
2007-10-07 17:14:02 +05:30
|
|
|
return LOGIN_ERROR_LOGIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = 0;
|
2009-04-29 00:42:48 +05:30
|
|
|
#ifdef USE_UTMPX
|
2007-10-07 17:15:23 +05:30
|
|
|
setutxent ();
|
2010-03-20 15:59:27 +05:30
|
|
|
while ((ut = getutxent ()))
|
2009-04-29 00:42:48 +05:30
|
|
|
#else /* !USE_UTMPX */
|
2007-10-07 17:15:23 +05:30
|
|
|
setutent ();
|
2010-03-20 15:59:27 +05:30
|
|
|
while ((ut = getutent ()))
|
2009-04-29 00:42:48 +05:30
|
|
|
#endif /* !USE_UTMPX */
|
2010-03-20 15:59:27 +05:30
|
|
|
{
|
2008-06-16 03:29:41 +05:30
|
|
|
if (USER_PROCESS != ut->ut_type) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
|
|
|
if ('\0' == ut->ut_user[0]) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
|
|
|
if (strncmp (name, ut->ut_user, sizeof (ut->ut_user)) != 0) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
|
|
|
count++;
|
|
|
|
if (count > limit) {
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2009-04-29 00:42:48 +05:30
|
|
|
#ifdef USE_UTMPX
|
2007-10-07 17:15:23 +05:30
|
|
|
endutxent ();
|
2009-04-29 00:42:48 +05:30
|
|
|
#else /* !USE_UTMPX */
|
2007-10-07 17:15:23 +05:30
|
|
|
endutent ();
|
2009-04-29 00:42:48 +05:30
|
|
|
#endif /* !USE_UTMPX */
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* This is called after setutmp(), so the number of logins counted
|
|
|
|
* includes the user who is currently trying to log in.
|
|
|
|
*/
|
|
|
|
if (count > limit) {
|
2009-09-08 01:20:00 +05:30
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"Too many logins (max %lu) for %s\n",
|
|
|
|
limit, name));
|
2007-10-07 17:14:02 +05:30
|
|
|
return LOGIN_ERROR_LOGIN;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-13 20:41:40 +05:30
|
|
|
/* Function setup_user_limits - checks/set limits for the current login
|
2007-10-07 17:14:02 +05:30
|
|
|
* Original idea from Joel Katz's lshell. Ported to shadow-login
|
|
|
|
* by Cristian Gafton - gafton@sorosis.ro
|
|
|
|
*
|
|
|
|
* We are passed a string of the form ('BASH' constants for ulimit)
|
2007-10-28 01:15:21 +05:30
|
|
|
* [Aa][Cc][Dd][Ff][Mm][Nn][Rr][Ss][Tt][Uu][Ll][Pp][Ii][Oo]
|
2007-10-07 17:14:02 +05:30
|
|
|
* (eg. 'C2F256D2048N5' or 'C2 F256 D2048 N5')
|
|
|
|
* where:
|
|
|
|
* [Aa]: a = RLIMIT_AS max address space (KB)
|
|
|
|
* [Cc]: c = RLIMIT_CORE max core file size (KB)
|
|
|
|
* [Dd]: d = RLIMIT_DATA max data size (KB)
|
2007-10-07 17:14:08 +05:30
|
|
|
* [Ff]: f = RLIMIT_FSIZE max file size (KB)
|
2011-11-07 00:09:47 +05:30
|
|
|
* [Ii]: i = RLIMIT_NICE max nice value (0..39 translates to 20..-19)
|
|
|
|
* [Kk]: k = file creation masK (umask)
|
|
|
|
* [Ll]: l = max number of logins for this user
|
2007-10-07 17:14:02 +05:30
|
|
|
* [Mm]: m = RLIMIT_MEMLOCK max locked-in-memory address space (KB)
|
|
|
|
* [Nn]: n = RLIMIT_NOFILE max number of open files
|
2011-11-07 00:09:47 +05:30
|
|
|
* [Oo]: o = RLIMIT_RTPRIO max real time priority (linux/sched.h 0..MAX_RT_PRIO)
|
|
|
|
* [Pp]: p = process priority -20..20 (negative = high, positive = low)
|
2007-10-07 17:14:02 +05:30
|
|
|
* [Rr]: r = RLIMIT_RSS max resident set size (KB)
|
|
|
|
* [Ss]: s = RLIMIT_STACK max stack size (KB)
|
|
|
|
* [Tt]: t = RLIMIT_CPU max CPU time (MIN)
|
|
|
|
* [Uu]: u = RLIMIT_NPROC max number of processes
|
|
|
|
*
|
2010-03-19 04:50:24 +05:30
|
|
|
* NOTE: Remember to extend the "no-limits" string below when adding a new
|
|
|
|
* limit...
|
2010-03-19 04:50:15 +05:30
|
|
|
*
|
2007-10-07 17:14:02 +05:30
|
|
|
* Return value:
|
|
|
|
* 0 = okay, of course
|
|
|
|
* LOGIN_ERROR_RLIMIT = error setting some RLIMIT
|
|
|
|
* LOGIN_ERROR_LOGIN = error - too many logins for this user
|
|
|
|
*
|
|
|
|
* buf - the limits string
|
|
|
|
* name - the username
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
static int do_user_limits (const char *buf, const char *name)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
const char *pp;
|
|
|
|
int retval = 0;
|
2008-06-16 03:29:41 +05:30
|
|
|
bool reported = false;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
pp = buf;
|
2010-03-19 04:50:24 +05:30
|
|
|
/* Skip leading whitespace. */
|
|
|
|
while ((' ' == *pp) || ('\t' == *pp)) {
|
|
|
|
pp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The special limit string "-" results in no limit for all known
|
|
|
|
* limits.
|
|
|
|
* We achieve that by parsing a full limit string, parts of it
|
|
|
|
* being ignored if a limit type is not known to the system.
|
|
|
|
* Though, there will be complaining for unknown limit types.
|
|
|
|
*/
|
|
|
|
if (strcmp (pp, "-") == 0) {
|
2010-03-19 04:50:15 +05:30
|
|
|
/* Remember to extend this, too, when adding new limits!
|
2010-03-19 04:50:24 +05:30
|
|
|
* Oh... but "unlimited" does not make sense for umask,
|
2011-11-07 00:09:47 +05:30
|
|
|
* or does it? (K-)
|
2010-03-19 04:50:24 +05:30
|
|
|
*/
|
2011-11-07 00:09:47 +05:30
|
|
|
pp = "A- C- D- F- I- L- M- N- O- P- R- S- T- U-";
|
2010-03-19 04:50:15 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-16 03:29:41 +05:30
|
|
|
while ('\0' != *pp) {
|
2007-10-07 17:15:23 +05:30
|
|
|
switch (*pp++) {
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef RLIMIT_AS
|
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
/* RLIMIT_AS - max address space (KB) */
|
2007-10-07 17:15:23 +05:30
|
|
|
retval |= setrlimit_value (RLIMIT_AS, pp, 1024);
|
2007-10-07 17:17:01 +05:30
|
|
|
break;
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2011-11-07 00:09:47 +05:30
|
|
|
#ifdef RLIMIT_CORE
|
|
|
|
case 'c':
|
|
|
|
case 'C':
|
|
|
|
/* RLIMIT_CORE - max core file size (KB) */
|
|
|
|
retval |= setrlimit_value (RLIMIT_CORE, pp, 1024);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_DATA
|
|
|
|
case 'd':
|
|
|
|
case 'D':
|
|
|
|
/* RLIMIT_DATA - max data size (KB) */
|
2007-10-07 17:15:23 +05:30
|
|
|
retval |= setrlimit_value (RLIMIT_DATA, pp, 1024);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_FSIZE
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
/* RLIMIT_FSIZE - Maximum filesize (KB) */
|
2007-10-07 17:15:23 +05:30
|
|
|
retval |= setrlimit_value (RLIMIT_FSIZE, pp, 1024);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
#endif
|
2011-11-07 00:09:47 +05:30
|
|
|
#ifdef RLIMIT_NICE
|
|
|
|
case 'i':
|
|
|
|
case 'I':
|
|
|
|
/* RLIMIT_NICE - max scheduling priority (0..39) */
|
|
|
|
retval |= setrlimit_value (RLIMIT_NICE, pp, 1);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
#endif
|
2011-11-07 00:09:47 +05:30
|
|
|
case 'k':
|
|
|
|
case 'K':
|
|
|
|
retval |= set_umask (pp);
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
case 'L':
|
|
|
|
/* LIMIT the number of concurrent logins */
|
|
|
|
retval |= check_logins (name, pp);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
#ifdef RLIMIT_MEMLOCK
|
|
|
|
case 'm':
|
|
|
|
case 'M':
|
2007-10-07 17:15:23 +05:30
|
|
|
/* RLIMIT_MEMLOCK - max locked-in-memory address space (KB) */
|
2007-10-07 17:16:07 +05:30
|
|
|
retval |= setrlimit_value (RLIMIT_MEMLOCK, pp, 1024);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_NOFILE
|
|
|
|
case 'n':
|
|
|
|
case 'N':
|
|
|
|
/* RLIMIT_NOFILE - max number of open files */
|
2007-10-07 17:15:23 +05:30
|
|
|
retval |= setrlimit_value (RLIMIT_NOFILE, pp, 1);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
#endif
|
2011-11-07 00:09:47 +05:30
|
|
|
#ifdef RLIMIT_RTPRIO
|
|
|
|
case 'o':
|
|
|
|
case 'O':
|
|
|
|
/* RLIMIT_RTPRIO - max real time priority (0..MAX_RT_PRIO) */
|
|
|
|
retval |= setrlimit_value (RLIMIT_RTPRIO, pp, 1);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case 'p':
|
|
|
|
case 'P':
|
|
|
|
retval |= set_prio (pp);
|
|
|
|
break;
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef RLIMIT_RSS
|
|
|
|
case 'r':
|
|
|
|
case 'R':
|
|
|
|
/* RLIMIT_RSS - max resident set size (KB) */
|
2007-10-07 17:15:23 +05:30
|
|
|
retval |= setrlimit_value (RLIMIT_RSS, pp, 1024);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef RLIMIT_STACK
|
|
|
|
case 's':
|
|
|
|
case 'S':
|
|
|
|
/* RLIMIT_STACK - max stack size (KB) */
|
2007-10-07 17:15:23 +05:30
|
|
|
retval |= setrlimit_value (RLIMIT_STACK, pp, 1024);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2007-10-28 01:15:21 +05:30
|
|
|
#endif
|
2011-11-07 00:09:47 +05:30
|
|
|
#ifdef RLIMIT_CPU
|
|
|
|
case 't':
|
|
|
|
case 'T':
|
|
|
|
/* RLIMIT_CPU - max CPU time (MIN) */
|
|
|
|
retval |= setrlimit_value (RLIMIT_CPU, pp, 60);
|
2007-10-28 01:15:21 +05:30
|
|
|
break;
|
|
|
|
#endif
|
2011-11-07 00:09:47 +05:30
|
|
|
#ifdef RLIMIT_NPROC
|
|
|
|
case 'u':
|
|
|
|
case 'U':
|
|
|
|
/* RLIMIT_NPROC - max number of processes */
|
|
|
|
retval |= setrlimit_value (RLIMIT_NPROC, pp, 1);
|
2007-10-28 01:15:21 +05:30
|
|
|
break;
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2008-06-16 03:29:41 +05:30
|
|
|
default:
|
|
|
|
/* Only report invalid strings once */
|
2010-03-19 04:50:24 +05:30
|
|
|
/* Note: A string can be invalid just because a
|
|
|
|
* specific (theoretically valid) setting is not
|
|
|
|
* supported by this build.
|
|
|
|
* It is just a warning in syslog anyway. The line
|
|
|
|
* is still processed
|
|
|
|
*/
|
2008-06-16 03:29:41 +05:30
|
|
|
if (!reported) {
|
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"Invalid limit string: '%s'",
|
|
|
|
pp-1));
|
|
|
|
reported = true;
|
|
|
|
retval |= LOGIN_ERROR_RLIMIT;
|
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
}
|
2010-03-19 04:50:24 +05:30
|
|
|
/* After parsing one limit setting (or just complaining
|
|
|
|
* about it), one still needs to skip its argument to
|
|
|
|
* prevent a bogus warning on trying to parse that as
|
|
|
|
* limit specification.
|
|
|
|
* So, let's skip all digits, "-" and our limited set of
|
|
|
|
* whitespace.
|
|
|
|
*/
|
|
|
|
while ( isdigit (*pp)
|
|
|
|
|| ('-' == *pp)
|
|
|
|
|| (' ' == *pp)
|
|
|
|
|| ('\t' ==*pp)) {
|
|
|
|
pp++;
|
2010-03-19 04:50:15 +05:30
|
|
|
}
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2010-03-19 04:50:15 +05:30
|
|
|
/* Check if user uname is in the group gname.
|
|
|
|
* Can I be sure that gr_mem contains no UID as string?
|
|
|
|
* Returns true when user is in the group, false when not.
|
2010-03-19 04:50:24 +05:30
|
|
|
* Any error is treated as false.
|
2010-03-19 04:50:15 +05:30
|
|
|
*/
|
|
|
|
static bool user_in_group (const char *uname, const char *gname)
|
|
|
|
{
|
|
|
|
struct group *groupdata;
|
2010-03-19 04:50:24 +05:30
|
|
|
|
2021-08-18 23:36:02 +05:30
|
|
|
if (uname == NULL || gname == NULL) {
|
2010-03-19 04:50:15 +05:30
|
|
|
return false;
|
|
|
|
}
|
2010-03-19 04:50:24 +05:30
|
|
|
|
2010-03-19 04:50:15 +05:30
|
|
|
/* We are not claiming to be re-entrant!
|
|
|
|
* In case of paranoia or a multithreaded login program,
|
|
|
|
* one needs to add some mess for getgrnam_r. */
|
2010-03-19 04:50:24 +05:30
|
|
|
groupdata = getgrnam (gname);
|
|
|
|
if (NULL == groupdata) {
|
|
|
|
SYSLOG ((LOG_WARN, "Nonexisting group `%s' in limits file.",
|
|
|
|
gname));
|
2010-03-19 04:50:15 +05:30
|
|
|
return false;
|
|
|
|
}
|
2010-03-19 04:50:24 +05:30
|
|
|
|
|
|
|
return is_on_list (groupdata->gr_mem, uname);
|
2010-03-19 04:50:15 +05:30
|
|
|
}
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
static int setup_user_limits (const char *uname)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
|
|
|
FILE *fil;
|
|
|
|
char buf[1024];
|
|
|
|
char name[1024];
|
|
|
|
char limits[1024];
|
|
|
|
char deflimits[1024];
|
|
|
|
char tempbuf[1024];
|
|
|
|
|
|
|
|
/* init things */
|
2007-10-07 17:15:23 +05:30
|
|
|
memzero (buf, sizeof (buf));
|
|
|
|
memzero (name, sizeof (name));
|
|
|
|
memzero (limits, sizeof (limits));
|
|
|
|
memzero (deflimits, sizeof (deflimits));
|
|
|
|
memzero (tempbuf, sizeof (tempbuf));
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/* start the checks */
|
2007-10-07 17:15:23 +05:30
|
|
|
fil = fopen (LIMITS_FILE, "r");
|
2007-10-07 17:14:02 +05:30
|
|
|
if (fil == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* The limits file have the following format:
|
|
|
|
* - '#' (comment) chars only as first chars on a line;
|
2010-03-19 04:50:24 +05:30
|
|
|
* - username must start on first column (or *, or @group)
|
|
|
|
*
|
|
|
|
* FIXME: A better (smarter) checking should be done
|
|
|
|
*/
|
2007-10-07 17:15:23 +05:30
|
|
|
while (fgets (buf, 1024, fil) != NULL) {
|
2008-06-16 03:29:41 +05:30
|
|
|
if (('#' == buf[0]) || ('\n' == buf[0])) {
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
memzero (tempbuf, sizeof (tempbuf));
|
2007-10-07 17:14:02 +05:30
|
|
|
/* a valid line should have a username, then spaces,
|
|
|
|
* then limits
|
|
|
|
* we allow the format:
|
|
|
|
* username L2 D2048 R4096
|
|
|
|
* where spaces={' ',\t}. Also, we reject invalid limits.
|
|
|
|
* Imposing a limit should be done with care, so a wrong
|
2010-03-19 04:50:24 +05:30
|
|
|
* entry means no care anyway :-).
|
|
|
|
*
|
|
|
|
* A '-' as a limits strings means no limits
|
|
|
|
*
|
|
|
|
* The username can also be:
|
|
|
|
* '*': the default limits (only the last is taken into
|
|
|
|
* account)
|
|
|
|
* @group: the limit applies to the members of the group
|
|
|
|
*
|
|
|
|
* To clarify: The first entry with matching user name rules,
|
|
|
|
* everything after it is ignored. If there is no user entry,
|
|
|
|
* the last encountered entry for a matching group rules.
|
|
|
|
* If there is no matching group entry, the default limits rule.
|
|
|
|
*/
|
2011-11-07 00:09:47 +05:30
|
|
|
if (sscanf (buf, "%s%[ACDFIKLMNOPRSTUacdfiklmnoprstu0-9 \t-]",
|
2010-03-19 04:50:24 +05:30
|
|
|
name, tempbuf) == 2) {
|
2007-10-07 17:15:23 +05:30
|
|
|
if (strcmp (name, uname) == 0) {
|
|
|
|
strcpy (limits, tempbuf);
|
2007-10-07 17:14:02 +05:30
|
|
|
break;
|
2007-10-07 17:15:23 +05:30
|
|
|
} else if (strcmp (name, "*") == 0) {
|
|
|
|
strcpy (deflimits, tempbuf);
|
2010-03-19 04:50:15 +05:30
|
|
|
} else if (name[0] == '@') {
|
2010-03-19 04:50:24 +05:30
|
|
|
/* If the user is in the group, the group
|
|
|
|
* limits apply unless later a line for
|
|
|
|
* the specific user is found.
|
|
|
|
*/
|
|
|
|
if (user_in_group (uname, name+1)) {
|
2010-03-19 04:50:15 +05:30
|
|
|
strcpy (limits, tempbuf);
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-06-16 03:29:41 +05:30
|
|
|
(void) fclose (fil);
|
2007-10-07 17:14:02 +05:30
|
|
|
if (limits[0] == '\0') {
|
|
|
|
/* no user specific limits */
|
2008-06-16 03:29:41 +05:30
|
|
|
if (deflimits[0] == '\0') { /* no default limits */
|
2007-10-07 17:14:02 +05:30
|
|
|
return 0;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
strcpy (limits, deflimits); /* use the default limits */
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
return do_user_limits (limits, uname);
|
2007-10-07 17:14:02 +05:30
|
|
|
}
|
2007-10-07 17:15:23 +05:30
|
|
|
#endif /* LIMITS */
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2007-10-07 17:14:08 +05:30
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
static void setup_usergroups (const struct passwd *info)
|
2007-10-07 17:14:08 +05:30
|
|
|
{
|
|
|
|
const struct group *grp;
|
|
|
|
|
|
|
|
/*
|
2007-10-07 17:16:25 +05:30
|
|
|
* if not root, and UID == GID, and username is the same as primary
|
2007-10-07 17:14:08 +05:30
|
|
|
* group name, set umask group bits to be the same as owner bits
|
|
|
|
* (examples: 022 -> 002, 077 -> 007).
|
|
|
|
*/
|
2008-06-16 03:29:41 +05:30
|
|
|
if ((0 != info->pw_uid) && (info->pw_uid == info->pw_gid)) {
|
* lib/prototypes.h, configure.in, libmisc/Makefile.am,
libmisc/xgetXXbyYY.c, libmisc/xgetpwnam.c, libmisc/xgetpwuid.c,
libmisc/xgetgrnam.c, libmisc/xgetgrgid.c, libmisc/xgetspnam.c:
Added functions xgetpwnam(), xgetpwuid(), xgetgrnam(),
xgetgrgid(), and xgetspnam(). They allocate memory for the
returned structure and are more robust to successive calls. They
are implemented with the libc's getxxyyy_r() functions if
available.
* libmisc/limits.c, libmisc/entry.c, libmisc/chowntty.c,
libmisc/addgrps.c, libmisc/myname.c, libmisc/rlogin.c,
libmisc/pwdcheck.c, src/newgrp.c, src/login_nopam.c,
src/userdel.c, src/lastlog.c, src/grpck.c, src/gpasswd.c,
src/newusers.c, src/chpasswd.c, src/chfn.c, src/groupmems.c,
src/usermod.c, src/expiry.c, src/groupdel.c, src/chgpasswd.c,
src/su.c, src/useradd.c, src/groupmod.c, src/passwd.c, src/pwck.c,
src/groupadd.c, src/chage.c, src/login.c, src/suauth.c,
src/faillog.c, src/groups.c, src/chsh.c, src/id.c: Review all the
usage of one of the getpwnam(), getpwuid(), getgrnam(),
getgrgid(), and getspnam() functions. It was noticed on
http://bugs.debian.org/341230 that chfn and chsh use a passwd
structure after calling a pam function, which result in using
information from the passwd structure requested by pam, not the
original one. It is much easier to use the new xget... functions
to avoid these issues. I've checked which call to the original
get... functions could be left (reducing the scope of the
structure if possible), and I've left comments to ease future
reviews (e.g. /* local, no need for xgetpwnam */).
Note: the getpwent/getgrent calls should probably be checked also.
* src/groupdel.c, src/expiry.c: Fix typos in comments.
* src/groupmod.c: Re-indent.
* libmisc/Makefile.am, lib/groupmem.c, lib/groupio.c, lib/pwmem.c,
lib/pwio.c, lib/shadowmem.c, lib/shadowio.c: Move the __<xx>_dup
functions (used by the xget... functions) from the <xx>io.c files
to the new <xx>mem.c files. This avoid linking some utils against
the SELinux library.
2007-11-19 04:45:26 +05:30
|
|
|
/* local, no need for xgetgrgid */
|
2007-10-07 17:15:23 +05:30
|
|
|
grp = getgrgid (info->pw_gid);
|
2008-06-16 03:29:41 +05:30
|
|
|
if ( (NULL != grp)
|
|
|
|
&& (strcmp (info->pw_name, grp->gr_name) == 0)) {
|
2010-03-23 14:26:52 +05:30
|
|
|
mode_t tmpmask;
|
2009-04-25 04:26:42 +05:30
|
|
|
tmpmask = umask (0777);
|
|
|
|
tmpmask = (tmpmask & ~070) | ((tmpmask >> 3) & 070);
|
|
|
|
(void) umask (tmpmask);
|
2007-10-07 17:14:08 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
/*
|
|
|
|
* set the process nice, ulimit, and umask from the password file entry
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
void setup_limits (const struct passwd *info)
|
2007-10-07 17:14:02 +05:30
|
|
|
{
|
2007-10-07 17:15:23 +05:30
|
|
|
char *cp;
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2008-06-16 03:29:41 +05:30
|
|
|
if (getdef_bool ("USERGROUPS_ENAB")) {
|
2007-10-07 17:15:23 +05:30
|
|
|
setup_usergroups (info);
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* See if the GECOS field contains values for NICE, UMASK or ULIMIT.
|
|
|
|
* If this feature is enabled in /etc/login.defs, we make those
|
|
|
|
* values the defaults for this login session.
|
|
|
|
*/
|
|
|
|
|
2007-10-07 17:15:23 +05:30
|
|
|
if (getdef_bool ("QUOTAS_ENAB")) {
|
2007-10-07 17:14:02 +05:30
|
|
|
#ifdef LIMITS
|
2008-06-16 03:29:41 +05:30
|
|
|
if (info->pw_uid != 0) {
|
* libmisc/limits.c: Avoid implicit conversion of integer to
boolean.
* libmisc/basename.c: Avoid implicit conversion of pointer to
boolean.
* libmisc/basename.c, lib/prototypes.h (Basename): Return a
constant string.
* libmisc/basename.c, libmisc/obscure.c, lib/prototypes.h,
libmisc/xmalloc.c, libmisc/getdate.h, libmisc/system.c,
libmisc/getgr_nam_gid.c, libmisc/failure.c, libmisc/valid.c: Add
splint annotations.
* libmisc/chowndir.c: Avoid memory leak.
* libmisc/chowndir.c: Do not check *printf/*puts return value.
* libmisc/chowntty.c: Avoid implicit conversion between integer
types.
* libmisc/obscure.c: Return a bool when possible instead of int.
* libmisc/shell.c: Do not check *printf/*puts return value.
* libmisc/shell.c: Do not check execle return value.
* libmisc/setupenv.c: Avoid implicit conversion between integer
types.
* libmisc/xmalloc.c: size should not be zero to avoid returning
NULL pointers.
* libmisc/hushed.c: Do not check *printf/*puts return value.
* libmisc/system.c: Avoid implicit conversion of integer to
boolean. safe_system last argument is a boolean.
* libmisc/system.c: Check return value of dup2.
* libmisc/system.c: Do not check *printf/*puts return value.
* libmisc/system.c: Do not check execve return value.
* libmisc/salt.c: Do not check *printf/*puts return value.
* libmisc/loginprompt.c: Do not check gethostname return value.
* libmisc/find_new_gid.c, libmisc/find_new_uid.c: Do not check
gr_rewind/pw_rewind return value.
* libmisc/ttytype.c: Limit the number of parsed characters in the
sscanf format.
* libmisc/ttytype.c: Test if a type was really read.
* libmisc/sub.c: Do not check *printf/*puts return value.
* libmisc/sub.c: Avoid implicit conversion of integer to boolean.
* src/userdel.c: Fix typo in comment.
* src/userdel.c: Avoid implicit conversion of boolean to integer.
* src/userdel.c: safe_system last argument is a boolean.
* src/newusers.c: Avoid implicit conversion of boolean to integer.
* src/newusers.c: Avoid implicit conversion of integer to boolean.
* src/usermod.c: Add brackets.
* src/usermod.c: Avoid implicit conversion of characters or
integers to booleans.
* src/vipw.c: Avoid implicit conversion of integer to boolean.
* src/su.c: Avoid implicit conversion of integer to boolean.
* src/su.c: Add brackets.
* src/useradd.c: Avoid implicit conversion of characters or
integers to booleans.
2010-08-23 00:43:53 +05:30
|
|
|
if ((setup_user_limits (info->pw_name) & LOGIN_ERROR_LOGIN) != 0) {
|
2021-11-29 05:07:53 +05:30
|
|
|
(void) fputs (_("Too many logins.\n"), log_get_logfd());
|
2008-06-16 03:29:41 +05:30
|
|
|
(void) sleep (2); /* XXX: Should be FAIL_DELAY */
|
* lib/exitcodes.h: Define E_SUCCESS as EXIT_SUCCESS. Added FIXMEs.
* libmisc/chowntty.c, libmisc/rlogin.c, libmisc/sub.c,
src/newusers.c, libmisc/sulog.c, libmisc/system.c, src/logoutd.c,
src/groups.c, src/id.c, lib/encrypt.c, libmisc/audit_help.c,
libmisc/limits.c: Return EXIT_FAILURE instead of 1, and
EXIT_SUCCESS instead of 0.
* libmisc/audit_help.c: Replace an fprintf() by fputs().
* libmisc/audit_help.c: Remove documentation of the audit_logger
returned values. The function returns void.
* libmisc/system.c: Only return status if waitpid succeeded.
Return -1 otherwise.
2009-05-01 02:38:49 +05:30
|
|
|
exit (EXIT_FAILURE);
|
2007-10-07 17:15:23 +05:30
|
|
|
}
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
#endif
|
2007-10-07 17:16:07 +05:30
|
|
|
for (cp = info->pw_gecos; cp != NULL; cp = strchr (cp, ',')) {
|
2008-06-16 03:29:41 +05:30
|
|
|
if (',' == *cp) {
|
2007-10-07 17:14:02 +05:30
|
|
|
cp++;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
if (strncmp (cp, "pri=", 4) == 0) {
|
2009-04-25 04:26:42 +05:30
|
|
|
long int inc;
|
|
|
|
if ( (getlong (cp + 4, &inc) == 1)
|
|
|
|
&& (inc >= -20) && (inc <= 20)) {
|
2008-06-16 03:29:41 +05:30
|
|
|
errno = 0;
|
2009-04-25 04:26:42 +05:30
|
|
|
if ( (nice ((int) inc) != -1)
|
|
|
|
|| (0 != errno)) {
|
|
|
|
continue;
|
2008-06-16 03:29:41 +05:30
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
2009-04-25 04:26:42 +05:30
|
|
|
/* Failed to parse or failed to nice() */
|
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"Can't set the nice value for user %s",
|
|
|
|
info->pw_name));
|
|
|
|
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp (cp, "ulimit=", 7) == 0) {
|
2009-04-25 04:26:42 +05:30
|
|
|
long int blocks;
|
|
|
|
if ( (getlong (cp + 7, &blocks) == 0)
|
|
|
|
|| (blocks != (int) blocks)
|
|
|
|
|| (set_filesize_limit ((int) blocks) != 0)) {
|
2008-06-16 03:29:41 +05:30
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"Can't set the ulimit for user %s",
|
|
|
|
info->pw_name));
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (strncmp (cp, "umask=", 6) == 0) {
|
2009-04-25 04:26:42 +05:30
|
|
|
unsigned long int mask;
|
|
|
|
if ( (getulong (cp + 6, &mask) == 0)
|
|
|
|
|| (mask != (mode_t) mask)) {
|
|
|
|
SYSLOG ((LOG_WARN,
|
|
|
|
"Can't set umask value for user %s",
|
|
|
|
info->pw_name));
|
|
|
|
} else {
|
|
|
|
(void) umask ((mode_t) mask);
|
|
|
|
}
|
2007-10-07 17:14:02 +05:30
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-10-07 17:16:07 +05:30
|
|
|
|
2008-01-01 19:48:55 +05:30
|
|
|
#else /* !USE_PAM */
|
|
|
|
extern int errno; /* warning: ANSI C forbids an empty source file */
|
2007-10-07 17:16:07 +05:30
|
|
|
#endif /* !USE_PAM */
|
2008-04-27 06:10:09 +05:30
|
|
|
|