2 * pam_limits - impose resource limits when opening a user session
4 * 1.6 - modified for PLD (added process priority settings)
5 * by Marcin Korzonek <mkorz@shadow.eu.org>
6 * 1.5 - Elliot Lee's "max system logins patch"
7 * 1.4 - addressed bug in configuration file parser
8 * 1.3 - modified the configuration file format
9 * 1.2 - added 'debug' and 'conf=' arguments
10 * 1.1 - added @group support
11 * 1.0 - initial release - Linux ONLY
13 * See end for Copyright information
16 #if !defined(linux) && !defined(__linux)
17 #warning THIS CODE IS KNOWN TO WORK ONLY ON LINUX !!!
30 #include <sys/types.h>
32 #include <sys/resource.h>
36 #ifndef UT_USER /* some systems have ut_name instead of ut_user */
37 #define UT_USER ut_user
45 #define LINE_LENGTH 1024
47 #define LIMITS_DEF_USER 0 /* limit was set by an user entry */
48 #define LIMITS_DEF_GROUP 1 /* limit was set by a group entry */
49 #define LIMITS_DEF_ALLGROUP 2 /* limit was set by a group entry */
50 #define LIMITS_DEF_ALL 3 /* limit was set by an default entry */
51 #define LIMITS_DEF_DEFAULT 4 /* limit was set by an default entry */
52 #define LIMITS_DEF_NONE 5 /* this limit was not set yet */
54 static const char *limits_def_names[] = {
64 struct user_limits_struct {
73 int login_limit; /* the max logins limit */
74 int login_limit_def; /* which entry set the login limit */
75 int flag_numsyslogins; /* whether to limit logins only for a
76 specific user or to count all logins */
77 int priority; /* the priority to run user process with */
78 struct user_limits_struct limits[RLIM_NLIMITS];
79 const char *conf_file;
80 int utmp_after_pam_call;
81 char login_group[LINE_LENGTH];
84 #define LIMIT_LOGIN RLIM_NLIMITS+1
85 #define LIMIT_NUMSYSLOGINS RLIM_NLIMITS+2
87 #define LIMIT_PRI RLIM_NLIMITS+3
92 #define PAM_SM_SESSION
94 #include <security/pam_modules.h>
95 #include <security/_pam_macros.h>
96 #include <security/pam_modutil.h>
97 #include <security/pam_ext.h>
99 /* argument parsing */
101 #define PAM_DEBUG_ARG 0x0001
102 #define PAM_DO_SETREUID 0x0002
103 #define PAM_UTMP_EARLY 0x0004
105 /* Limits from globbed files. */
106 #define LIMITS_CONF_GLOB LIMITS_FILE_DIR
108 #define CONF_FILE (pl->conf_file != NULL)?pl->conf_file:LIMITS_FILE
111 _pam_parse (const pam_handle_t *pamh, int argc, const char **argv,
112 struct pam_limit_s *pl)
116 /* step through arguments */
117 for (ctrl=0; argc-- > 0; ++argv) {
119 /* generic options */
121 if (!strcmp(*argv,"debug")) {
122 ctrl |= PAM_DEBUG_ARG;
123 } else if (!strncmp(*argv,"conf=",5)) {
124 pl->conf_file = *argv+5;
125 } else if (!strncmp(*argv,"change_uid",10)) {
126 ctrl |= PAM_DO_SETREUID;
127 } else if (!strcmp(*argv,"utmp_early")) {
128 ctrl |= PAM_UTMP_EARLY;
130 pam_syslog(pamh, LOG_ERR, "unknown option: %s", *argv);
138 #define LIMITED_OK 0 /* limit setting appeared to work */
139 #define LIMIT_ERR 1 /* error setting a limit */
140 #define LOGIN_ERR 2 /* too many logins err */
142 /* Counts the number of user logins and check against the limit*/
144 check_logins (pam_handle_t *pamh, const char *name, int limit, int ctrl,
145 struct pam_limit_s *pl)
150 if (ctrl & PAM_DEBUG_ARG) {
151 pam_syslog(pamh, LOG_DEBUG,
152 "checking logins for '%s' (maximum of %d)", name, limit);
156 return 0; /* no limits imposed */
157 if (limit == 0) /* maximum 0 logins ? */ {
158 pam_syslog(pamh, LOG_WARNING, "No logins allowed for '%s'", name);
164 /* Because there is no definition about when an application
165 actually adds a utmp entry, some applications bizarrely do the
166 utmp call before the have PAM authenticate them to the system:
167 you're logged it, sort of...? Anyway, you can use the
168 "utmp_early" module argument in your PAM config file to make
169 allowances for this sort of problem. (There should be a PAM
170 standard for this, since if a module wants to actually map a
171 username then any early utmp entry will be for the unmapped
174 if (ctrl & PAM_UTMP_EARLY) {
180 while((ut = getutent())) {
182 if (ut->ut_type != USER_PROCESS) {
186 if (ut->UT_USER[0] == '\0') {
189 if (!pl->flag_numsyslogins) {
190 if (((pl->login_limit_def == LIMITS_DEF_USER)
191 || (pl->login_limit_def == LIMITS_DEF_GROUP)
192 || (pl->login_limit_def == LIMITS_DEF_DEFAULT))
193 && strncmp(name, ut->UT_USER, sizeof(ut->UT_USER)) != 0) {
196 if ((pl->login_limit_def == LIMITS_DEF_ALLGROUP)
197 && !pam_modutil_user_in_group_nam_nam(pamh, ut->UT_USER, pl->login_group)) {
201 if (++count > limit) {
208 pam_syslog(pamh, LOG_WARNING,
209 "Too many logins (max %d) for %s", limit, name);
211 pam_syslog(pamh, LOG_WARNING, "Too many system logins (max %d)", limit);
218 static int init_limits(struct pam_limit_s *pl)
221 int retval = PAM_SUCCESS;
225 for(i = 0; i < RLIM_NLIMITS; i++) {
226 int r = getrlimit(i, &pl->limits[i].limit);
228 pl->limits[i].supported = 0;
229 if (errno != EINVAL) {
230 retval = !PAM_SUCCESS;
233 pl->limits[i].supported = 1;
234 pl->limits[i].src_soft = LIMITS_DEF_NONE;
235 pl->limits[i].src_hard = LIMITS_DEF_NONE;
240 pl->priority = getpriority (PRIO_PROCESS, 0);
241 if (pl->priority == -1 && errno != 0)
242 retval = !PAM_SUCCESS;
243 pl->login_limit = -2;
244 pl->login_limit_def = LIMITS_DEF_NONE;
250 process_limit (const pam_handle_t *pamh, int source, const char *lim_type,
251 const char *lim_item, const char *lim_value,
252 int ctrl, struct pam_limit_s *pl)
257 rlim_t rlimit_value = 0;
259 const char *value_orig = lim_value;
261 if (ctrl & PAM_DEBUG_ARG)
262 pam_syslog(pamh, LOG_DEBUG, "%s: processing %s %s %s for %s",
263 __FUNCTION__, lim_type, lim_item, lim_value,
264 limits_def_names[source]);
266 if (strcmp(lim_item, "cpu") == 0)
267 limit_item = RLIMIT_CPU;
268 else if (strcmp(lim_item, "fsize") == 0)
269 limit_item = RLIMIT_FSIZE;
270 else if (strcmp(lim_item, "data") == 0)
271 limit_item = RLIMIT_DATA;
272 else if (strcmp(lim_item, "stack") == 0)
273 limit_item = RLIMIT_STACK;
274 else if (strcmp(lim_item, "core") == 0)
275 limit_item = RLIMIT_CORE;
276 else if (strcmp(lim_item, "rss") == 0)
277 limit_item = RLIMIT_RSS;
278 else if (strcmp(lim_item, "nproc") == 0)
279 limit_item = RLIMIT_NPROC;
280 else if (strcmp(lim_item, "nofile") == 0)
281 limit_item = RLIMIT_NOFILE;
282 else if (strcmp(lim_item, "memlock") == 0)
283 limit_item = RLIMIT_MEMLOCK;
285 else if (strcmp(lim_item, "as") == 0)
286 limit_item = RLIMIT_AS;
289 else if (strcmp(lim_item, "locks") == 0)
290 limit_item = RLIMIT_LOCKS;
292 #ifdef RLIMIT_SIGPENDING
293 else if (strcmp(lim_item, "sigpending") == 0)
294 limit_item = RLIMIT_SIGPENDING;
296 #ifdef RLIMIT_MSGQUEUE
297 else if (strcmp(lim_item, "msgqueue") == 0)
298 limit_item = RLIMIT_MSGQUEUE;
301 else if (strcmp(lim_item, "nice") == 0)
302 limit_item = RLIMIT_NICE;
305 else if (strcmp(lim_item, "rtprio") == 0)
306 limit_item = RLIMIT_RTPRIO;
308 else if (strcmp(lim_item, "maxlogins") == 0) {
309 limit_item = LIMIT_LOGIN;
310 pl->flag_numsyslogins = 0;
311 } else if (strcmp(lim_item, "maxsyslogins") == 0) {
312 limit_item = LIMIT_NUMSYSLOGINS;
313 pl->flag_numsyslogins = 1;
314 } else if (strcmp(lim_item, "priority") == 0) {
315 limit_item = LIMIT_PRI;
317 pam_syslog(pamh, LOG_DEBUG, "unknown limit item '%s'", lim_item);
321 if (strcmp(lim_type,"soft")==0)
322 limit_type=LIMIT_SOFT;
323 else if (strcmp(lim_type, "hard")==0)
324 limit_type=LIMIT_HARD;
325 else if (strcmp(lim_type,"-")==0)
326 limit_type=LIMIT_SOFT | LIMIT_HARD;
327 else if (limit_item != LIMIT_LOGIN && limit_item != LIMIT_NUMSYSLOGINS) {
328 pam_syslog(pamh, LOG_DEBUG, "unknown limit type '%s'", lim_type);
331 if (limit_item != LIMIT_PRI
333 && limit_item != RLIMIT_NICE
335 && (strcmp(lim_value, "-1") == 0
336 || strcmp(lim_value, "-") == 0 || strcmp(lim_value, "unlimited") == 0
337 || strcmp(lim_value, "infinity") == 0)) {
339 rlimit_value = RLIM_INFINITY;
340 } else if (limit_item == LIMIT_PRI || limit_item == LIMIT_LOGIN ||
342 limit_item == RLIMIT_NICE ||
344 limit_item == LIMIT_NUMSYSLOGINS) {
346 temp = strtol (lim_value, &endptr, 10);
347 temp = temp < INT_MAX ? temp : INT_MAX;
348 int_value = temp > INT_MIN ? temp : INT_MIN;
349 if (int_value == 0 && value_orig == endptr) {
350 pam_syslog(pamh, LOG_DEBUG,
351 "wrong limit value '%s' for limit type '%s'",
352 lim_value, lim_type);
356 #ifdef __USE_FILE_OFFSET64
357 rlimit_value = strtoull (lim_value, &endptr, 10);
359 rlimit_value = strtoul (lim_value, &endptr, 10);
361 if (rlimit_value == 0 && value_orig == endptr) {
362 pam_syslog(pamh, LOG_DEBUG,
363 "wrong limit value '%s' for limit type '%s'",
364 lim_value, lim_type);
369 /* one more special case when limiting logins */
370 if ((source == LIMITS_DEF_ALL || source == LIMITS_DEF_ALLGROUP)
371 && (limit_item != LIMIT_LOGIN)) {
372 if (ctrl & PAM_DEBUG_ARG)
373 pam_syslog(pamh, LOG_DEBUG,
374 "'%%' domain valid for maxlogins type only");
380 if (rlimit_value != RLIM_INFINITY)
382 if (rlimit_value >= RLIM_INFINITY/60)
383 rlimit_value = RLIM_INFINITY;
397 if (rlimit_value != RLIM_INFINITY)
399 if (rlimit_value >= RLIM_INFINITY/1024)
400 rlimit_value = RLIM_INFINITY;
402 rlimit_value *= 1024;
411 rlimit_value = 20 - int_value;
416 if ( (limit_item != LIMIT_LOGIN)
417 && (limit_item != LIMIT_NUMSYSLOGINS)
418 && (limit_item != LIMIT_PRI) ) {
419 if (limit_type & LIMIT_SOFT) {
420 if (pl->limits[limit_item].src_soft < source) {
423 pl->limits[limit_item].limit.rlim_cur = rlimit_value;
424 pl->limits[limit_item].src_soft = source;
427 if (limit_type & LIMIT_HARD) {
428 if (pl->limits[limit_item].src_hard < source) {
431 pl->limits[limit_item].limit.rlim_max = rlimit_value;
432 pl->limits[limit_item].src_hard = source;
436 /* recent kernels support negative priority limits (=raise priority) */
438 if (limit_item == LIMIT_PRI) {
439 pl->priority = int_value;
441 if (pl->login_limit_def < source) {
444 pl->login_limit = int_value;
445 pl->login_limit_def = source;
452 static int parse_config_file(pam_handle_t *pamh, const char *uname, int ctrl,
453 struct pam_limit_s *pl)
456 char buf[LINE_LENGTH];
458 /* check for the LIMITS_FILE */
459 if (ctrl & PAM_DEBUG_ARG)
460 pam_syslog(pamh, LOG_DEBUG, "reading settings from '%s'", CONF_FILE);
461 fil = fopen(CONF_FILE, "r");
463 pam_syslog (pamh, LOG_WARNING,
464 "cannot read settings from %s: %m", CONF_FILE);
465 return PAM_SERVICE_ERR;
469 while (fgets(buf, LINE_LENGTH, fil) != NULL) {
470 char domain[LINE_LENGTH];
471 char ltype[LINE_LENGTH];
472 char item[LINE_LENGTH];
473 char value[LINE_LENGTH];
479 /* skip the leading white space */
480 while (*line && isspace(*line))
483 /* Rip off the comments */
484 tptr = strchr(line,'#');
487 /* Rip off the newline char */
488 tptr = strchr(line,'\n');
491 /* Anything left ? */
495 domain[0] = ltype[0] = item[0] = value[0] = '\0';
497 i = sscanf(line,"%s%s%s%s", domain, ltype, item, value);
498 D(("scanned line[%d]: domain[%s], ltype[%s], item[%s], value[%s]",
499 i, domain, ltype, item, value));
501 for(j=0; j < strlen(ltype); j++)
502 ltype[j]=tolower(ltype[j]);
504 if (i == 4) { /* a complete line */
505 for(j=0; j < strlen(item); j++)
506 item[j]=tolower(item[j]);
507 for(j=0; j < strlen(value); j++)
508 value[j]=tolower(value[j]);
510 if (strcmp(uname, domain) == 0) /* this user have a limit */
511 process_limit(pamh, LIMITS_DEF_USER, ltype, item, value, ctrl, pl);
512 else if (domain[0]=='@') {
513 if (ctrl & PAM_DEBUG_ARG) {
514 pam_syslog(pamh, LOG_DEBUG,
515 "checking if %s is in group %s",
518 if (pam_modutil_user_in_group_nam_nam(pamh, uname, domain+1))
519 process_limit(pamh, LIMITS_DEF_GROUP, ltype, item, value, ctrl,
521 } else if (domain[0]=='%') {
522 if (ctrl & PAM_DEBUG_ARG) {
523 pam_syslog(pamh, LOG_DEBUG,
524 "checking if %s is in group %s",
527 if (strcmp(domain,"%") == 0)
528 process_limit(pamh, LIMITS_DEF_ALL, ltype, item, value, ctrl,
530 else if (pam_modutil_user_in_group_nam_nam(pamh, uname, domain+1)) {
531 strcpy(pl->login_group, domain+1);
532 process_limit(pamh, LIMITS_DEF_ALLGROUP, ltype, item, value, ctrl,
535 } else if (strcmp(domain, "*") == 0)
536 process_limit(pamh, LIMITS_DEF_DEFAULT, ltype, item, value, ctrl,
538 } else if (i == 2 && ltype[0] == '-') { /* Probably a no-limit line */
539 if (strcmp(uname, domain) == 0) {
540 if (ctrl & PAM_DEBUG_ARG) {
541 pam_syslog(pamh, LOG_DEBUG, "no limits for '%s'", uname);
545 } else if (domain[0] == '@' && pam_modutil_user_in_group_nam_nam(pamh, uname, domain+1)) {
546 if (ctrl & PAM_DEBUG_ARG) {
547 pam_syslog(pamh, LOG_DEBUG,
548 "no limits for '%s' in group '%s'",
555 pam_syslog(pamh, LOG_WARNING, "invalid line '%s' - skipped", line);
562 static int setup_limits(pam_handle_t *pamh,
563 const char *uname, uid_t uid, int ctrl,
564 struct pam_limit_s *pl)
568 int retval = LIMITED_OK;
570 for (i=0, status=LIMITED_OK; i<RLIM_NLIMITS; i++) {
571 if (!pl->limits[i].supported) {
572 /* skip it if its not known to the system */
575 if (pl->limits[i].src_soft == LIMITS_DEF_NONE &&
576 pl->limits[i].src_hard == LIMITS_DEF_NONE) {
577 /* skip it if its not initialized */
580 if (pl->limits[i].limit.rlim_cur > pl->limits[i].limit.rlim_max)
581 pl->limits[i].limit.rlim_cur = pl->limits[i].limit.rlim_max;
582 status |= setrlimit(i, &pl->limits[i].limit);
589 status = setpriority(PRIO_PROCESS, 0, pl->priority);
595 D(("skip login limit check for uid=0"));
596 } else if (pl->login_limit > 0) {
597 if (check_logins(pamh, uname, pl->login_limit, ctrl, pl) == LOGIN_ERR) {
600 } else if (pl->login_limit == 0) {
607 /* now the session stuff */
609 pam_sm_open_session (pam_handle_t *pamh, int flags UNUSED,
610 int argc, const char **argv)
618 struct pam_limit_s plstruct;
619 struct pam_limit_s *pl = &plstruct;
621 const char *oldlocale;
625 memset(pl, 0, sizeof(*pl));
626 memset(&globbuf, 0, sizeof(globbuf));
628 ctrl = _pam_parse(pamh, argc, argv, pl);
629 retval = pam_get_item( pamh, PAM_USER, (void*) &user_name );
630 if ( user_name == NULL || retval != PAM_SUCCESS ) {
631 pam_syslog(pamh, LOG_CRIT, "open_session - error recovering username");
632 return PAM_SESSION_ERR;
635 pwd = pam_modutil_getpwnam(pamh, user_name);
637 if (ctrl & PAM_DEBUG_ARG)
638 pam_syslog(pamh, LOG_WARNING,
639 "open_session username '%s' does not exist", user_name);
640 return PAM_USER_UNKNOWN;
643 retval = init_limits(pl);
644 if (retval != PAM_SUCCESS) {
645 pam_syslog(pamh, LOG_WARNING, "cannot initialize");
649 retval = parse_config_file(pamh, pwd->pw_name, ctrl, pl);
650 if (retval == PAM_IGNORE) {
651 D(("the configuration file ('%s') has an applicable '<domain> -' entry", CONF_FILE));
654 if (retval != PAM_SUCCESS || pl->conf_file != NULL)
655 /* skip reading limits.d if config file explicitely specified */
658 /* Read subsequent *.conf files, if they exist. */
660 /* set the LC_COLLATE so the sorting order doesn't depend
663 oldlocale = setlocale(LC_COLLATE, "C");
664 glob_rc = glob(LIMITS_CONF_GLOB, GLOB_ERR, NULL, &globbuf);
666 if (oldlocale != NULL)
667 setlocale (LC_COLLATE, oldlocale);
670 /* Parse the *.conf files. */
671 for (i = 0; globbuf.gl_pathv[i] != NULL; i++) {
672 pl->conf_file = globbuf.gl_pathv[i];
673 retval = parse_config_file(pamh, pwd->pw_name, ctrl, pl);
674 if (retval == PAM_IGNORE) {
675 D(("the configuration file ('%s') has an applicable '<domain> -' entry", pl->conf_file));
679 if (retval != PAM_SUCCESS)
686 if (retval != PAM_SUCCESS)
688 pam_syslog(pamh, LOG_WARNING, "error parsing the configuration file: '%s' ",CONF_FILE);
692 if (ctrl & PAM_DO_SETREUID) {
693 setreuid(pwd->pw_uid, -1);
696 retval = setup_limits(pamh, pwd->pw_name, pwd->pw_uid, ctrl, pl);
697 if (retval & LOGIN_ERR)
698 pam_error(pamh, _("Too many logins for '%s'."), pwd->pw_name);
699 if (retval != LIMITED_OK) {
700 return PAM_PERM_DENIED;
707 pam_sm_close_session (pam_handle_t *pamh UNUSED, int flags UNUSED,
708 int argc UNUSED, const char **argv UNUSED)
716 /* static module data */
718 struct pam_module _pam_limits_modstruct = {
724 pam_sm_close_session,
730 * Copyright (c) Cristian Gafton, 1996-1997, <gafton@redhat.com>
731 * All rights reserved.
733 * Redistribution and use in source and binary forms, with or without
734 * modification, are permitted provided that the following conditions
736 * 1. Redistributions of source code must retain the above copyright
737 * notice, and the entire permission notice in its entirety,
738 * including the disclaimer of warranties.
739 * 2. Redistributions in binary form must reproduce the above copyright
740 * notice, this list of conditions and the following disclaimer in the
741 * documentation and/or other materials provided with the distribution.
742 * 3. The name of the author may not be used to endorse or promote
743 * products derived from this software without specific prior
744 * written permission.
746 * ALTERNATIVELY, this product may be distributed under the terms of
747 * the GNU Public License, in which case the provisions of the GPL are
748 * required INSTEAD OF the above restrictions. (This clause is
749 * necessary due to a potential bad interaction between the GPL and
750 * the restrictions contained in a BSD-style copyright.)
752 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
753 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
754 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
755 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
756 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
757 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
758 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
759 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
760 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
761 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
762 * OF THE POSSIBILITY OF SUCH DAMAGE.