systems.c revision 6735
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:$
21 *
22 *  TODO:
23 */
24#include "fsm.h"
25#include "vars.h"
26#include "ipcp.h"
27#include "pathnames.h"
28#include "vars.h"
29
30extern void DecodeCommand();
31
32static int uid, gid;
33static int euid, egid;
34static int usermode;
35
36void
37GetUid()
38{
39  uid = getuid();
40  gid = getgid();
41  euid = geteuid();
42  egid = getegid();
43  usermode = 0;
44}
45
46static void
47SetUserId()
48{
49  if (!usermode) {
50#ifdef __FreeBSD__
51    setruid(euid);
52    seteuid(uid);
53    setrgid(egid);
54    setegid(gid);
55#else
56    setreuid(euid, uid);
57    setregid(egid, gid);
58#endif
59    usermode = 1;
60  }
61}
62
63static void
64SetPppId()
65{
66  if (usermode) {
67#ifdef __FreeBSD__
68    setruid(uid);
69    seteuid(euid);
70    setrgid(gid);
71    setegid(egid);
72#else
73    setreuid(uid, euid);
74    setregid(gid, egid);
75#endif
76    usermode = 0;
77  }
78}
79
80FILE *
81OpenSecret(file)
82char *file;
83{
84  FILE *fp;
85  char *cp;
86  char line[100];
87
88  fp = NULL;
89  cp = getenv("HOME");
90  if (cp) {
91    SetUserId();
92    sprintf(line, "%s/.%s", cp, file);
93    fp = fopen(line, "r");
94  }
95  if (fp == NULL) {
96    SetPppId();
97    sprintf(line, "%s/%s",_PATH_PPP, file);
98    fp = fopen(line, "r");
99  }
100  if (fp == NULL) {
101    fprintf(stderr, "can't open %s.\n", line);
102    SetPppId();
103    return(NULL);
104  }
105  return(fp);
106}
107
108void
109CloseSecret(fp)
110FILE *fp;
111{
112  fclose(fp);
113  SetPppId();
114}
115
116int
117SelectSystem(name, file)
118char *name;
119char *file;
120{
121  FILE *fp;
122  char *cp, *wp;
123  int n;
124  int val = -1;
125  u_char  olauth;
126  char line[200];
127
128  fp = NULL;
129  cp = getenv("HOME");
130  if (cp) {
131    SetUserId();
132    sprintf(line, "%s/.%s", cp, file);
133    fp = fopen(line, "r");
134  }
135  if (fp == NULL) {
136    SetPppId();		/* fix from pdp@ark.jr3uom.iijnet.or.jp */
137    sprintf(line, "%s/%s",_PATH_PPP, file);
138    fp = fopen(line, "r");
139  }
140  if (fp == NULL) {
141    fprintf(stderr, "can't open %s.\n", line);
142    SetPppId();
143    return(-1);
144  }
145#ifdef DEBUG
146  fprintf(stderr, "checking %s (%s).\n", name, line);
147#endif
148  while (fgets(line, sizeof(line), fp)) {
149    cp = line;
150    switch (*cp) {
151    case '#':		/* comment */
152      break;
153    case ' ':
154    case '\t':
155      break;
156    default:
157      wp = strpbrk(cp, ":\n");
158      *wp = '\0';
159      if (strcmp(cp, name) == 0) {
160	while (fgets(line, sizeof(line), fp)) {
161	  cp = line;
162	  if (*cp == ' ' || *cp == '\t') {
163	    n = strspn(cp, " \t");
164	    cp += n;
165#ifdef DEBUG
166	    fprintf(stderr, "%s", cp);
167#endif
168	    SetPppId();
169            olauth = VarLocalAuth;
170	    VarLocalAuth = LOCAL_AUTH;
171	    DecodeCommand(cp, strlen(cp), 0);
172            VarLocalAuth = olauth;
173	    SetUserId();
174	  } else if (*cp == '#') {
175	    continue;
176	  } else
177	    break;
178	}
179	fclose(fp);
180	SetPppId();
181	return(0);
182      }
183      break;
184    }
185  }
186  fclose(fp);
187  SetPppId();
188  return(val);
189}
190
191int
192LoadCommand(list, argc, argv)
193struct cmdtab *list;
194int argc;
195char **argv;
196{
197  char *name;
198
199  if (argc > 0)
200    name = *argv;
201  else
202    name = "default";
203
204  if (SelectSystem(name, CONFFILE) < 0) {
205    printf("%s: not found.\n", name);
206    return(-1);
207  }
208  return(1);
209}
210
211extern struct in_addr ifnetmask;
212
213int
214SaveCommand(list, argc, argv)
215struct cmdtab *list;
216int argc;
217char **argv;
218{
219  printf("save command is not implemented (yet).\n");
220  return(1);
221}
222