1178825Sdfr/* 2233294Sstas * Copyright (c) 2005 Kungliga Tekniska H��gskolan 3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden). 4233294Sstas * All rights reserved. 5178825Sdfr * 6233294Sstas * Redistribution and use in source and binary forms, with or without 7233294Sstas * modification, are permitted provided that the following conditions 8233294Sstas * are met: 9178825Sdfr * 10233294Sstas * 1. Redistributions of source code must retain the above copyright 11233294Sstas * notice, this list of conditions and the following disclaimer. 12178825Sdfr * 13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 14233294Sstas * notice, this list of conditions and the following disclaimer in the 15233294Sstas * documentation and/or other materials provided with the distribution. 16178825Sdfr * 17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors 18233294Sstas * may be used to endorse or promote products derived from this software 19233294Sstas * without specific prior written permission. 20178825Sdfr * 21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24233294Sstas * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31233294Sstas * SUCH DAMAGE. 32178825Sdfr */ 33178825Sdfr 34178825Sdfr#include "login_locl.h" 35178825Sdfr 36233294SstasRCSID("$Id$"); 37178825Sdfr 38178825Sdfr#include <errno.h> 39178825Sdfr#include <limits.h> 40178825Sdfr#ifdef HAVE_SYS_RESOURCE_H 41178825Sdfr#include <sys/resource.h> 42178825Sdfr#endif 43178825Sdfr 44178825Sdfrstruct limit { 45178825Sdfr const char *name; 46178825Sdfr int resource; 47178825Sdfr int scale; 48178825Sdfr int has_limit; 49178825Sdfr struct rlimit limit; 50178825Sdfr} limits[] = { 51178825Sdfr#define LIM(X, S) { #X, RLIMIT_##X, S, 0 } 52178825Sdfr LIM(CORE, 1024), 53178825Sdfr LIM(CPU, 60), 54178825Sdfr LIM(DATA, 1024), 55178825Sdfr LIM(FSIZE, 1024), 56178825Sdfr#ifdef RLIMIT_MEMLOCK 57178825Sdfr LIM(MEMLOCK, 1024), 58178825Sdfr#endif 59178825Sdfr LIM(NOFILE, 1), 60178825Sdfr#ifdef RLIMIT_NPROC 61178825Sdfr LIM(NPROC, 1), 62178825Sdfr#endif 63178825Sdfr#ifdef RLIMIT_RSS 64178825Sdfr LIM(RSS, 1024), 65178825Sdfr#endif 66178825Sdfr LIM(STACK, 1024), 67178825Sdfr 68178825Sdfr#ifdef RLIMIT_AS 69178825Sdfr LIM(AS, 1024), 70178825Sdfr#endif 71178825Sdfr#ifdef RLIMIT_LOCKS 72178825Sdfr LIM(LOCKS, 1), 73178825Sdfr#endif 74178825Sdfr /* 75178825Sdfr maxlogins 76178825Sdfr priority 77178825Sdfr */ 78178825Sdfr { NULL, 0 } 79178825Sdfr}; 80178825Sdfr 81178825Sdfrstatic struct limit * 82178825Sdfrfind_limit(const char *name) 83178825Sdfr{ 84178825Sdfr struct limit *l; 85178825Sdfr for(l = limits; l->name != NULL; l++) 86178825Sdfr if(strcasecmp(name, l->name) == 0) 87178825Sdfr return l; 88178825Sdfr return NULL; 89178825Sdfr} 90178825Sdfr 91178825Sdfr/* this function reads limits.conf files similar to pam_limits 92178825Sdfr unimplemented features include: 93178825Sdfr % maxlogins 94233294Sstas "-" no limits, 95178825Sdfr priorities etc that are not set via setrlimit 96178825Sdfr XXX uses static storage, and clobbers getgr* 97178825Sdfr*/ 98178825Sdfr 99178825Sdfrint 100178825Sdfrread_limits_conf(const char *file, const struct passwd *pwd) 101178825Sdfr{ 102178825Sdfr FILE *f; 103178825Sdfr char *args[4]; 104178825Sdfr int lineno = 0; 105178825Sdfr char buf[1024]; 106178825Sdfr struct limit *l; 107178825Sdfr rlim_t value; 108178825Sdfr 109178825Sdfr f = fopen(file, "r"); 110178825Sdfr if(f == NULL) { 111178825Sdfr if(errno != ENOENT && errno != ENOTDIR) 112178825Sdfr syslog(LOG_ERR, "%s: %m", file); 113178825Sdfr return -1; 114178825Sdfr } 115178825Sdfr 116178825Sdfr while(fgets(buf, sizeof(buf), f) != NULL) { 117178825Sdfr char *last = NULL; 118178825Sdfr char *end = NULL; 119178825Sdfr int level; 120178825Sdfr 121178825Sdfr lineno++; 122178825Sdfr 123178825Sdfr if(buf[0] == '\0') { 124178825Sdfr syslog(LOG_ERR, "%s: line %d: NUL character", file, lineno); 125178825Sdfr continue; 126178825Sdfr } 127178825Sdfr if(buf[strlen(buf) - 1] != '\n') { 128178825Sdfr /* file did not end with a newline, figure out if we're at 129178825Sdfr the EOF, or if our buffer was too small */ 130178825Sdfr int eof = 1; 131178825Sdfr int c; 132178825Sdfr while((c = fgetc(f)) != EOF) { 133178825Sdfr eof = 0; 134233294Sstas if(c == '\n') 135178825Sdfr break; 136178825Sdfr } 137178825Sdfr if(!eof) { 138178825Sdfr syslog(LOG_ERR, "%s: line %d: line too long", file, lineno); 139178825Sdfr continue; 140178825Sdfr } 141178825Sdfr } 142178825Sdfr buf[strcspn(buf, "#\r\n")] = '\0'; 143178825Sdfr if((args[0] = strtok_r(buf, " \t", &last)) == NULL || 144178825Sdfr (args[1] = strtok_r(NULL, " \t", &last)) == NULL || 145178825Sdfr (args[2] = strtok_r(NULL, " \t", &last)) == NULL || 146178825Sdfr (args[3] = strtok_r(NULL, " \t", &last)) == NULL) { 147178825Sdfr if(args[0] != NULL) /* this would include comment lines */ 148178825Sdfr syslog(LOG_ERR, "%s: line %d: malformed line", file, lineno); 149178825Sdfr continue; 150178825Sdfr } 151178825Sdfr 152178825Sdfr l = find_limit(args[2]); 153178825Sdfr if(l == NULL) { 154178825Sdfr syslog(LOG_ERR, "%s: line %d: unknown limit %s", file, lineno, args[2]); 155178825Sdfr continue; 156178825Sdfr } 157178825Sdfr if(strcmp(args[3], "-") == 0) { 158178825Sdfr value = RLIM_INFINITY; 159178825Sdfr } else { 160178825Sdfr errno = 0; 161178825Sdfr value = strtol(args[3], &end, 10); 162178825Sdfr if(*end != '\0') { 163178825Sdfr syslog(LOG_ERR, "%s: line %d: bad value %s", file, lineno, args[3]); 164178825Sdfr continue; 165178825Sdfr } 166178825Sdfr if((value == LONG_MIN || value == LONG_MAX) && errno == ERANGE) { 167178825Sdfr syslog(LOG_ERR, "%s: line %d: bad value %s", file, lineno, args[3]); 168178825Sdfr continue; 169178825Sdfr } 170178825Sdfr if(value * l->scale < value) 171178825Sdfr value = RLIM_INFINITY; 172178825Sdfr else 173178825Sdfr value *= l->scale; 174178825Sdfr } 175178825Sdfr level = 0; 176178825Sdfr /* XXX unclear: if you set group hard and user soft limit, 177178825Sdfr should the hard limit still apply? this code doesn't. */ 178178825Sdfr if(strcmp(args[0], pwd->pw_name) == 0) 179178825Sdfr level = 3; 180178825Sdfr if(*args[0] == '@') { 181178825Sdfr struct group *gr; 182178825Sdfr gr = getgrnam(args[0] + 1); 183178825Sdfr if(gr != NULL && gr->gr_gid == pwd->pw_gid) 184178825Sdfr level = 2; 185178825Sdfr } 186178825Sdfr if(strcmp(args[0], "*") == 0) 187178825Sdfr level = 1; 188178825Sdfr if(level == 0 || level < l->has_limit) /* not for us */ 189178825Sdfr continue; 190178825Sdfr if(l->has_limit < level) { 191178825Sdfr if(getrlimit(l->resource, &l->limit) < 0) 192178825Sdfr continue; 193178825Sdfr l->has_limit = level; 194178825Sdfr } 195233294Sstas 196178825Sdfr /* XXX unclear: if you soft to more than default hard, should 197178825Sdfr we set hard to soft? this code doesn't. */ 198178825Sdfr if(strcasecmp(args[1], "soft") == 0 || strcmp(args[1], "-") == 0) 199178825Sdfr l->limit.rlim_cur = value; 200233294Sstas if(strcasecmp(args[1], "hard") == 0 || strcmp(args[1], "-") == 0) 201178825Sdfr l->limit.rlim_max = value; 202178825Sdfr } 203178825Sdfr fclose(f); 204178825Sdfr for(l = limits; l->name != NULL; l++) { 205178825Sdfr if(l->has_limit) { 206178825Sdfr if(l->limit.rlim_cur > l->limit.rlim_max) 207178825Sdfr l->limit.rlim_cur = l->limit.rlim_max; 208178825Sdfr if(setrlimit(l->resource, &l->limit) != 0) 209178825Sdfr syslog(LOG_ERR, "setrlimit RLIM_%s failed: %m", l->name); 210178825Sdfr } 211178825Sdfr l->has_limit = 0; 212178825Sdfr } 213178825Sdfr return 0; 214178825Sdfr} 215