switch from rcS.d to rc[0-6].d
[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 #ifndef lint
36 __unused static const char rcsid[] = "$Sudo: aix.c,v 1.7 2008/11/06 00:42:37 millert Exp $";
37 #endif /* lint */
38
39 #ifdef HAVE_GETUSERATTR
40
41 #ifndef RLIM_SAVED_MAX
42 # define RLIM_SAVED_MAX RLIM_INFINITY
43 #endif
44
45 struct aix_limit {
46     int resource;
47     char *soft;
48     char *hard;
49     int factor;
50 };
51
52 static struct aix_limit aix_limits[] = {
53     { RLIMIT_FSIZE, S_UFSIZE, S_UFSIZE_HARD, 512 },
54     { RLIMIT_CPU, S_UCPU, S_UCPU_HARD, 1 },
55     { RLIMIT_DATA, S_UDATA, S_UDATA_HARD, 512 },
56     { RLIMIT_STACK, S_USTACK, S_USTACK_HARD, 512 },
57     { RLIMIT_RSS, S_URSS, S_URSS_HARD, 512 },
58     { RLIMIT_CORE, S_UCORE, S_UCORE_HARD, 512 },
59     { RLIMIT_NOFILE, S_UNOFILE, S_UNOFILE_HARD, 1 }
60 };
61
62 static int
63 aix_getlimit(user, lim, valp)
64     char *user;
65     char *lim;
66     int *valp;
67 {
68     if (getuserattr(user, lim, valp, SEC_INT) != 0)
69         return getuserattr("default", lim, valp, SEC_INT);
70     return(0);
71 }
72
73 void
74 aix_setlimits(user)
75     char *user;
76 {
77     struct rlimit rlim;
78     int i, n;
79
80     /*
81      * For each resource limit, get the soft/hard values for the user
82      * and set those values via setrlimit().  Must be run as euid 0.
83      */
84     for (n = 0; n < sizeof(aix_limits) / sizeof(aix_limits[0]); n++) {
85         /*
86          * We have two strategies, depending on whether or not the
87          * hard limit has been defined.
88          */
89         if (aix_getlimit(user, aix_limits[n].hard, &i) == 0) {
90             rlim.rlim_max = i == -1 ? RLIM_INFINITY : i * aix_limits[n].factor;
91             if (aix_getlimit(user, aix_limits[n].soft, &i) == 0)
92                 rlim.rlim_cur = i == -1 ? RLIM_INFINITY : i * aix_limits[n].factor;
93             else
94                 rlim.rlim_cur = rlim.rlim_max;  /* soft not specd, use hard */
95         } else {
96             /* No hard limit set, try soft limit. */
97             if (aix_getlimit(user, aix_limits[n].soft, &i) == 0)
98                 rlim.rlim_cur = i == -1 ? RLIM_INFINITY : i * aix_limits[n].factor;
99
100             /* Set hard limit per AIX /etc/security/limits documentation. */
101             switch (aix_limits[n].resource) {
102                 case RLIMIT_CPU:
103                 case RLIMIT_FSIZE:
104                     rlim.rlim_max = rlim.rlim_cur;
105                     break;
106                 case RLIMIT_STACK:
107                     rlim.rlim_max = RLIM_SAVED_MAX;
108                     break;
109                 default:
110                     rlim.rlim_max = RLIM_INFINITY;
111                     break;
112             }
113         }
114         (void)setrlimit(aix_limits[n].resource, &rlim);
115     }
116 }
117
118 #endif /* HAVE_GETUSERATTR */