systems.c revision 28974
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.14 1997/08/25 00:29:29 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#include "command.h" 32 33extern void DecodeCommand(); 34 35static int uid, gid; 36static int euid, egid; 37static int usermode; 38 39int 40OrigUid() 41{ 42 return uid; 43} 44 45void 46GetUid() 47{ 48 uid = getuid(); 49 gid = getgid(); 50 euid = geteuid(); 51 egid = getegid(); 52 usermode = 0; 53} 54 55static void 56SetUserId() 57{ 58 if (!usermode) { 59 if (setreuid(euid, uid) == -1) { 60 LogPrintf(LogERROR, "unable to setreuid!\n"); 61 ServerClose(); 62 exit(1); 63 } 64 if (setregid(egid, gid) == -1) { 65 LogPrintf(LogERROR, "unable to setregid!\n"); 66 ServerClose(); 67 exit(1); 68 } 69 usermode = 1; 70 } 71} 72 73static void 74SetPppId() 75{ 76 if (usermode) { 77 if (setreuid(uid, euid) == -1) { 78 LogPrintf(LogERROR, "unable to setreuid!\n"); 79 ServerClose(); 80 exit(1); 81 } 82 if (setregid(gid, egid) == -1) { 83 LogPrintf(LogERROR, "unable to setregid!\n"); 84 ServerClose(); 85 exit(1); 86 } 87 usermode = 0; 88 } 89} 90 91FILE * 92OpenSecret(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 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