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