]> granicus.if.org Git - linux-pam/blob - modules/pam_limits/pam_limits.c
Relevant BUGIDs:
[linux-pam] / modules / pam_limits / pam_limits.c
1 /*
2  * pam_limits - impose resource limits when opening a user session
3  *
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
12  *
13  * See end for Copyright information
14  */
15
16 #if !defined(linux) && !defined(__linux)
17 #warning THIS CODE IS KNOWN TO WORK ONLY ON LINUX !!!
18 #endif
19
20 #include "config.h"
21
22 #include <stdio.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <stdlib.h>
27 #include <errno.h>
28 #include <syslog.h>
29 #include <stdarg.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/resource.h>
33 #include <limits.h>
34 #include <glob.h>
35 #include <utmp.h>
36 #ifndef UT_USER  /* some systems have ut_name instead of ut_user */
37 #define UT_USER ut_user
38 #endif
39
40 #include <grp.h>
41 #include <pwd.h>
42 #include <locale.h>
43
44 #ifdef HAVE_LIBAUDIT
45 #include <libaudit.h>
46 #endif
47
48 /* Module defines */
49 #define LINE_LENGTH 1024
50
51 #define LIMITS_DEF_USER     0 /* limit was set by an user entry */
52 #define LIMITS_DEF_GROUP    1 /* limit was set by a group entry */
53 #define LIMITS_DEF_ALLGROUP 2 /* limit was set by a group entry */
54 #define LIMITS_DEF_ALL      3 /* limit was set by an default entry */
55 #define LIMITS_DEF_DEFAULT  4 /* limit was set by an default entry */
56 #define LIMITS_DEF_NONE     5 /* this limit was not set yet */
57
58 static const char *limits_def_names[] = {
59        "USER",
60        "GROUP",
61        "ALLGROUP",
62        "ALL",
63        "DEFAULT",
64        "NONE",
65        NULL
66 };
67
68 struct user_limits_struct {
69     int supported;
70     int src_soft;
71     int src_hard;
72     struct rlimit limit;
73 };
74
75 /* internal data */
76 struct pam_limit_s {
77     int login_limit;     /* the max logins limit */
78     int login_limit_def; /* which entry set the login limit */
79     int flag_numsyslogins; /* whether to limit logins only for a
80                               specific user or to count all logins */
81     int priority;        /* the priority to run user process with */
82     struct user_limits_struct limits[RLIM_NLIMITS];
83     const char *conf_file;
84     int utmp_after_pam_call;
85     char login_group[LINE_LENGTH];
86 };
87
88 #define LIMIT_LOGIN RLIM_NLIMITS+1
89 #define LIMIT_NUMSYSLOGINS RLIM_NLIMITS+2
90
91 #define LIMIT_PRI RLIM_NLIMITS+3
92
93 #define LIMIT_SOFT  1
94 #define LIMIT_HARD  2
95
96 #define PAM_SM_SESSION
97
98 #include <security/pam_modules.h>
99 #include <security/_pam_macros.h>
100 #include <security/pam_modutil.h>
101 #include <security/pam_ext.h>
102
103 /* argument parsing */
104
105 #define PAM_DEBUG_ARG       0x0001
106 #define PAM_DO_SETREUID     0x0002
107 #define PAM_UTMP_EARLY      0x0004
108 #define PAM_NO_AUDIT        0x0008
109
110 /* Limits from globbed files. */
111 #define LIMITS_CONF_GLOB LIMITS_FILE_DIR
112
113 #define CONF_FILE (pl->conf_file != NULL)?pl->conf_file:LIMITS_FILE
114
115 static int
116 _pam_parse (const pam_handle_t *pamh, int argc, const char **argv,
117             struct pam_limit_s *pl)
118 {
119     int ctrl=0;
120
121     /* step through arguments */
122     for (ctrl=0; argc-- > 0; ++argv) {
123
124         /* generic options */
125
126         if (!strcmp(*argv,"debug")) {
127             ctrl |= PAM_DEBUG_ARG;
128         } else if (!strncmp(*argv,"conf=",5)) {
129             pl->conf_file = *argv+5;
130         } else if (!strncmp(*argv,"change_uid",10)) {
131             ctrl |= PAM_DO_SETREUID;
132         } else if (!strcmp(*argv,"utmp_early")) {
133             ctrl |= PAM_UTMP_EARLY;
134         } else if (!strcmp(*argv,"noaudit")) {
135             ctrl |= PAM_NO_AUDIT;
136         } else {
137             pam_syslog(pamh, LOG_ERR, "unknown option: %s", *argv);
138         }
139     }
140
141     return ctrl;
142 }
143
144 static const char *
145 rlimit2str (int i)
146 {
147   switch (i) {
148   case RLIMIT_CPU:
149     return "cpu";
150     break;
151   case RLIMIT_FSIZE:
152     return "fsize";
153     break;
154   case RLIMIT_DATA:
155     return "data";
156     break;
157   case RLIMIT_STACK:
158     return "stack";
159     break;
160   case RLIMIT_CORE:
161     return "core";
162     break;
163   case RLIMIT_RSS:
164     return "rss";
165     break;
166   case RLIMIT_NPROC:
167     return "nproc";
168     break;
169   case RLIMIT_NOFILE:
170     return "nofile";
171     break;
172   case RLIMIT_MEMLOCK:
173     return "memlock";
174     break;
175 #ifdef RLIMIT_AS
176   case RLIMIT_AS:
177     return "as";
178     break;
179 #endif
180 #ifdef RLIMIT_LOCKS
181   case RLIMIT_LOCKS:
182     return "locks";
183     break;
184 #endif
185 #ifdef RLIMIT_SIGPENDING
186   case RLIMIT_SIGPENDING:
187     return "sigpending";
188     break;
189 #endif
190 #ifdef RLIMIT_MSGQUEUE
191   case RLIMIT_MSGQUEUE:
192     return "msgqueue";
193     break;
194 #endif
195 #ifdef RLIMIT_NICE
196   case RLIMIT_NICE:
197     return "nice";
198     break;
199 #endif
200 #ifdef RLIMIT_RTPRIO
201   case RLIMIT_RTPRIO:
202     return "rtprio";
203     break;
204 #endif
205   default:
206     return "UNKNOWN";
207     break;
208   }
209 }
210
211
212 #define LIMITED_OK 0 /* limit setting appeared to work */
213 #define LIMIT_ERR  1 /* error setting a limit */
214 #define LOGIN_ERR  2 /* too many logins err */
215
216 /* Counts the number of user logins and check against the limit*/
217 static int
218 check_logins (pam_handle_t *pamh, const char *name, int limit, int ctrl,
219               struct pam_limit_s *pl)
220 {
221     struct utmp *ut;
222     int count;
223
224     if (ctrl & PAM_DEBUG_ARG) {
225         pam_syslog(pamh, LOG_DEBUG,
226                    "checking logins for '%s' (maximum of %d)", name, limit);
227     }
228
229     if (limit < 0)
230         return 0; /* no limits imposed */
231     if (limit == 0) /* maximum 0 logins ? */ {
232         pam_syslog(pamh, LOG_WARNING, "No logins allowed for '%s'", name);
233         return LOGIN_ERR;
234     }
235
236     setutent();
237
238     /* Because there is no definition about when an application
239        actually adds a utmp entry, some applications bizarrely do the
240        utmp call before the have PAM authenticate them to the system:
241        you're logged it, sort of...? Anyway, you can use the
242        "utmp_early" module argument in your PAM config file to make
243        allowances for this sort of problem. (There should be a PAM
244        standard for this, since if a module wants to actually map a
245        username then any early utmp entry will be for the unmapped
246        name = broken.) */
247
248     if (ctrl & PAM_UTMP_EARLY) {
249         count = 0;
250     } else {
251         count = 1;
252     }
253
254     while((ut = getutent())) {
255 #ifdef USER_PROCESS
256         if (ut->ut_type != USER_PROCESS) {
257             continue;
258         }
259 #endif
260         if (ut->UT_USER[0] == '\0') {
261             continue;
262         }
263         if (!pl->flag_numsyslogins) {
264             if (((pl->login_limit_def == LIMITS_DEF_USER)
265                  || (pl->login_limit_def == LIMITS_DEF_GROUP)
266                  || (pl->login_limit_def == LIMITS_DEF_DEFAULT))
267                 && strncmp(name, ut->UT_USER, sizeof(ut->UT_USER)) != 0) {
268                 continue;
269             }
270             if ((pl->login_limit_def == LIMITS_DEF_ALLGROUP)
271                 && !pam_modutil_user_in_group_nam_nam(pamh, ut->UT_USER, pl->login_group)) {
272                 continue;
273             }
274         }
275         if (++count > limit) {
276             break;
277         }
278     }
279     endutent();
280     if (count > limit) {
281         if (name) {
282             pam_syslog(pamh, LOG_WARNING,
283                        "Too many logins (max %d) for %s", limit, name);
284         } else {
285             pam_syslog(pamh, LOG_WARNING, "Too many system logins (max %d)", limit);
286         }
287         return LOGIN_ERR;
288     }
289     return 0;
290 }
291
292 static int init_limits(struct pam_limit_s *pl)
293 {
294     int i;
295     int retval = PAM_SUCCESS;
296
297     D(("called."));
298
299     for(i = 0; i < RLIM_NLIMITS; i++) {
300         int r = getrlimit(i, &pl->limits[i].limit);
301         if (r == -1) {
302             pl->limits[i].supported = 0;
303             if (errno != EINVAL) {
304                 retval = !PAM_SUCCESS;
305             }
306         } else {
307             pl->limits[i].supported = 1;
308             pl->limits[i].src_soft = LIMITS_DEF_NONE;
309             pl->limits[i].src_hard = LIMITS_DEF_NONE;
310         }
311     }
312
313     errno = 0;
314     pl->priority = getpriority (PRIO_PROCESS, 0);
315     if (pl->priority == -1 && errno != 0)
316       retval = !PAM_SUCCESS;
317     pl->login_limit = -2;
318     pl->login_limit_def = LIMITS_DEF_NONE;
319
320     return retval;
321 }
322
323 static void
324 process_limit (const pam_handle_t *pamh, int source, const char *lim_type,
325                const char *lim_item, const char *lim_value,
326                int ctrl, struct pam_limit_s *pl)
327 {
328     int limit_item;
329     int limit_type = 0;
330     int int_value = 0;
331     rlim_t rlimit_value = 0;
332     char *endptr;
333     const char *value_orig = lim_value;
334
335     if (ctrl & PAM_DEBUG_ARG)
336          pam_syslog(pamh, LOG_DEBUG, "%s: processing %s %s %s for %s",
337                     __FUNCTION__, lim_type, lim_item, lim_value,
338                     limits_def_names[source]);
339
340     if (strcmp(lim_item, "cpu") == 0)
341         limit_item = RLIMIT_CPU;
342     else if (strcmp(lim_item, "fsize") == 0)
343         limit_item = RLIMIT_FSIZE;
344     else if (strcmp(lim_item, "data") == 0)
345         limit_item = RLIMIT_DATA;
346     else if (strcmp(lim_item, "stack") == 0)
347         limit_item = RLIMIT_STACK;
348     else if (strcmp(lim_item, "core") == 0)
349         limit_item = RLIMIT_CORE;
350     else if (strcmp(lim_item, "rss") == 0)
351         limit_item = RLIMIT_RSS;
352     else if (strcmp(lim_item, "nproc") == 0)
353         limit_item = RLIMIT_NPROC;
354     else if (strcmp(lim_item, "nofile") == 0)
355         limit_item = RLIMIT_NOFILE;
356     else if (strcmp(lim_item, "memlock") == 0)
357         limit_item = RLIMIT_MEMLOCK;
358 #ifdef RLIMIT_AS
359     else if (strcmp(lim_item, "as") == 0)
360         limit_item = RLIMIT_AS;
361 #endif /*RLIMIT_AS*/
362 #ifdef RLIMIT_LOCKS
363     else if (strcmp(lim_item, "locks") == 0)
364         limit_item = RLIMIT_LOCKS;
365 #endif
366 #ifdef RLIMIT_SIGPENDING
367     else if (strcmp(lim_item, "sigpending") == 0)
368         limit_item = RLIMIT_SIGPENDING;
369 #endif
370 #ifdef RLIMIT_MSGQUEUE
371     else if (strcmp(lim_item, "msgqueue") == 0)
372         limit_item = RLIMIT_MSGQUEUE;
373 #endif
374 #ifdef RLIMIT_NICE
375     else if (strcmp(lim_item, "nice") == 0)
376         limit_item = RLIMIT_NICE;
377 #endif
378 #ifdef RLIMIT_RTPRIO
379     else if (strcmp(lim_item, "rtprio") == 0)
380         limit_item = RLIMIT_RTPRIO;
381 #endif
382     else if (strcmp(lim_item, "maxlogins") == 0) {
383         limit_item = LIMIT_LOGIN;
384         pl->flag_numsyslogins = 0;
385     } else if (strcmp(lim_item, "maxsyslogins") == 0) {
386         limit_item = LIMIT_NUMSYSLOGINS;
387         pl->flag_numsyslogins = 1;
388     } else if (strcmp(lim_item, "priority") == 0) {
389         limit_item = LIMIT_PRI;
390     } else {
391         pam_syslog(pamh, LOG_DEBUG, "unknown limit item '%s'", lim_item);
392         return;
393     }
394
395     if (strcmp(lim_type,"soft")==0)
396         limit_type=LIMIT_SOFT;
397     else if (strcmp(lim_type, "hard")==0)
398         limit_type=LIMIT_HARD;
399     else if (strcmp(lim_type,"-")==0)
400         limit_type=LIMIT_SOFT | LIMIT_HARD;
401     else if (limit_item != LIMIT_LOGIN && limit_item != LIMIT_NUMSYSLOGINS) {
402         pam_syslog(pamh, LOG_DEBUG, "unknown limit type '%s'", lim_type);
403         return;
404     }
405         if (limit_item != LIMIT_PRI
406 #ifdef RLIMIT_NICE
407             && limit_item != RLIMIT_NICE
408 #endif
409             && (strcmp(lim_value, "-1") == 0
410                 || strcmp(lim_value, "-") == 0 || strcmp(lim_value, "unlimited") == 0
411                 || strcmp(lim_value, "infinity") == 0)) {
412                 int_value = -1;
413                 rlimit_value = RLIM_INFINITY;
414         } else if (limit_item == LIMIT_PRI || limit_item == LIMIT_LOGIN ||
415 #ifdef RLIMIT_NICE
416                 limit_item == RLIMIT_NICE ||
417 #endif
418                 limit_item == LIMIT_NUMSYSLOGINS) {
419                 long temp;
420                 temp = strtol (lim_value, &endptr, 10);
421                 temp = temp < INT_MAX ? temp : INT_MAX;
422                 int_value = temp > INT_MIN ? temp : INT_MIN;
423                 if (int_value == 0 && value_orig == endptr) {
424                         pam_syslog(pamh, LOG_DEBUG,
425                                    "wrong limit value '%s' for limit type '%s'",
426                                    lim_value, lim_type);
427             return;
428                 }
429         } else {
430 #ifdef __USE_FILE_OFFSET64
431                 rlimit_value = strtoull (lim_value, &endptr, 10);
432 #else
433                 rlimit_value = strtoul (lim_value, &endptr, 10);
434 #endif
435                 if (rlimit_value == 0 && value_orig == endptr) {
436                         pam_syslog(pamh, LOG_DEBUG,
437                                    "wrong limit value '%s' for limit type '%s'",
438                                    lim_value, lim_type);
439                         return;
440                 }
441         }
442
443     /* one more special case when limiting logins */
444     if ((source == LIMITS_DEF_ALL || source == LIMITS_DEF_ALLGROUP)
445                 && (limit_item != LIMIT_LOGIN)) {
446         if (ctrl & PAM_DEBUG_ARG)
447             pam_syslog(pamh, LOG_DEBUG,
448                        "'%%' domain valid for maxlogins type only");
449         return;
450     }
451
452     switch(limit_item) {
453         case RLIMIT_CPU:
454           if (rlimit_value != RLIM_INFINITY)
455             {
456               if (rlimit_value >= RLIM_INFINITY/60)
457                 rlimit_value = RLIM_INFINITY;
458               else
459                 rlimit_value *= 60;
460             }
461          break;
462         case RLIMIT_FSIZE:
463         case RLIMIT_DATA:
464         case RLIMIT_STACK:
465         case RLIMIT_CORE:
466         case RLIMIT_RSS:
467         case RLIMIT_MEMLOCK:
468 #ifdef RLIMIT_AS
469         case RLIMIT_AS:
470 #endif
471          if (rlimit_value != RLIM_INFINITY)
472            {
473              if (rlimit_value >= RLIM_INFINITY/1024)
474                rlimit_value = RLIM_INFINITY;
475              else
476                rlimit_value *= 1024;
477            }
478          break;
479 #ifdef RLIMIT_NICE
480         case RLIMIT_NICE:
481          if (int_value > 19)
482             int_value = 19;
483          if (int_value < -20)
484            int_value = -20;
485          rlimit_value = 20 - int_value;
486          break;
487 #endif
488     }
489
490     if ( (limit_item != LIMIT_LOGIN)
491          && (limit_item != LIMIT_NUMSYSLOGINS)
492          && (limit_item != LIMIT_PRI) ) {
493         if (limit_type & LIMIT_SOFT) {
494             if (pl->limits[limit_item].src_soft < source) {
495                 return;
496             } else {
497                 pl->limits[limit_item].limit.rlim_cur = rlimit_value;
498                 pl->limits[limit_item].src_soft = source;
499             }
500         }
501         if (limit_type & LIMIT_HARD) {
502             if (pl->limits[limit_item].src_hard < source) {
503                 return;
504             } else {
505                 pl->limits[limit_item].limit.rlim_max = rlimit_value;
506                 pl->limits[limit_item].src_hard = source;
507             }
508         }
509     } else {
510         /* recent kernels support negative priority limits (=raise priority) */
511
512         if (limit_item == LIMIT_PRI) {
513                 pl->priority = int_value;
514         } else {
515                 if (pl->login_limit_def < source) {
516                     return;
517                 } else {
518                     pl->login_limit = int_value;
519                     pl->login_limit_def = source;
520                 }
521         }
522     }
523     return;
524 }
525
526 static int parse_config_file(pam_handle_t *pamh, const char *uname, int ctrl,
527                              struct pam_limit_s *pl)
528 {
529     FILE *fil;
530     char buf[LINE_LENGTH];
531
532     /* check for the LIMITS_FILE */
533     if (ctrl & PAM_DEBUG_ARG)
534         pam_syslog(pamh, LOG_DEBUG, "reading settings from '%s'", CONF_FILE);
535     fil = fopen(CONF_FILE, "r");
536     if (fil == NULL) {
537         pam_syslog (pamh, LOG_WARNING,
538                     "cannot read settings from %s: %m", CONF_FILE);
539         return PAM_SERVICE_ERR;
540     }
541
542     /* start the show */
543     while (fgets(buf, LINE_LENGTH, fil) != NULL) {
544         char domain[LINE_LENGTH];
545         char ltype[LINE_LENGTH];
546         char item[LINE_LENGTH];
547         char value[LINE_LENGTH];
548         int i;
549         size_t j;
550         char *tptr,*line;
551
552         line = buf;
553         /* skip the leading white space */
554         while (*line && isspace(*line))
555             line++;
556
557         /* Rip off the comments */
558         tptr = strchr(line,'#');
559         if (tptr)
560             *tptr = '\0';
561         /* Rip off the newline char */
562         tptr = strchr(line,'\n');
563         if (tptr)
564             *tptr = '\0';
565         /* Anything left ? */
566         if (!strlen(line))
567             continue;
568
569         domain[0] = ltype[0] = item[0] = value[0] = '\0';
570
571         i = sscanf(line,"%s%s%s%s", domain, ltype, item, value);
572         D(("scanned line[%d]: domain[%s], ltype[%s], item[%s], value[%s]",
573            i, domain, ltype, item, value));
574
575         for(j=0; j < strlen(ltype); j++)
576             ltype[j]=tolower(ltype[j]);
577
578         if (i == 4) { /* a complete line */
579             for(j=0; j < strlen(item); j++)
580                 item[j]=tolower(item[j]);
581             for(j=0; j < strlen(value); j++)
582                 value[j]=tolower(value[j]);
583
584             if (strcmp(uname, domain) == 0) /* this user have a limit */
585                 process_limit(pamh, LIMITS_DEF_USER, ltype, item, value, ctrl, pl);
586             else if (domain[0]=='@') {
587                     if (ctrl & PAM_DEBUG_ARG) {
588                         pam_syslog(pamh, LOG_DEBUG,
589                                    "checking if %s is in group %s",
590                                    uname, domain + 1);
591                     }
592                 if (pam_modutil_user_in_group_nam_nam(pamh, uname, domain+1))
593                     process_limit(pamh, LIMITS_DEF_GROUP, ltype, item, value, ctrl,
594                                   pl);
595             } else if (domain[0]=='%') {
596                     if (ctrl & PAM_DEBUG_ARG) {
597                         pam_syslog(pamh, LOG_DEBUG,
598                                    "checking if %s is in group %s",
599                                    uname, domain + 1);
600                     }
601                 if (strcmp(domain,"%") == 0)
602                     process_limit(pamh, LIMITS_DEF_ALL, ltype, item, value, ctrl,
603                                   pl);
604                 else if (pam_modutil_user_in_group_nam_nam(pamh, uname, domain+1)) {
605                     strcpy(pl->login_group, domain+1);
606                     process_limit(pamh, LIMITS_DEF_ALLGROUP, ltype, item, value, ctrl,
607                                   pl);
608                 }
609             } else if (strcmp(domain, "*") == 0)
610                 process_limit(pamh, LIMITS_DEF_DEFAULT, ltype, item, value, ctrl,
611                               pl);
612         } else if (i == 2 && ltype[0] == '-') { /* Probably a no-limit line */
613             if (strcmp(uname, domain) == 0) {
614                 if (ctrl & PAM_DEBUG_ARG) {
615                     pam_syslog(pamh, LOG_DEBUG, "no limits for '%s'", uname);
616                 }
617                 fclose(fil);
618                 return PAM_IGNORE;
619             } else if (domain[0] == '@' && pam_modutil_user_in_group_nam_nam(pamh, uname, domain+1)) {
620                 if (ctrl & PAM_DEBUG_ARG) {
621                     pam_syslog(pamh, LOG_DEBUG,
622                                "no limits for '%s' in group '%s'",
623                                uname, domain+1);
624                 }
625                 fclose(fil);
626                 return PAM_IGNORE;
627             }
628         } else {
629             pam_syslog(pamh, LOG_WARNING, "invalid line '%s' - skipped", line);
630         }
631     }
632     fclose(fil);
633     return PAM_SUCCESS;
634 }
635
636 static int setup_limits(pam_handle_t *pamh,
637                         const char *uname, uid_t uid, int ctrl,
638                         struct pam_limit_s *pl)
639 {
640     int i;
641     int status;
642     int retval = LIMITED_OK;
643
644     for (i=0, status=LIMITED_OK; i<RLIM_NLIMITS; i++) {
645       int res;
646
647         if (!pl->limits[i].supported) {
648             /* skip it if its not known to the system */
649             continue;
650         }
651         if (pl->limits[i].src_soft == LIMITS_DEF_NONE &&
652             pl->limits[i].src_hard == LIMITS_DEF_NONE) {
653             /* skip it if its not initialized */
654             continue;
655         }
656         if (pl->limits[i].limit.rlim_cur > pl->limits[i].limit.rlim_max)
657             pl->limits[i].limit.rlim_cur = pl->limits[i].limit.rlim_max;
658         res = setrlimit(i, &pl->limits[i].limit);
659         if (res != 0)
660           pam_syslog(pamh, LOG_ERR, "Could not set limit for '%s': %m",
661                      rlimit2str(i));
662         status |= res;
663     }
664
665     if (status) {
666         retval = LIMIT_ERR;
667     }
668
669     status = setpriority(PRIO_PROCESS, 0, pl->priority);
670     if (status != 0) {
671         pam_syslog(pamh, LOG_ERR, "Could not set limit for PRIO_PROCESS: %m");
672         retval = LIMIT_ERR;
673     }
674
675     if (uid == 0) {
676         D(("skip login limit check for uid=0"));
677     } else if (pl->login_limit > 0) {
678         if (check_logins(pamh, uname, pl->login_limit, ctrl, pl) == LOGIN_ERR) {
679 #ifdef HAVE_LIBAUDIT
680             if (!(ctrl & PAM_NO_AUDIT)) {
681                 pam_modutil_audit_write(pamh, AUDIT_ANOM_LOGIN_SESSIONS,
682                     "pam_limits", PAM_PERM_DENIED);
683                 /* ignore return value as we fail anyway */
684             }
685 #endif
686             retval |= LOGIN_ERR;
687         }
688     } else if (pl->login_limit == 0) {
689         retval |= LOGIN_ERR;
690     }
691
692     return retval;
693 }
694
695 /* now the session stuff */
696 PAM_EXTERN int
697 pam_sm_open_session (pam_handle_t *pamh, int flags UNUSED,
698                      int argc, const char **argv)
699 {
700     int retval;
701     int i;
702     int glob_rc;
703     char *user_name;
704     struct passwd *pwd;
705     int ctrl;
706     struct pam_limit_s plstruct;
707     struct pam_limit_s *pl = &plstruct;
708     glob_t globbuf;
709     const char *oldlocale;
710
711     D(("called."));
712
713     memset(pl, 0, sizeof(*pl));
714     memset(&globbuf, 0, sizeof(globbuf));
715
716     ctrl = _pam_parse(pamh, argc, argv, pl);
717     retval = pam_get_item( pamh, PAM_USER, (void*) &user_name );
718     if ( user_name == NULL || retval != PAM_SUCCESS ) {
719         pam_syslog(pamh, LOG_CRIT, "open_session - error recovering username");
720         return PAM_SESSION_ERR;
721      }
722
723     pwd = pam_modutil_getpwnam(pamh, user_name);
724     if (!pwd) {
725         if (ctrl & PAM_DEBUG_ARG)
726             pam_syslog(pamh, LOG_WARNING,
727                        "open_session username '%s' does not exist", user_name);
728         return PAM_USER_UNKNOWN;
729     }
730
731     retval = init_limits(pl);
732     if (retval != PAM_SUCCESS) {
733         pam_syslog(pamh, LOG_WARNING, "cannot initialize");
734         return PAM_ABORT;
735     }
736
737     retval = parse_config_file(pamh, pwd->pw_name, ctrl, pl);
738     if (retval == PAM_IGNORE) {
739         D(("the configuration file ('%s') has an applicable '<domain> -' entry", CONF_FILE));
740         return PAM_SUCCESS;
741     }
742     if (retval != PAM_SUCCESS || pl->conf_file != NULL)
743         /* skip reading limits.d if config file explicitely specified */
744         goto out;
745
746     /* Read subsequent *.conf files, if they exist. */
747
748     /* set the LC_COLLATE so the sorting order doesn't depend
749         on system locale */
750
751     oldlocale = setlocale(LC_COLLATE, "C");
752     glob_rc = glob(LIMITS_CONF_GLOB, GLOB_ERR, NULL, &globbuf);
753
754     if (oldlocale != NULL)
755         setlocale (LC_COLLATE, oldlocale);
756
757     if (!glob_rc) {
758         /* Parse the *.conf files. */
759         for (i = 0; globbuf.gl_pathv[i] != NULL; i++) {
760             pl->conf_file = globbuf.gl_pathv[i];
761             retval = parse_config_file(pamh, pwd->pw_name, ctrl, pl);
762             if (retval == PAM_IGNORE) {
763                 D(("the configuration file ('%s') has an applicable '<domain> -' entry", pl->conf_file));
764                 globfree(&globbuf);
765                 return PAM_SUCCESS;
766             }
767             if (retval != PAM_SUCCESS)
768                 goto out;
769         }
770     }
771
772 out:
773     globfree(&globbuf);
774     if (retval != PAM_SUCCESS)
775     {
776         pam_syslog(pamh, LOG_WARNING, "error parsing the configuration file: '%s' ",CONF_FILE);
777         return retval;
778     }
779
780     if (ctrl & PAM_DO_SETREUID) {
781         setreuid(pwd->pw_uid, -1);
782     }
783
784     retval = setup_limits(pamh, pwd->pw_name, pwd->pw_uid, ctrl, pl);
785     if (retval & LOGIN_ERR)
786         pam_error(pamh, _("Too many logins for '%s'."), pwd->pw_name);
787     if (retval != LIMITED_OK) {
788         return PAM_PERM_DENIED;
789     }
790
791     return PAM_SUCCESS;
792 }
793
794 PAM_EXTERN int
795 pam_sm_close_session (pam_handle_t *pamh UNUSED, int flags UNUSED,
796                       int argc UNUSED, const char **argv UNUSED)
797 {
798      /* nothing to do */
799      return PAM_SUCCESS;
800 }
801
802 #ifdef PAM_STATIC
803
804 /* static module data */
805
806 struct pam_module _pam_limits_modstruct = {
807      "pam_limits",
808      NULL,
809      NULL,
810      NULL,
811      pam_sm_open_session,
812      pam_sm_close_session,
813      NULL
814 };
815 #endif
816
817 /*
818  * Copyright (c) Cristian Gafton, 1996-1997, <gafton@redhat.com>
819  *                                              All rights reserved.
820  *
821  * Redistribution and use in source and binary forms, with or without
822  * modification, are permitted provided that the following conditions
823  * are met:
824  * 1. Redistributions of source code must retain the above copyright
825  *    notice, and the entire permission notice in its entirety,
826  *    including the disclaimer of warranties.
827  * 2. Redistributions in binary form must reproduce the above copyright
828  *    notice, this list of conditions and the following disclaimer in the
829  *    documentation and/or other materials provided with the distribution.
830  * 3. The name of the author may not be used to endorse or promote
831  *    products derived from this software without specific prior
832  *    written permission.
833  *
834  * ALTERNATIVELY, this product may be distributed under the terms of
835  * the GNU Public License, in which case the provisions of the GPL are
836  * required INSTEAD OF the above restrictions.  (This clause is
837  * necessary due to a potential bad interaction between the GPL and
838  * the restrictions contained in a BSD-style copyright.)
839  *
840  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
841  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
842  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
843  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
844  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
845  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
846  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
847  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
848  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
849  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
850  * OF THE POSSIBILITY OF SUCH DAMAGE.
851  */