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
17 #error 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
44 #define LINE_LENGTH 1024
46 #define LIMITS_DEF_USER 0 /* limit was set by an user entry */
47 #define LIMITS_DEF_GROUP 1 /* limit was set by a group entry */
48 #define LIMITS_DEF_ALLGROUP 2 /* limit was set by a group entry */
49 #define LIMITS_DEF_ALL 3 /* limit was set by an default entry */
50 #define LIMITS_DEF_DEFAULT 4 /* limit was set by an default entry */
51 #define LIMITS_DEF_NONE 5 /* this limit was not set yet */
53 static const char *limits_def_names[] = {
63 struct user_limits_struct {
72 int login_limit; /* the max logins limit */
73 int login_limit_def; /* which entry set the login limit */
74 int flag_numsyslogins; /* whether to limit logins only for a
75 specific user or to count all logins */
76 int priority; /* the priority to run user process with */
77 struct user_limits_struct limits[RLIM_NLIMITS];
78 char conf_file[BUFSIZ];
79 int utmp_after_pam_call;
80 char login_group[LINE_LENGTH];
83 #define LIMIT_LOGIN RLIM_NLIMITS+1
84 #define LIMIT_NUMSYSLOGINS RLIM_NLIMITS+2
86 #define LIMIT_PRI RLIM_NLIMITS+3
91 #define PAM_SM_SESSION
93 #include <security/pam_modules.h>
94 #include <security/_pam_macros.h>
95 #include <security/_pam_modutil.h>
98 static void _pam_log(int err, const char *format, ...)
102 va_start(args, format);
103 openlog("pam_limits", LOG_CONS|LOG_PID, LOG_AUTH);
104 vsyslog(err, format, args);
109 /* argument parsing */
111 #define PAM_DEBUG_ARG 0x0001
112 #define PAM_DO_SETREUID 0x0002
113 #define PAM_UTMP_EARLY 0x0004
115 static int _pam_parse(int argc, const char **argv, struct pam_limit_s *pl)
119 /* step through arguments */
120 for (ctrl=0; argc-- > 0; ++argv) {
122 /* generic options */
124 if (!strcmp(*argv,"debug")) {
125 ctrl |= PAM_DEBUG_ARG;
126 } else if (!strncmp(*argv,"conf=",5)) {
127 strncpy(pl->conf_file,*argv+5,sizeof(pl->conf_file)-1);
128 } else if (!strncmp(*argv,"change_uid",10)) {
129 ctrl |= PAM_DO_SETREUID;
130 } else if (!strcmp(*argv,"utmp_early")) {
131 ctrl |= PAM_UTMP_EARLY;
133 _pam_log(LOG_ERR,"pam_parse: unknown option; %s",*argv);
136 pl->conf_file[sizeof(pl->conf_file) - 1] = '\0';
142 #define LIMITED_OK 0 /* limit setting appeared to work */
143 #define LIMIT_ERR 1 /* error setting a limit */
144 #define LOGIN_ERR 2 /* too many logins err */
146 /* Counts the number of user logins and check against the limit*/
148 check_logins (pam_handle_t *pamh, const char *name, int limit, int ctrl,
149 struct pam_limit_s *pl)
154 if (ctrl & PAM_DEBUG_ARG) {
155 _pam_log(LOG_DEBUG, "checking logins for '%s' (maximum of %d)\n",
160 return 0; /* no limits imposed */
161 if (limit == 0) /* maximum 0 logins ? */ {
162 _pam_log(LOG_WARNING, "No logins allowed for '%s'\n", name);
168 /* Because there is no definition about when an application
169 actually adds a utmp entry, some applications bizarrely do the
170 utmp call before the have PAM authenticate them to the system:
171 you're logged it, sort of...? Anyway, you can use the
172 "utmp_early" module argument in your PAM config file to make
173 allowances for this sort of problem. (There should be a PAM
174 standard for this, since if a module wants to actually map a
175 username then any early utmp entry will be for the unmapped
178 if (ctrl & PAM_UTMP_EARLY) {
184 while((ut = getutent())) {
186 if (ut->ut_type != USER_PROCESS) {
190 if (ut->UT_USER[0] == '\0') {
193 if (!pl->flag_numsyslogins) {
194 if (((pl->login_limit_def == LIMITS_DEF_USER)
195 || (pl->login_limit_def == LIMITS_DEF_GROUP)
196 || (pl->login_limit_def == LIMITS_DEF_DEFAULT))
197 && strncmp(name, ut->UT_USER, sizeof(ut->UT_USER)) != 0) {
200 if ((pl->login_limit_def == LIMITS_DEF_ALLGROUP)
201 && !_pammodutil_user_in_group_nam_nam(pamh, ut->UT_USER, pl->login_group)) {
205 if (++count > limit) {
212 _pam_log(LOG_WARNING, "Too many logins (max %d) for %s",
215 _pam_log(LOG_WARNING, "Too many system logins (max %d)", limit);
222 static int init_limits(struct pam_limit_s *pl)
225 int retval = PAM_SUCCESS;
229 for(i = 0; i < RLIM_NLIMITS; i++) {
230 int r = getrlimit(i, &pl->limits[i].limit);
232 pl->limits[i].supported = 0;
233 if (errno != EINVAL) {
234 retval = !PAM_SUCCESS;
237 pl->limits[i].supported = 1;
238 pl->limits[i].src_soft = LIMITS_DEF_NONE;
239 pl->limits[i].src_hard = LIMITS_DEF_NONE;
244 pl->priority = getpriority (PRIO_PROCESS, 0);
245 if (pl->priority == -1 && errno != 0)
246 retval = !PAM_SUCCESS;
247 pl->login_limit = -2;
248 pl->login_limit_def = LIMITS_DEF_NONE;
253 static void process_limit(int source, const char *lim_type,
254 const char *lim_item, const char *lim_value,
255 int ctrl, struct pam_limit_s *pl)
260 rlim_t rlimit_value = 0;
262 const char *value_orig = lim_value;
264 if (ctrl & PAM_DEBUG_ARG)
265 _pam_log(LOG_DEBUG, "%s: processing %s %s %s for %s\n",
266 __FUNCTION__,lim_type,lim_item,lim_value,
267 limits_def_names[source]);
269 if (strcmp(lim_item, "cpu") == 0)
270 limit_item = RLIMIT_CPU;
271 else if (strcmp(lim_item, "fsize") == 0)
272 limit_item = RLIMIT_FSIZE;
273 else if (strcmp(lim_item, "data") == 0)
274 limit_item = RLIMIT_DATA;
275 else if (strcmp(lim_item, "stack") == 0)
276 limit_item = RLIMIT_STACK;
277 else if (strcmp(lim_item, "core") == 0)
278 limit_item = RLIMIT_CORE;
279 else if (strcmp(lim_item, "rss") == 0)
280 limit_item = RLIMIT_RSS;
281 else if (strcmp(lim_item, "nproc") == 0)
282 limit_item = RLIMIT_NPROC;
283 else if (strcmp(lim_item, "nofile") == 0)
284 limit_item = RLIMIT_NOFILE;
285 else if (strcmp(lim_item, "memlock") == 0)
286 limit_item = RLIMIT_MEMLOCK;
287 else if (strcmp(lim_item, "as") == 0)
288 limit_item = RLIMIT_AS;
290 else if (strcmp(lim_item, "locks") == 0)
291 limit_item = RLIMIT_LOCKS;
293 #ifdef RLIMIT_SIGPENDING
294 else if (strcmp(lim_item, "sigpending") == 0)
295 limit_item = RLIMIT_SIGPENDING;
297 #ifdef RLIMIT_MSGQUEUE
298 else if (strcmp(lim_item, "msgqueue") == 0)
299 limit_item = RLIMIT_MSGQUEUE;
302 else if (strcmp(lim_item, "nice") == 0)
303 limit_item = RLIMIT_NICE;
306 else if (strcmp(lim_item, "rtprio") == 0)
307 limit_item = RLIMIT_RTPRIO;
309 else if (strcmp(lim_item, "maxlogins") == 0) {
310 limit_item = LIMIT_LOGIN;
311 pl->flag_numsyslogins = 0;
312 } else if (strcmp(lim_item, "maxsyslogins") == 0) {
313 limit_item = LIMIT_NUMSYSLOGINS;
314 pl->flag_numsyslogins = 1;
315 } else if (strcmp(lim_item, "priority") == 0) {
316 limit_item = LIMIT_PRI;
318 _pam_log(LOG_DEBUG,"unknown limit item '%s'", lim_item);
322 if (strcmp(lim_type,"soft")==0)
323 limit_type=LIMIT_SOFT;
324 else if (strcmp(lim_type, "hard")==0)
325 limit_type=LIMIT_HARD;
326 else if (strcmp(lim_type,"-")==0)
327 limit_type=LIMIT_SOFT | LIMIT_HARD;
328 else if (limit_item != LIMIT_LOGIN && limit_item != LIMIT_NUMSYSLOGINS) {
329 _pam_log(LOG_DEBUG,"unknown limit type '%s'", lim_type);
332 if (limit_item != LIMIT_PRI
334 && limit_item != RLIMIT_NICE
336 && (strcmp(lim_value, "-1") == 0
337 || strcmp(lim_value, "-") == 0 || strcmp(lim_value, "unlimited") == 0
338 || strcmp(lim_value, "infinity") == 0)) {
340 rlimit_value = RLIM_INFINITY;
341 } else if (limit_item == LIMIT_PRI || limit_item == LIMIT_LOGIN ||
343 limit_item == RLIMIT_NICE ||
345 limit_item == LIMIT_NUMSYSLOGINS) {
347 temp = strtol (lim_value, &endptr, 10);
348 temp = temp < INT_MAX ? temp : INT_MAX;
349 int_value = temp > INT_MIN ? temp : INT_MIN;
350 if (int_value == 0 && value_orig == endptr) {
351 _pam_log(LOG_DEBUG, "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_log(LOG_DEBUG, "wrong limit value '%s' for limit type '%s'",
363 lim_value, lim_type);
368 /* one more special case when limiting logins */
369 if ((source == LIMITS_DEF_ALL || source == LIMITS_DEF_ALLGROUP)
370 && (limit_item != LIMIT_LOGIN)) {
371 if (ctrl & PAM_DEBUG_ARG)
373 "'%%' domain valid for maxlogins type only");
379 if (rlimit_value != RLIM_INFINITY)
389 if (rlimit_value != RLIM_INFINITY)
390 rlimit_value *= 1024;
395 rlimit_value = 19 - int_value;
400 if ( (limit_item != LIMIT_LOGIN)
401 && (limit_item != LIMIT_NUMSYSLOGINS)
402 && (limit_item != LIMIT_PRI) ) {
403 if (limit_type & LIMIT_SOFT) {
404 if (pl->limits[limit_item].src_soft < source) {
407 pl->limits[limit_item].limit.rlim_cur = rlimit_value;
408 pl->limits[limit_item].src_soft = source;
411 if (limit_type & LIMIT_HARD) {
412 if (pl->limits[limit_item].src_hard < source) {
415 pl->limits[limit_item].limit.rlim_max = rlimit_value;
416 pl->limits[limit_item].src_hard = source;
420 /* recent kernels support negative priority limits (=raise priority) */
422 if (limit_item == LIMIT_PRI) {
423 pl->priority = int_value;
425 if (pl->login_limit_def < source) {
428 pl->login_limit = int_value;
429 pl->login_limit_def = source;
436 static int parse_config_file(pam_handle_t *pamh, const char *uname, int ctrl,
437 struct pam_limit_s *pl)
440 char buf[LINE_LENGTH];
442 #define CONF_FILE (pl->conf_file[0])?pl->conf_file:LIMITS_FILE
443 /* check for the LIMITS_FILE */
444 if (ctrl & PAM_DEBUG_ARG)
445 _pam_log(LOG_DEBUG,"reading settings from '%s'", CONF_FILE);
446 fil = fopen(CONF_FILE, "r");
448 _pam_log (LOG_WARNING, "can not read settings from %s", CONF_FILE);
449 return PAM_SERVICE_ERR;
454 memset(buf, 0, sizeof(buf));
456 while (fgets(buf, LINE_LENGTH, fil) != NULL) {
457 char domain[LINE_LENGTH];
458 char ltype[LINE_LENGTH];
459 char item[LINE_LENGTH];
460 char value[LINE_LENGTH];
466 /* skip the leading white space */
467 while (*tptr && isspace(*tptr))
469 strncpy(buf, tptr, sizeof(buf)-1);
470 buf[sizeof(buf)-1] = '\0';
472 /* Rip off the comments */
473 tptr = strchr(buf,'#');
476 /* Rip off the newline char */
477 tptr = strchr(buf,'\n');
480 /* Anything left ? */
482 memset(buf, 0, sizeof(buf));
486 memset(domain, 0, sizeof(domain));
487 memset(ltype, 0, sizeof(ltype));
488 memset(item, 0, sizeof(item));
489 memset(value, 0, sizeof(value));
491 i = sscanf(buf,"%s%s%s%s", domain, ltype, item, value);
492 D(("scanned line[%d]: domain[%s], ltype[%s], item[%s], value[%s]",
493 i, domain, ltype, item, value));
495 for(j=0; j < strlen(ltype); j++)
496 ltype[j]=tolower(ltype[j]);
497 for(j=0; j < strlen(item); j++)
498 item[j]=tolower(item[j]);
499 for(j=0; j < strlen(value); j++)
500 value[j]=tolower(value[j]);
502 if (i == 4) { /* a complete line */
503 if (strcmp(uname, domain) == 0) /* this user have a limit */
504 process_limit(LIMITS_DEF_USER, ltype, item, value, ctrl, pl);
505 else if (domain[0]=='@') {
506 if (ctrl & PAM_DEBUG_ARG) {
507 _pam_log(LOG_DEBUG, "checking if %s is in group %s",
510 if (_pammodutil_user_in_group_nam_nam(pamh, uname, domain+1))
511 process_limit(LIMITS_DEF_GROUP, ltype, item, value, ctrl,
513 } else if (domain[0]=='%') {
514 if (ctrl & PAM_DEBUG_ARG) {
515 _pam_log(LOG_DEBUG, "checking if %s is in group %s",
518 if (strcmp(domain,"%") == 0)
519 process_limit(LIMITS_DEF_ALL, ltype, item, value, ctrl,
521 else if (_pammodutil_user_in_group_nam_nam(pamh, uname, domain+1)) {
522 strcpy(pl->login_group, domain+1);
523 process_limit(LIMITS_DEF_ALLGROUP, ltype, item, value, ctrl,
526 } else if (strcmp(domain, "*") == 0)
527 process_limit(LIMITS_DEF_DEFAULT, ltype, item, value, ctrl,
529 } else if (i == 2 && ltype[0] == '-') { /* Probably a no-limit line */
530 if (strcmp(uname, domain) == 0) {
531 if (ctrl & PAM_DEBUG_ARG) {
532 _pam_log(LOG_DEBUG, "no limits for '%s'", uname);
536 } else if (domain[0] == '@' && _pammodutil_user_in_group_nam_nam(pamh, uname, domain+1)) {
537 if (ctrl & PAM_DEBUG_ARG) {
538 _pam_log(LOG_DEBUG, "no limits for '%s' in group '%s'",
545 _pam_log(LOG_DEBUG,"invalid line '%s' - skipped", buf);
552 static int setup_limits(pam_handle_t *pamh,
553 const char *uname, uid_t uid, int ctrl,
554 struct pam_limit_s *pl)
558 int retval = LIMITED_OK;
561 /* do not impose limits (+ve limits anyway) on the superuser */
562 if (pl->priority > 0) {
563 if (ctrl & PAM_DEBUG_ARG) {
564 _pam_log(LOG_DEBUG, "user '%s' has UID 0 - no limits imposed",
571 for (i=0, status=LIMITED_OK; i<RLIM_NLIMITS; i++) {
572 if (!pl->limits[i].supported) {
573 /* skip it if its not known to the system */
576 if (pl->limits[i].src_soft == LIMITS_DEF_NONE &&
577 pl->limits[i].src_hard == LIMITS_DEF_NONE) {
578 /* skip it if its not initialized */
581 if (pl->limits[i].limit.rlim_cur > pl->limits[i].limit.rlim_max)
582 pl->limits[i].limit.rlim_cur = pl->limits[i].limit.rlim_max;
583 status |= setrlimit(i, &pl->limits[i].limit);
590 status = setpriority(PRIO_PROCESS, 0, pl->priority);
596 D(("skip login limit check for uid=0"));
597 } else if (pl->login_limit > 0) {
598 if (check_logins(pamh, uname, pl->login_limit, ctrl, pl) == LOGIN_ERR) {
601 } else if (pl->login_limit == 0) {
608 /* now the session stuff */
610 pam_sm_open_session (pam_handle_t *pamh, int flags UNUSED,
611 int argc, const char **argv)
617 struct pam_limit_s pl;
621 memset(&pl, 0, sizeof(pl));
623 ctrl = _pam_parse(argc, argv, &pl);
624 retval = pam_get_item( pamh, PAM_USER, (void*) &user_name );
625 if ( user_name == NULL || retval != PAM_SUCCESS ) {
626 _pam_log(LOG_CRIT, "open_session - error recovering username");
627 return PAM_SESSION_ERR;
630 pwd = getpwnam(user_name);
632 if (ctrl & PAM_DEBUG_ARG)
633 _pam_log(LOG_WARNING, "open_session username '%s' does not exist",
635 return PAM_SESSION_ERR;
638 retval = init_limits(&pl);
639 if (retval != PAM_SUCCESS) {
640 _pam_log(LOG_WARNING, "cannot initialize");
644 retval = parse_config_file(pamh, pwd->pw_name, ctrl, &pl);
645 if (retval == PAM_IGNORE) {
646 D(("the configuration file has an applicable '<domain> -' entry"));
649 if (retval != PAM_SUCCESS) {
650 _pam_log(LOG_WARNING, "error parsing the configuration file");
654 if (ctrl & PAM_DO_SETREUID) {
655 setreuid(pwd->pw_uid, -1);
657 retval = setup_limits(pamh, pwd->pw_name, pwd->pw_uid, ctrl, &pl);
658 if (retval != LIMITED_OK) {
659 return PAM_PERM_DENIED;
666 pam_sm_close_session (pam_handle_t *pamh UNUSED, int flags UNUSED,
667 int argc UNUSED, const char **argv UNUSED)
675 /* static module data */
677 struct pam_module _pam_limits_modstruct = {
683 pam_sm_close_session,
689 * Copyright (c) Cristian Gafton, 1996-1997, <gafton@redhat.com>
690 * All rights reserved.
692 * Redistribution and use in source and binary forms, with or without
693 * modification, are permitted provided that the following conditions
695 * 1. Redistributions of source code must retain the above copyright
696 * notice, and the entire permission notice in its entirety,
697 * including the disclaimer of warranties.
698 * 2. Redistributions in binary form must reproduce the above copyright
699 * notice, this list of conditions and the following disclaimer in the
700 * documentation and/or other materials provided with the distribution.
701 * 3. The name of the author may not be used to endorse or promote
702 * products derived from this software without specific prior
703 * written permission.
705 * ALTERNATIVELY, this product may be distributed under the terms of
706 * the GNU Public License, in which case the provisions of the GPL are
707 * required INSTEAD OF the above restrictions. (This clause is
708 * necessary due to a potential bad interaction between the GPL and
709 * the restrictions contained in a BSD-style copyright.)
711 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
712 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
713 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
714 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
715 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
716 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
717 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
718 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
719 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
720 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
721 * OF THE POSSIBILITY OF SUCH DAMAGE.