systems.c revision 21673
175584Sru/*
2151497Sru *	          System configuration routines
3151497Sru *
475584Sru *	    Written by Toshiharu OHNO (tony-o@iij.ad.jp)
575584Sru *
675584Sru *   Copyright (C) 1993, Internet Initiative Japan, Inc. All rights reserverd.
775584Sru *
875584Sru * Redistribution and use in source and binary forms are permitted
975584Sru * provided that the above copyright notice and this paragraph are
1075584Sru * duplicated in all such forms and that any documentation,
1175584Sru * advertising materials, and other materials related to such
1275584Sru * distribution and use acknowledge that the software was developed
1375584Sru * by the Internet Initiative Japan, Inc.  The name of the
1475584Sru * IIJ may not be used to endorse or promote products derived
1575584Sru * from this software without specific prior written permission.
1675584Sru * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1775584Sru * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18151497Sru * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1975584Sru *
2075584Sru * $FreeBSD: head/usr.sbin/ppp/systems.c 21673 1997-01-14 07:20:47Z jkh $
2175584Sru *
2275584Sru *  TODO:
2375584Sru */
2475584Sru#include "fsm.h"
2575584Sru#include "vars.h"
2675584Sru#include "ipcp.h"
2775584Sru#include "pathnames.h"
2875584Sru#include "vars.h"
2975584Sru
3075584Sruextern void DecodeCommand();
3175584Sru
32151497Srustatic int uid, gid;
3375584Srustatic int euid, egid;
3475584Srustatic int usermode;
3575584Sru
3675584Sruvoid
37151497SruGetUid()
3875584Sru{
3975584Sru  uid = getuid();
40151497Sru  gid = getgid();
4179543Sru  euid = geteuid();
4275584Sru  egid = getegid();
4375584Sru  usermode = 0;
4475584Sru}
4575584Sru
4675584Srustatic void
4775584SruSetUserId()
48151497Sru{
4975584Sru  if (!usermode) {
50151497Sru    setreuid(euid, uid);
5175584Sru    setregid(egid, gid);
5279543Sru    usermode = 1;
5375584Sru  }
5475584Sru}
55151497Sru
5675584Srustatic void
57151497SruSetPppId()
5875584Sru{
59114402Sru  if (usermode) {
6075584Sru    setreuid(uid, euid);
6175584Sru    setregid(gid, egid);
62151497Sru    usermode = 0;
6375584Sru  }
6475584Sru}
6575584Sru
66151497SruFILE *
6775584SruOpenSecret(file)
68151497Sruchar *file;
69151497Sru{
7075584Sru  FILE *fp;
71151497Sru  char *cp;
7275584Sru  char line[100];
73151497Sru
7475584Sru  fp = NULL;
75151497Sru  cp = getenv("HOME");
76151497Sru  if (cp) {
7775584Sru    SetUserId();
78151497Sru    snprintf(line, sizeof line, "%s/.%s", cp, file);
7975584Sru    fp = fopen(line, "r");
80151497Sru  }
8175584Sru  if (fp == NULL) {
82151497Sru    SetPppId();
83151497Sru    snprintf(line, sizeof line, "%s/%s", _PATH_PPP, file);
8475584Sru    fp = fopen(line, "r");
85151497Sru  }
8675584Sru  if (fp == NULL) {
87151497Sru    fprintf(stderr, "can't open %s.\n", line);
8875584Sru    SetPppId();
89151497Sru    return(NULL);
90151497Sru  }
9175584Sru  return(fp);
92151497Sru}
9375584Sru
94151497Sruvoid
9575584SruCloseSecret(fp)
96151497SruFILE *fp;
97151497Sru{
9875584Sru  fclose(fp);
99151497Sru  SetPppId();
10075584Sru}
10175584Sru
10275584Sruint
10375584SruSelectSystem(name, file)
104151497Sruchar *name;
10575584Sruchar *file;
106151497Sru{
10775584Sru  FILE *fp;
10875584Sru  char *cp, *wp;
10975584Sru  int n;
11075584Sru  int val = -1;
111151497Sru  u_char  olauth;
11275584Sru  char line[200];
113151497Sru
11475584Sru  fp = NULL;
11575584Sru  cp = getenv("HOME");
11675584Sru  if (cp) {
11775584Sru    SetUserId();
118151497Sru    snprintf(line, sizeof line, "%s/.%s", cp, file);
11975584Sru    fp = fopen(line, "r");
120151497Sru  }
12175584Sru  if (fp == NULL) {
12275584Sru    SetPppId();		/* fix from pdp@ark.jr3uom.iijnet.or.jp */
12375584Sru    snprintf(line, sizeof line, "%s/%s", _PATH_PPP, file);
12475584Sru    fp = fopen(line, "r");
12575584Sru  }
12675584Sru  if (fp == NULL) {
127151497Sru#ifdef DEBUG
12875584Sru    fprintf(stderr, "can't open %s.\n", line);
129151497Sru#endif
13075584Sru    SetPppId();
13175584Sru    return(-1);
13275584Sru  }
13375584Sru#ifdef DEBUG
13475584Sru  fprintf(stderr, "checking %s (%s).\n", name, line);
13575584Sru#endif
13675584Sru  while (fgets(line, sizeof(line), fp)) {
137151497Sru    cp = line;
13875584Sru    switch (*cp) {
13975584Sru    case '#':		/* comment */
140151497Sru      break;
141151497Sru    case ' ':
142151497Sru    case '\t':
14375584Sru      break;
14475584Sru    default:
14575584Sru      wp = strpbrk(cp, ":\n");
14675584Sru      *wp = '\0';
14775584Sru      if (strcmp(cp, name) == 0) {
14875584Sru	while (fgets(line, sizeof(line), fp)) {
14975584Sru	  cp = line;
15075584Sru	  if (*cp == ' ' || *cp == '\t') {
15175584Sru	    n = strspn(cp, " \t");
15275584Sru	    cp += n;
15375584Sru#ifdef DEBUG
15475584Sru	    fprintf(stderr, "%s", cp);
15575584Sru#endif
15675584Sru	    SetPppId();
15775584Sru            olauth = VarLocalAuth;
15875584Sru	    VarLocalAuth = LOCAL_AUTH;
15975584Sru	    DecodeCommand(cp, strlen(cp), 0);
16075584Sru            VarLocalAuth = olauth;
16175584Sru	    SetUserId();
16275584Sru	  } else if (*cp == '#') {
16375584Sru	    continue;
16475584Sru	  } else
165151497Sru	    break;
16675584Sru	}
167151497Sru	fclose(fp);
16875584Sru	SetPppId();
16975584Sru	return(0);
17075584Sru      }
17175584Sru      break;
17275584Sru    }
17375584Sru  }
174151497Sru  fclose(fp);
17575584Sru  SetPppId();
17675584Sru  return(val);
17775584Sru}
17875584Sru
17975584Sruint
18075584SruLoadCommand(list, argc, argv)
18175584Srustruct cmdtab *list;
18275584Sruint argc;
18375584Sruchar **argv;
18475584Sru{
18575584Sru  char *name;
18675584Sru
18775584Sru  if (argc > 0)
18875584Sru    name = *argv;
189151497Sru  else
19075584Sru    name = "default";
191151497Sru
19275584Sru  if (SelectSystem(name, CONFFILE) < 0) {
19375584Sru    printf("%s: not found.\n", name);
19475584Sru    return(-1);
19575584Sru  }
196151497Sru  return(1);
19775584Sru}
198151497Sru
199151497Sruextern struct in_addr ifnetmask;
20079543Sru
20175584Sruint
20275584SruSaveCommand(list, argc, argv)
20375584Srustruct cmdtab *list;
20475584Sruint argc;
20575584Sruchar **argv;
20675584Sru{
20775584Sru  printf("save command is not implemented (yet).\n");
208151497Sru  return(1);
20975584Sru}
210151497Sru