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