Path: blob/main/crypto/heimdal/appl/rsh/limits_conf.c
34878 views
/*1* Copyright (c) 2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "login_locl.h"3435RCSID("$Id$");3637#include <errno.h>38#include <limits.h>39#ifdef HAVE_SYS_RESOURCE_H40#include <sys/resource.h>41#endif4243struct limit {44const char *name;45int resource;46int scale;47int has_limit;48struct rlimit limit;49} limits[] = {50#define LIM(X, S) { #X, RLIMIT_##X, S, 0 }51LIM(CORE, 1024),52LIM(CPU, 60),53LIM(DATA, 1024),54LIM(FSIZE, 1024),55#ifdef RLIMIT_MEMLOCK56LIM(MEMLOCK, 1024),57#endif58LIM(NOFILE, 1),59#ifdef RLIMIT_NPROC60LIM(NPROC, 1),61#endif62#ifdef RLIMIT_RSS63LIM(RSS, 1024),64#endif65LIM(STACK, 1024),6667#ifdef RLIMIT_AS68LIM(AS, 1024),69#endif70#ifdef RLIMIT_LOCKS71LIM(LOCKS, 1),72#endif73/*74maxlogins75priority76*/77{ NULL, 0 }78};7980static struct limit *81find_limit(const char *name)82{83struct limit *l;84for(l = limits; l->name != NULL; l++)85if(strcasecmp(name, l->name) == 0)86return l;87return NULL;88}8990/* this function reads limits.conf files similar to pam_limits91unimplemented features include:92% maxlogins93"-" no limits,94priorities etc that are not set via setrlimit95XXX uses static storage, and clobbers getgr*96*/9798int99read_limits_conf(const char *file, const struct passwd *pwd)100{101FILE *f;102char *args[4];103int lineno = 0;104char buf[1024];105struct limit *l;106rlim_t value;107108f = fopen(file, "r");109if(f == NULL) {110if(errno != ENOENT && errno != ENOTDIR)111syslog(LOG_ERR, "%s: %m", file);112return -1;113}114115while(fgets(buf, sizeof(buf), f) != NULL) {116char *last = NULL;117char *end = NULL;118int level;119120lineno++;121122if(buf[0] == '\0') {123syslog(LOG_ERR, "%s: line %d: NUL character", file, lineno);124continue;125}126if(buf[strlen(buf) - 1] != '\n') {127/* file did not end with a newline, figure out if we're at128the EOF, or if our buffer was too small */129int eof = 1;130int c;131while((c = fgetc(f)) != EOF) {132eof = 0;133if(c == '\n')134break;135}136if(!eof) {137syslog(LOG_ERR, "%s: line %d: line too long", file, lineno);138continue;139}140}141buf[strcspn(buf, "#\r\n")] = '\0';142if((args[0] = strtok_r(buf, " \t", &last)) == NULL ||143(args[1] = strtok_r(NULL, " \t", &last)) == NULL ||144(args[2] = strtok_r(NULL, " \t", &last)) == NULL ||145(args[3] = strtok_r(NULL, " \t", &last)) == NULL) {146if(args[0] != NULL) /* this would include comment lines */147syslog(LOG_ERR, "%s: line %d: malformed line", file, lineno);148continue;149}150151l = find_limit(args[2]);152if(l == NULL) {153syslog(LOG_ERR, "%s: line %d: unknown limit %s", file, lineno, args[2]);154continue;155}156if(strcmp(args[3], "-") == 0) {157value = RLIM_INFINITY;158} else {159errno = 0;160value = strtol(args[3], &end, 10);161if(*end != '\0') {162syslog(LOG_ERR, "%s: line %d: bad value %s", file, lineno, args[3]);163continue;164}165if((value == LONG_MIN || value == LONG_MAX) && errno == ERANGE) {166syslog(LOG_ERR, "%s: line %d: bad value %s", file, lineno, args[3]);167continue;168}169if(value * l->scale < value)170value = RLIM_INFINITY;171else172value *= l->scale;173}174level = 0;175/* XXX unclear: if you set group hard and user soft limit,176should the hard limit still apply? this code doesn't. */177if(strcmp(args[0], pwd->pw_name) == 0)178level = 3;179if(*args[0] == '@') {180struct group *gr;181gr = getgrnam(args[0] + 1);182if(gr != NULL && gr->gr_gid == pwd->pw_gid)183level = 2;184}185if(strcmp(args[0], "*") == 0)186level = 1;187if(level == 0 || level < l->has_limit) /* not for us */188continue;189if(l->has_limit < level) {190if(getrlimit(l->resource, &l->limit) < 0)191continue;192l->has_limit = level;193}194195/* XXX unclear: if you soft to more than default hard, should196we set hard to soft? this code doesn't. */197if(strcasecmp(args[1], "soft") == 0 || strcmp(args[1], "-") == 0)198l->limit.rlim_cur = value;199if(strcasecmp(args[1], "hard") == 0 || strcmp(args[1], "-") == 0)200l->limit.rlim_max = value;201}202fclose(f);203for(l = limits; l->name != NULL; l++) {204if(l->has_limit) {205if(l->limit.rlim_cur > l->limit.rlim_max)206l->limit.rlim_cur = l->limit.rlim_max;207if(setrlimit(l->resource, &l->limit) != 0)208syslog(LOG_ERR, "setrlimit RLIM_%s failed: %m", l->name);209}210l->has_limit = 0;211}212return 0;213}214215216