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