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