add doc about interaction with RAMRUN to README.Debian in response to #581393
[debian/sudo] / aix.c
1 /*
2  * Copyright (c) 2008 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20 #include <sys/resource.h>
21
22 #include <stdio.h>
23 #ifdef STDC_HEADERS
24 # include <stdlib.h>
25 # include <stddef.h>
26 #else
27 # ifdef HAVE_STDLIB_H
28 #  include <stdlib.h>
29 # endif
30 #endif /* STDC_HEADERS */
31 #include <usersec.h>
32
33 #include <compat.h>
34
35 #ifdef HAVE_GETUSERATTR
36
37 #ifndef HAVE_SETRLIMIT64
38 # define setrlimit64(a, b) setrlimit(a, b)
39 # define rlimit64 rlimit
40 # define rlim64_t rlim_t
41 # define RLIM64_INFINITY RLIM_INFINITY
42 #endif /* HAVE_SETRLIMIT64 */
43
44 #ifndef RLIM_SAVED_MAX
45 # define RLIM_SAVED_MAX RLIM64_INFINITY
46 #endif
47
48 struct aix_limit {
49     int resource;
50     char *soft;
51     char *hard;
52     int factor;
53 };
54
55 static struct aix_limit aix_limits[] = {
56     { RLIMIT_FSIZE, S_UFSIZE, S_UFSIZE_HARD, 512 },
57     { RLIMIT_CPU, S_UCPU, S_UCPU_HARD, 1 },
58     { RLIMIT_DATA, S_UDATA, S_UDATA_HARD, 512 },
59     { RLIMIT_STACK, S_USTACK, S_USTACK_HARD, 512 },
60     { RLIMIT_RSS, S_URSS, S_URSS_HARD, 512 },
61     { RLIMIT_CORE, S_UCORE, S_UCORE_HARD, 512 },
62     { RLIMIT_NOFILE, S_UNOFILE, S_UNOFILE_HARD, 1 }
63 };
64
65 static int
66 aix_getlimit(user, lim, valp)
67     char *user;
68     char *lim;
69     rlim64_t *valp;
70 {
71     int val;
72
73     if (getuserattr(user, lim, &val, SEC_INT) != 0 &&
74         getuserattr("default", lim, &val, SEC_INT) != 0) {
75         return(-1);
76     }
77     *valp = val;
78     return(0);
79 }
80
81 void
82 aix_setlimits(user)
83     char *user;
84 {
85     struct rlimit64 rlim;
86     rlim64_t val;
87     int n;
88
89     /*
90      * For each resource limit, get the soft/hard values for the user
91      * and set those values via setrlimit64().  Must be run as euid 0.
92      */
93     for (n = 0; n < sizeof(aix_limits) / sizeof(aix_limits[0]); n++) {
94         /*
95          * We have two strategies, depending on whether or not the
96          * hard limit has been defined.
97          */
98         if (aix_getlimit(user, aix_limits[n].hard, &val) == 0) {
99             rlim.rlim_max = val == -1 ? RLIM64_INFINITY : val * aix_limits[n].factor;
100             if (aix_getlimit(user, aix_limits[n].soft, &val) == 0)
101                 rlim.rlim_cur = val == -1 ? RLIM64_INFINITY : val * aix_limits[n].factor;
102             else
103                 rlim.rlim_cur = rlim.rlim_max;  /* soft not specd, use hard */
104         } else {
105             /* No hard limit set, try soft limit. */
106             if (aix_getlimit(user, aix_limits[n].soft, &val) == 0)
107                 rlim.rlim_cur = val == -1 ? RLIM64_INFINITY : val * aix_limits[n].factor;
108
109             /* Set hard limit per AIX /etc/security/limits documentation. */
110             switch (aix_limits[n].resource) {
111                 case RLIMIT_CPU:
112                 case RLIMIT_FSIZE:
113                     rlim.rlim_max = rlim.rlim_cur;
114                     break;
115                 case RLIMIT_STACK:
116                     rlim.rlim_max = RLIM_SAVED_MAX;
117                     break;
118                 default:
119                     rlim.rlim_max = RLIM64_INFINITY;
120                     break;
121             }
122         }
123         (void)setrlimit64(aix_limits[n].resource, &rlim);
124     }
125 }
126
127 #endif /* HAVE_GETUSERATTR */