systems.c revision 6059
1125388Sdes/*
2125388Sdes *	          System configuration routines
3125388Sdes *
4125388Sdes *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
5125388Sdes *
6125388Sdes *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
7125498Sdes *
8136224Smtm * Redistribution and use in source and binary forms are permitted
9125388Sdes * provided that the above copyright notice and this paragraph are
10125388Sdes * duplicated in all such forms and that any documentation,
11125388Sdes * advertising materials, and other materials related to such
12148765Spjd * distribution and use acknowledge that the software was developed
13148765Spjd * by the Internet Initiative Japan, Inc.  The name of the
14148765Spjd * IIJ may not be used to endorse or promote products derived
15125388Sdes * from this software without specific prior written permission.
16148765Spjd * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17125388Sdes * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18125388Sdes * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19133987Sthomas *
20133987Sthomas * $Id:$
21133987Sthomas *
22133987Sthomas *  TODO:
23125388Sdes */
24148765Spjd#include "fsm.h"
25148765Spjd#include "vars.h"
26148765Spjd#include "ipcp.h"
27148765Spjd
28125388Sdesextern void DecodeCommand();
29148765Spjd
30148765Spjdstatic int uid, gid;
31148765Spjdstatic int euid, egid;
32125388Sdesstatic int usermode;
33125388Sdes
34133987Sthomasvoid
35125388SdesGetUid()
36125388Sdes{
37148765Spjd  uid = getuid();
38125388Sdes  gid = getgid();
39125388Sdes  euid = geteuid();
40133987Sthomas  egid = getegid();
41133987Sthomas  usermode = 0;
42133987Sthomas}
43133987Sthomas
44125388Sdesstatic void
45148765SpjdSetUserId()
46148765Spjd{
47125388Sdes  if (!usermode) {
48148765Spjd#ifdef __FreeBSD__
49148765Spjd    setruid(euid);
50148765Spjd    seteuid(uid);
51125388Sdes    setrgid(egid);
52125388Sdes    setegid(gid);
53133987Sthomas#else
54125388Sdes    setreuid(euid, uid);
55125388Sdes    setregid(egid, gid);
56125388Sdes#endif
57125388Sdes    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