xref: /freebsd/usr.sbin/ppp/systems.c (revision 5ebc7e6281887681c3a348a5a4c902e262ccd656)
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 
30 extern void DecodeCommand();
31 
32 static int uid, gid;
33 static int euid, egid;
34 static int usermode;
35 
36 void
37 GetUid()
38 {
39   uid = getuid();
40   gid = getgid();
41   euid = geteuid();
42   egid = getegid();
43   usermode = 0;
44 }
45 
46 static void
47 SetUserId()
48 {
49   if (!usermode) {
50     setreuid(euid, uid);
51     setregid(egid, gid);
52     usermode = 1;
53   }
54 }
55 
56 static void
57 SetPppId()
58 {
59   if (usermode) {
60     setreuid(uid, euid);
61     setregid(gid, egid);
62     usermode = 0;
63   }
64 }
65 
66 FILE *
67 OpenSecret(file)
68 char *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 
94 void
95 CloseSecret(fp)
96 FILE *fp;
97 {
98   fclose(fp);
99   SetPppId();
100 }
101 
102 int
103 SelectSystem(name, file)
104 char *name;
105 char *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 
177 int
178 LoadCommand(list, argc, argv)
179 struct cmdtab *list;
180 int argc;
181 char **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 
197 extern struct in_addr ifnetmask;
198 
199 int
200 SaveCommand(list, argc, argv)
201 struct cmdtab *list;
202 int argc;
203 char **argv;
204 {
205   printf("save command is not implemented (yet).\n");
206   return(1);
207 }
208