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