2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1996 - 2001 Brian Somers <brian@Awfulhak.org>
5 * based on work by Toshiharu OHNO <tony-o@iij.ad.jp>
6 * Internet Initiative Japan, Inc (IIJ)
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/param.h>
48 #define issep(ch) ((ch) == ' ' || (ch) == '\t')
51 OpenSecret(const char *file)
56 snprintf(line, sizeof line, "%s/%s", PPP_CONFDIR, file);
57 fp = ID0fopen(line, "r");
59 log_Printf(LogWARN, "OpenSecret: Can't open %s.\n", line);
69 /* Move string from ``from'' to ``to'', interpreting ``~'' and $.... */
71 InterpretArg(const char *from, char *to)
73 char *ptr, *startto, *endto;
81 endto = to + LINE_LEN - 1;
86 while (*from != '\0') {
96 break; /* Swallow the escapes */
99 *to++ = '\\'; /* Pass the escapes on, maybe skipping \# */
105 if (from[1] == '$') {
106 *to = '\0'; /* For an empty var name below */
108 } else if (from[1] == '{') {
109 ptr = strchr(from+2, '}');
111 len = ptr - from - 2;
112 if (endto - to < (int)len )
115 strncpy(to, from+2, len);
128 for (from++; (isalnum(*from) || *from == '_') && ptr < endto; from++)
134 else if ((env = getenv(to)) != NULL) {
135 strncpy(to, env, endto - to);
142 ptr = strchr(++from, '/');
143 len = ptr ? (size_t)(ptr - from) : strlen(from);
145 pwd = getpwuid(ID0realuid());
147 strncpy(to, from, len);
154 strncpy(to, pwd->pw_dir, endto - to);
168 while (to > startto) {
180 #define CTRL_UNKNOWN (0)
181 #define CTRL_INCLUDE (1)
184 DecodeCtrlCommand(char *line, char *arg)
188 if (!strncasecmp(line, "include", 7) && issep(line[7])) {
189 end = InterpretArg(line+8, arg);
190 if (*end && *end != '#')
191 log_Printf(LogWARN, "usage: !include filename\n");
199 * Initialised in system_IsValid(), set in ReadSystem(),
200 * used by system_IsValid()
207 AllowUsers(struct cmdargs const *arg)
209 /* arg->bundle may be NULL (see system_IsValid()) ! */
216 pwd = getpwuid(ID0realuid());
218 for (f = arg->argn; f < arg->argc; f++)
219 if (!strcmp("*", arg->argv[f]) || !strcmp(pwd->pw_name, arg->argv[f])) {
229 AllowModes(struct cmdargs const *arg)
231 /* arg->bundle may be NULL (see system_IsValid()) ! */
232 int f, mode, allowed;
235 for (f = arg->argn; f < arg->argc; f++) {
236 mode = Nam2mode(arg->argv[f]);
237 if (mode == PHYS_NONE || mode == PHYS_ALL)
238 log_Printf(LogWARN, "allow modes: %s: Invalid mode\n", arg->argv[f]);
243 modeok = modereq & allowed ? 1 : 0;
253 while (len && (line[len-1] == '\n' || line[len-1] == '\r' ||
267 xgets(char *buf, int buflen, FILE *fp)
272 while (fgets(buf, buflen-1, fp)) {
274 buf[buflen-1] = '\0';
276 while (len && (buf[len-1] == '\n' || buf[len-1] == '\r'))
278 if (len && buf[len-1] == '\\') {
281 if (!buflen) /* No buffer space */
289 /* Values for ``how'' in ReadSystem */
290 #define SYSTEM_EXISTS 1
291 #define SYSTEM_VALIDATE 2
292 #define SYSTEM_EXEC 3
295 GetLabel(char *line, const char *filename, int linenum)
300 argc = MakeArgs(line, argv, MAXARGS, PARSE_REDUCE);
302 if (argc == 2 && !strcmp(argv[1], ":"))
305 if (argc != 1 || (len = strlen(argv[0])) < 2 || argv[0][len-1] != ':') {
306 log_Printf(LogWARN, "Bad label in %s (line %d) - missing colon\n",
310 argv[0][len-1] = '\0'; /* Lose the ':' */
315 /* Returns -2 for ``file not found'' and -1 for ``label not found'' */
318 ReadSystem(struct bundle *bundle, const char *name, const char *file,
319 struct prompt *prompt, struct datalink *cx, int how)
325 char filename[PATH_MAX];
335 snprintf(filename, sizeof filename, "%s", file);
337 snprintf(filename, sizeof filename, "%s/%s", PPP_CONFDIR, file);
338 fp = ID0fopen(filename, "r");
340 log_Printf(LogDEBUG, "ReadSystem: Can't open %s.\n", filename);
343 log_Printf(LogDEBUG, "ReadSystem: Checking %s (%s).\n", name, filename);
346 while ((n = xgets(line, sizeof line, fp))) {
354 case '\0': /* empty/comment */
358 switch (DecodeCtrlCommand(cp+1, arg)) {
360 log_Printf(LogCOMMAND, "%s: Including \"%s\"\n", filename, arg);
361 n = ReadSystem(bundle, name, arg, prompt, cx, how);
362 log_Printf(LogCOMMAND, "%s: Done include of \"%s\"\n", filename, arg);
365 return 0; /* got it */
369 log_Printf(LogWARN, "%s: %s: Invalid command\n", filename, cp);
375 if ((cp = GetLabel(cp, filename, linenum)) == NULL)
378 if (strcmp(cp, name) == 0) {
379 /* We're in business */
380 if (how == SYSTEM_EXISTS) {
384 while ((n = xgets(line, sizeof line, fp))) {
386 indent = issep(*line);
389 if (*cp == '\0') /* empty / comment */
392 if (!indent) { /* start of next section */
393 if (*cp != '!' && how == SYSTEM_EXEC)
394 cp = GetLabel(cp, filename, linenum);
399 if ((argc = command_Expand_Interpret(cp, len, argv, cp - line)) < 0)
400 log_Printf(LogWARN, "%s: %d: Syntax error\n", filename, linenum);
402 allowcmd = argc > 0 && !strcasecmp(argv[0], "allow");
403 if ((how != SYSTEM_EXEC && allowcmd) ||
404 (how == SYSTEM_EXEC && !allowcmd)) {
406 * Disable any context so that warnings are given to everyone,
409 op = log_PromptContext;
410 log_PromptContext = NULL;
411 command_Run(bundle, argc, (char const *const *)argv, prompt,
413 log_PromptContext = op;
418 fclose(fp); /* everything read - get out */
429 system_IsValid(const char *name, struct prompt *prompt, int mode)
432 * Note: The ReadSystem() calls only result in calls to the Allow*
433 * functions. arg->bundle will be set to NULL for these commands !
438 def = !strcmp(name, "default");
439 how = ID0realuid() == 0 ? SYSTEM_EXISTS : SYSTEM_VALIDATE;
444 rs = ReadSystem(NULL, "default", CONFFILE, prompt, NULL, how);
451 rs = 0; /* we don't care that ``default'' doesn't exist */
454 rs = ReadSystem(NULL, name, CONFFILE, prompt, NULL, how);
457 return "Configuration label not found";
460 return PPP_CONFDIR "/" CONFFILE " : File not found";
466 if (how == SYSTEM_EXISTS)
470 return "User access denied";
473 return "Mode denied for this label";
479 system_Select(struct bundle *bundle, const char *name, const char *file,
480 struct prompt *prompt, struct datalink *cx)
484 return ReadSystem(bundle, name, file, prompt, cx, SYSTEM_EXEC);