systems.c revision 14418
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.5 1995/05/30 03:50:58 rgrimes 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#ifdef DEBUG
128    fprintf(stderr, "can't open %s.\n", line);
129#endif
130    SetPppId();
131    return(-1);
132  }
133#ifdef DEBUG
134  fprintf(stderr, "checking %s (%s).\n", name, line);
135#endif
136  while (fgets(line, sizeof(line), fp)) {
137    cp = line;
138    switch (*cp) {
139    case '#':		/* comment */
140      break;
141    case ' ':
142    case '\t':
143      break;
144    default:
145      wp = strpbrk(cp, ":\n");
146      *wp = '\0';
147      if (strcmp(cp, name) == 0) {
148	while (fgets(line, sizeof(line), fp)) {
149	  cp = line;
150	  if (*cp == ' ' || *cp == '\t') {
151	    n = strspn(cp, " \t");
152	    cp += n;
153#ifdef DEBUG
154	    fprintf(stderr, "%s", cp);
155#endif
156	    SetPppId();
157            olauth = VarLocalAuth;
158	    VarLocalAuth = LOCAL_AUTH;
159	    DecodeCommand(cp, strlen(cp), 0);
160            VarLocalAuth = olauth;
161	    SetUserId();
162	  } else if (*cp == '#') {
163	    continue;
164	  } else
165	    break;
166	}
167	fclose(fp);
168	SetPppId();
169	return(0);
170      }
171      break;
172    }
173  }
174  fclose(fp);
175  SetPppId();
176  return(val);
177}
178
179int
180LoadCommand(list, argc, argv)
181struct cmdtab *list;
182int argc;
183char **argv;
184{
185  char *name;
186
187  if (argc > 0)
188    name = *argv;
189  else
190    name = "default";
191
192  if (SelectSystem(name, CONFFILE) < 0) {
193    printf("%s: not found.\n", name);
194    return(-1);
195  }
196  return(1);
197}
198
199extern struct in_addr ifnetmask;
200
201int
202SaveCommand(list, argc, argv)
203struct cmdtab *list;
204int argc;
205char **argv;
206{
207  printf("save command is not implemented (yet).\n");
208  return(1);
209}
210