systems.c revision 26516
1/*
2 *	          System configuration routines
3 *
4 *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5 *
6 *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7 *
8 * Redistribution and use in source and binary forms are permitted
9 * provided that the above copyright notice and this paragraph are
10 * duplicated in all such forms and that any documentation,
11 * advertising materials, and other materials related to such
12 * distribution and use acknowledge that the software was developed
13 * by the Internet Initiative Japan, Inc.  The name of the
14 * IIJ may not be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * $Id: systems.c,v 1.11 1997/05/26 00:44:09 brian Exp $
21 *
22 *  TODO:
23 */
24#include "fsm.h"
25#include "loadalias.h"
26#include "vars.h"
27#include "ipcp.h"
28#include "pathnames.h"
29#include "vars.h"
30
31extern void DecodeCommand();
32
33static int uid, gid;
34static int euid, egid;
35static int usermode;
36
37int
38OrigUid()
39{
40    return uid;
41}
42
43void
44GetUid()
45{
46  uid = getuid();
47  gid = getgid();
48  euid = geteuid();
49  egid = getegid();
50  usermode = 0;
51}
52
53static void
54SetUserId()
55{
56  if (!usermode) {
57    if (setreuid(euid, uid) == -1) {
58      LogPrintf(LogERROR, "unable to setreuid!\n");
59      exit(1);
60    }
61    if (setregid(egid, gid) == -1) {
62      LogPrintf(LogERROR, "unable to setregid!\n");
63      exit(1);
64    }
65    usermode = 1;
66  }
67}
68
69static void
70SetPppId()
71{
72  if (usermode) {
73    if (setreuid(uid, euid) == -1) {
74      LogPrintf(LogERROR, "unable to setreuid!\n");
75      exit(1);
76    }
77    if (setregid(gid, egid) == -1) {
78      LogPrintf(LogERROR, "unable to setregid!\n");
79      exit(1);
80    }
81    usermode = 0;
82  }
83}
84
85FILE *
86OpenSecret(file)
87char *file;
88{
89  FILE *fp;
90  char *cp;
91  char line[100];
92
93  fp = NULL;
94  cp = getenv("HOME");
95  if (cp) {
96    SetUserId();
97    snprintf(line, sizeof line, "%s/.%s", cp, file);
98    fp = fopen(line, "r");
99  }
100  if (fp == NULL) {
101    SetPppId();
102    snprintf(line, sizeof line, "%s/%s", _PATH_PPP, file);
103    fp = fopen(line, "r");
104  }
105  if (fp == NULL) {
106    LogPrintf(LogWARN, "OpenSecret: Can't open %s.\n", line);
107    SetPppId();
108    return(NULL);
109  }
110  return(fp);
111}
112
113void
114CloseSecret(fp)
115FILE *fp;
116{
117  fclose(fp);
118  SetPppId();
119}
120
121int
122SelectSystem(name, file)
123char *name;
124char *file;
125{
126  FILE *fp;
127  char *cp, *wp;
128  int n;
129  u_char  olauth;
130  char line[200];
131  char filename[200];
132  int linenum;
133
134  fp = NULL;
135  cp = getenv("HOME");
136  if (cp) {
137    SetUserId();
138    snprintf(filename, sizeof filename, "%s/.%s", cp, file);
139    fp = fopen(filename, "r");
140  }
141  if (fp == NULL) {
142    SetPppId();		/* fix from pdp@ark.jr3uom.iijnet.or.jp */
143    snprintf(filename, sizeof filename, "%s/%s", _PATH_PPP, file);
144    fp = fopen(filename, "r");
145  }
146  if (fp == NULL) {
147    LogPrintf(LogDEBUG, "SelectSystem: Can't open %s.\n", filename);
148    SetPppId();
149    return(-1);
150  }
151  LogPrintf(LogDEBUG, "SelectSystem: Checking %s (%s).\n", name, filename);
152
153  linenum = 0;
154  while (fgets(line, sizeof(line), fp)) {
155    linenum++;
156    cp = line;
157    switch (*cp) {
158    case '#':		/* comment */
159      break;
160    case ' ':
161    case '\t':
162      break;
163    default:
164      wp = strpbrk(cp, ":\n");
165      if (wp == NULL) {
166	LogPrintf(LogWARN, "Bad rule in %s (line %d) - missing colon.\n",
167		filename, linenum);
168	exit(1);
169      }
170      *wp = '\0';
171      if (strcmp(cp, name) == 0) {
172	while (fgets(line, sizeof(line), fp)) {
173	  cp = line;
174	  if (*cp == ' ' || *cp == '\t') {
175	    n = strspn(cp, " \t");
176	    cp += n;
177	    LogPrintf(LogCOMMAND, "%s: %s", name, cp);
178	    SetPppId();
179            olauth = VarLocalAuth;
180	    VarLocalAuth = LOCAL_AUTH;
181	    DecodeCommand(cp, strlen(cp), 0);
182            VarLocalAuth = olauth;
183	    SetUserId();
184	  } else if (*cp == '#') {
185	    continue;
186	  } else
187	    break;
188	}
189	fclose(fp);
190	SetPppId();
191	return(0);
192      }
193      break;
194    }
195  }
196  fclose(fp);
197  SetPppId();
198  return -1;
199}
200
201int
202LoadCommand(list, argc, argv)
203struct cmdtab *list;
204int argc;
205char **argv;
206{
207  char *name;
208
209  if (argc > 0)
210    name = *argv;
211  else
212    name = "default";
213
214  if (SelectSystem(name, CONFFILE) < 0) {
215    LogPrintf(LogWARN, "%s: not found.\n", name);
216    return -1;
217  }
218
219  return 0;
220}
221
222int
223SaveCommand(list, argc, argv)
224struct cmdtab *list;
225int argc;
226char **argv;
227{
228  LogPrintf(LogWARN, "save command is not implemented (yet).\n");
229  return 1;
230}
231