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