systems.c revision 28974
1228753Smm/* 2228753Smm * System configuration routines 3228753Smm * 4228753Smm * Written by Toshiharu OHNO (tony-o@iij.ad.jp) 5228753Smm * 6228753Smm * Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd. 7228753Smm * 8228753Smm * Redistribution and use in source and binary forms are permitted 9228753Smm * provided that the above copyright notice and this paragraph are 10228753Smm * duplicated in all such forms and that any documentation, 11228753Smm * advertising materials, and other materials related to such 12228753Smm * distribution and use acknowledge that the software was developed 13228753Smm * by the Internet Initiative Japan, Inc. The name of the 14228753Smm * IIJ may not be used to endorse or promote products derived 15228753Smm * from this software without specific prior written permission. 16228753Smm * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 17228753Smm * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 18228753Smm * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 19228753Smm * 20228753Smm * $Id: systems.c,v 1.14 1997/08/25 00:29:29 brian Exp $ 21228753Smm * 22228753Smm * TODO: 23228753Smm */ 24228753Smm#include "fsm.h" 25228753Smm#include "loadalias.h" 26229592Smm#include "vars.h" 27228753Smm#include "ipcp.h" 28228753Smm#include "pathnames.h" 29228753Smm#include "vars.h" 30228753Smm#include "server.h" 31228753Smm#include "command.h" 32228753Smm 33228753Smmextern void DecodeCommand(); 34228753Smm 35228753Smmstatic int uid, gid; 36228753Smmstatic int euid, egid; 37228753Smmstatic int usermode; 38228753Smm 39228753Smmint 40228753SmmOrigUid() 41228753Smm{ 42228753Smm return uid; 43228753Smm} 44228753Smm 45228753Smmvoid 46228753SmmGetUid() 47228753Smm{ 48228753Smm uid = getuid(); 49228753Smm gid = getgid(); 50228753Smm euid = geteuid(); 51228753Smm egid = getegid(); 52228753Smm usermode = 0; 53228753Smm} 54228753Smm 55228753Smmstatic void 56228753SmmSetUserId() 57228753Smm{ 58228753Smm if (!usermode) { 59228753Smm if (setreuid(euid, uid) == -1) { 60228753Smm LogPrintf(LogERROR, "unable to setreuid!\n"); 61228753Smm ServerClose(); 62228753Smm exit(1); 63228753Smm } 64228753Smm if (setregid(egid, gid) == -1) { 65228753Smm LogPrintf(LogERROR, "unable to setregid!\n"); 66228753Smm ServerClose(); 67228753Smm exit(1); 68228753Smm } 69228753Smm usermode = 1; 70228753Smm } 71228753Smm} 72228753Smm 73228753Smmstatic void 74228753SmmSetPppId() 75228753Smm{ 76228753Smm if (usermode) { 77228753Smm if (setreuid(uid, euid) == -1) { 78228753Smm LogPrintf(LogERROR, "unable to setreuid!\n"); 79228753Smm ServerClose(); 80228753Smm exit(1); 81228753Smm } 82228753Smm if (setregid(gid, egid) == -1) { 83228753Smm LogPrintf(LogERROR, "unable to setregid!\n"); 84228753Smm ServerClose(); 85228753Smm exit(1); 86228753Smm } 87228753Smm usermode = 0; 88228753Smm } 89228753Smm} 90228753Smm 91228753SmmFILE * 92228753SmmOpenSecret(char *file) 93228753Smm{ 94228753Smm FILE *fp; 95228753Smm char *cp; 96228753Smm char line[100]; 97228753Smm 98228753Smm fp = NULL; 99228753Smm 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 118void 119CloseSecret(FILE * fp) 120{ 121 fclose(fp); 122 SetPppId(); 123} 124 125int 126SelectSystem(char *name, char *file) 127{ 128 FILE *fp; 129 char *cp, *wp; 130 int n; 131 u_char olauth; 132 char line[200]; 133 char filename[200]; 134 int linenum; 135 136 fp = NULL; 137 cp = getenv("HOME"); 138 if (cp) { 139 SetUserId(); 140 snprintf(filename, sizeof filename, "%s/.%s", cp, file); 141 fp = fopen(filename, "r"); 142 } 143 if (fp == NULL) { 144 SetPppId(); /* fix from pdp@ark.jr3uom.iijnet.or.jp */ 145 snprintf(filename, sizeof filename, "%s/%s", _PATH_PPP, file); 146 fp = fopen(filename, "r"); 147 } 148 if (fp == NULL) { 149 LogPrintf(LogDEBUG, "SelectSystem: Can't open %s.\n", filename); 150 SetPppId(); 151 return (-1); 152 } 153 LogPrintf(LogDEBUG, "SelectSystem: Checking %s (%s).\n", name, filename); 154 155 linenum = 0; 156 while (fgets(line, sizeof(line), fp)) { 157 linenum++; 158 cp = line; 159 switch (*cp) { 160 case '#': /* comment */ 161 break; 162 case ' ': 163 case '\t': 164 break; 165 default: 166 wp = strpbrk(cp, ":\n"); 167 if (wp == NULL) { 168 LogPrintf(LogWARN, "Bad rule in %s (line %d) - missing colon.\n", 169 filename, linenum); 170 ServerClose(); 171 exit(1); 172 } 173 *wp = '\0'; 174 if (strcmp(cp, name) == 0) { 175 while (fgets(line, sizeof(line), fp)) { 176 cp = line; 177 if (*cp == ' ' || *cp == '\t') { 178 n = strspn(cp, " \t"); 179 cp += n; 180 LogPrintf(LogCOMMAND, "%s: %s\n", name, cp); 181 SetPppId(); 182 olauth = VarLocalAuth; 183 VarLocalAuth = LOCAL_AUTH; 184 DecodeCommand(cp, strlen(cp), 0); 185 VarLocalAuth = olauth; 186 SetUserId(); 187 } else if (*cp == '#') { 188 continue; 189 } else 190 break; 191 } 192 fclose(fp); 193 SetPppId(); 194 return (0); 195 } 196 break; 197 } 198 } 199 fclose(fp); 200 SetPppId(); 201 return -1; 202} 203 204int 205LoadCommand(struct cmdtab const * list, int argc, char **argv) 206{ 207 char *name; 208 209 if (argc > 0) 210 name = *argv; 211 else 212 name = "default"; 213 214 if (SelectSystem(name, CONFFILE) < 0) { 215 LogPrintf(LogWARN, "%s: not found.\n", name); 216 return -1; 217 } 218 return 0; 219} 220 221int 222SaveCommand(struct cmdtab const * list, int argc, char **argv) 223{ 224 LogPrintf(LogWARN, "save command is not implemented (yet).\n"); 225 return 1; 226} 227