systems.c revision 30913
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.17 1997/10/26 01:03:48 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 45static int uid; 46static int gid; 47static int euid; 48static int egid; 49static int usermode; 50 51int 52OrigUid() 53{ 54 return uid; 55} 56 57void 58GetUid() 59{ 60 uid = getuid(); 61 gid = getgid(); 62 euid = geteuid(); 63 egid = getegid(); 64 usermode = 0; 65} 66 67static void 68SetUserId() 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 85static void 86SetPppId() 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 103FILE * 104OpenSecret(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 130void 131CloseSecret(FILE * fp) 132{ 133 fclose(fp); 134 SetPppId(); 135} 136 137int 138SelectSystem(char *name, char *file) 139{ 140 FILE *fp; 141 char *cp, *wp; 142 int n, len; 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 len = strlen(cp); 193 if (!len) 194 continue; 195 if (cp[len-1] == '\n') 196 cp[--len] = '\0'; 197 if (!len) 198 continue; 199 LogPrintf(LogCOMMAND, "%s: %s\n", name, cp); 200 SetPppId(); 201 olauth = VarLocalAuth; 202 if (VarLocalAuth == LOCAL_NO_AUTH) 203 VarLocalAuth = LOCAL_AUTH; 204 DecodeCommand(cp, len, 0); 205 VarLocalAuth = olauth; 206 SetUserId(); 207 } else if (*cp == '#') { 208 continue; 209 } else 210 break; 211 } 212 fclose(fp); 213 SetPppId(); 214 return (0); 215 } 216 break; 217 } 218 } 219 fclose(fp); 220 SetPppId(); 221 return -1; 222} 223 224int 225LoadCommand(struct cmdtab const * list, int argc, char **argv) 226{ 227 char *name; 228 229 if (argc > 0) 230 name = *argv; 231 else 232 name = "default"; 233 234 if (SelectSystem(name, CONFFILE) < 0) { 235 LogPrintf(LogWARN, "%s: not found.\n", name); 236 return -1; 237 } 238 return 0; 239} 240 241int 242SaveCommand(struct cmdtab const *list, int argc, char **argv) 243{ 244 LogPrintf(LogWARN, "save command is not implemented (yet).\n"); 245 return 1; 246} 247