17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
237c478bd9Sstevel@tonic-gate /* All Rights Reserved */
24*34e48580Sdp /*
25*34e48580Sdp * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
26*34e48580Sdp * Use is subject to license terms.
27*34e48580Sdp */
287c478bd9Sstevel@tonic-gate
29*34e48580Sdp #pragma ident "%Z%%M% %I% %E% SMI"
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <stdio.h>
33*34e48580Sdp #include <stdlib.h>
34*34e48580Sdp #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <signal.h>
367c478bd9Sstevel@tonic-gate #include <sac.h>
377c478bd9Sstevel@tonic-gate #include <sys/types.h>
387c478bd9Sstevel@tonic-gate #include <sys/stat.h>
397c478bd9Sstevel@tonic-gate #include <unistd.h>
407c478bd9Sstevel@tonic-gate #include "misc.h"
417c478bd9Sstevel@tonic-gate #include "structs.h"
427c478bd9Sstevel@tonic-gate #include "extern.h"
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate * error - print out an error message and die
477c478bd9Sstevel@tonic-gate *
487c478bd9Sstevel@tonic-gate * args: msg - message to be printed, Saferrno previously set
497c478bd9Sstevel@tonic-gate */
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate void
error(msg)527c478bd9Sstevel@tonic-gate error(msg)
537c478bd9Sstevel@tonic-gate char *msg;
547c478bd9Sstevel@tonic-gate {
557c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", msg);
567c478bd9Sstevel@tonic-gate quit();
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate * quit - exit the program with the status in Saferrno
627c478bd9Sstevel@tonic-gate */
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate void
quit()657c478bd9Sstevel@tonic-gate quit()
667c478bd9Sstevel@tonic-gate {
677c478bd9Sstevel@tonic-gate exit(Saferrno);
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate /*
727c478bd9Sstevel@tonic-gate * make_tempname - generate a temp name to be used for updating files.
737c478bd9Sstevel@tonic-gate * Names will be of the form HOME/xxx/.name, where HOME
747c478bd9Sstevel@tonic-gate * is from misc.h
757c478bd9Sstevel@tonic-gate *
767c478bd9Sstevel@tonic-gate * args: bname - the basename of the file. For example foo/_config
777c478bd9Sstevel@tonic-gate * will generate a tempname of HOME/foo/._config
787c478bd9Sstevel@tonic-gate */
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate
817c478bd9Sstevel@tonic-gate char *
make_tempname(bname)827c478bd9Sstevel@tonic-gate make_tempname(bname)
837c478bd9Sstevel@tonic-gate char *bname;
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate static char buf[SIZE]; /* this is where we put the new name */
867c478bd9Sstevel@tonic-gate char *p; /* work pointer */
877c478bd9Sstevel@tonic-gate
887c478bd9Sstevel@tonic-gate p = strrchr(bname, '/');
897c478bd9Sstevel@tonic-gate if (p == NULL)
907c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%s/.%s", HOME, bname);
917c478bd9Sstevel@tonic-gate else {
927c478bd9Sstevel@tonic-gate (void) strcpy(buf, HOME);
937c478bd9Sstevel@tonic-gate /* this zaps the trailing slash so the '.' can be stuck in */
947c478bd9Sstevel@tonic-gate *p = '\0';
957c478bd9Sstevel@tonic-gate (void) strcat(buf, "/");
967c478bd9Sstevel@tonic-gate (void) strcat(buf, bname);
977c478bd9Sstevel@tonic-gate (void) strcat(buf, "/.");
987c478bd9Sstevel@tonic-gate (void) strcat(buf, (p + 1));
997c478bd9Sstevel@tonic-gate *p = '/';
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate return(buf);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate
1047c478bd9Sstevel@tonic-gate
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate * open_temp - open up a temp file
1077c478bd9Sstevel@tonic-gate *
1087c478bd9Sstevel@tonic-gate * args: tname - temp file name
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate FILE *
open_temp(tname)1147c478bd9Sstevel@tonic-gate open_temp(tname)
1157c478bd9Sstevel@tonic-gate char *tname;
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate FILE *fp; /* fp associated with tname */
1187c478bd9Sstevel@tonic-gate struct sigaction sigact; /* for signal handling */
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
1217c478bd9Sstevel@tonic-gate sigact.sa_handler = SIG_IGN;
1227c478bd9Sstevel@tonic-gate (void) sigemptyset(&sigact.sa_mask);
1237c478bd9Sstevel@tonic-gate (void) sigaddset(&sigact.sa_mask, SIGHUP);
1247c478bd9Sstevel@tonic-gate (void) sigaddset(&sigact.sa_mask, SIGINT);
1257c478bd9Sstevel@tonic-gate (void) sigaddset(&sigact.sa_mask, SIGQUIT);
1267c478bd9Sstevel@tonic-gate (void) sigaction(SIGHUP, &sigact, NULL);
1277c478bd9Sstevel@tonic-gate (void) sigaction(SIGINT, &sigact, NULL);
1287c478bd9Sstevel@tonic-gate (void) sigaction(SIGQUIT, &sigact, NULL);
1297c478bd9Sstevel@tonic-gate (void) umask(0333);
1307c478bd9Sstevel@tonic-gate if (access(tname, 0) != -1) {
1317c478bd9Sstevel@tonic-gate Saferrno = E_SAFERR;
1327c478bd9Sstevel@tonic-gate error("tempfile busy; try again later");
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate fp = fopen(tname, "w");
1357c478bd9Sstevel@tonic-gate if (fp == NULL) {
1367c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
1377c478bd9Sstevel@tonic-gate error("cannot create tempfile");
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate return(fp);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate * replace - replace one file with another, only returns on success
1457c478bd9Sstevel@tonic-gate *
1467c478bd9Sstevel@tonic-gate * args: fname - name of target file
1477c478bd9Sstevel@tonic-gate * tname - name of source file
1487c478bd9Sstevel@tonic-gate */
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate void
replace(fname,tname)1527c478bd9Sstevel@tonic-gate replace(fname, tname)
1537c478bd9Sstevel@tonic-gate char *fname;
1547c478bd9Sstevel@tonic-gate char *tname;
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate char buf[SIZE]; /* scratch buffer */
1577c478bd9Sstevel@tonic-gate
1587c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%s/%s", HOME, fname);
1597c478bd9Sstevel@tonic-gate (void) unlink(buf);
1607c478bd9Sstevel@tonic-gate if (rename(tname, buf) < 0) {
1617c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
1627c478bd9Sstevel@tonic-gate (void) unlink(tname);
1637c478bd9Sstevel@tonic-gate quit();
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate /*
1697c478bd9Sstevel@tonic-gate * copy_file - copy information from one file to another, return 0 on
1707c478bd9Sstevel@tonic-gate * success, -1 on failure
1717c478bd9Sstevel@tonic-gate *
1727c478bd9Sstevel@tonic-gate * args: fp - source file's file pointer
1737c478bd9Sstevel@tonic-gate * tfp - destination file's file pointer
1747c478bd9Sstevel@tonic-gate * start - starting line number
1757c478bd9Sstevel@tonic-gate * finish - ending line number (-1 indicates entire file)
1767c478bd9Sstevel@tonic-gate */
1777c478bd9Sstevel@tonic-gate
178*34e48580Sdp int
copy_file(FILE * fp,FILE * tfp,int start,int finish)179*34e48580Sdp copy_file(FILE *fp, FILE *tfp, int start, int finish)
1807c478bd9Sstevel@tonic-gate {
181*34e48580Sdp int i; /* loop variable */
1827c478bd9Sstevel@tonic-gate char dummy[SIZE]; /* scratch buffer */
1837c478bd9Sstevel@tonic-gate
1847c478bd9Sstevel@tonic-gate /*
1857c478bd9Sstevel@tonic-gate * always start from the beginning because line numbers are absolute
1867c478bd9Sstevel@tonic-gate */
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate rewind(fp);
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate /*
1917c478bd9Sstevel@tonic-gate * get to the starting point of interest
1927c478bd9Sstevel@tonic-gate */
1937c478bd9Sstevel@tonic-gate
1947c478bd9Sstevel@tonic-gate if (start != 1) {
1957c478bd9Sstevel@tonic-gate for (i = 1; i < start; i++)
1967c478bd9Sstevel@tonic-gate if (!fgets(dummy, SIZE, fp))
1977c478bd9Sstevel@tonic-gate return(-1);
1987c478bd9Sstevel@tonic-gate }
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate /*
2017c478bd9Sstevel@tonic-gate * copy as much as was requested
2027c478bd9Sstevel@tonic-gate */
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate if (finish != -1) {
2057c478bd9Sstevel@tonic-gate for (i = start; i <= finish; i++) {
2067c478bd9Sstevel@tonic-gate if (!fgets(dummy, SIZE, fp))
2077c478bd9Sstevel@tonic-gate return(-1);
2087c478bd9Sstevel@tonic-gate if (fputs(dummy, tfp) == EOF)
2097c478bd9Sstevel@tonic-gate return(-1);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate else {
2137c478bd9Sstevel@tonic-gate for (;;) {
2147c478bd9Sstevel@tonic-gate if (fgets(dummy, SIZE, fp) == NULL) {
2157c478bd9Sstevel@tonic-gate if (feof(fp))
2167c478bd9Sstevel@tonic-gate break;
2177c478bd9Sstevel@tonic-gate else
2187c478bd9Sstevel@tonic-gate return(-1);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate if (fputs(dummy, tfp) == EOF)
2217c478bd9Sstevel@tonic-gate return(-1);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate return(0);
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate
2277c478bd9Sstevel@tonic-gate
2287c478bd9Sstevel@tonic-gate /*
2297c478bd9Sstevel@tonic-gate * find_pm - find an entry in _sactab for a particular port monitor
2307c478bd9Sstevel@tonic-gate *
2317c478bd9Sstevel@tonic-gate * args: fp - file pointer for _sactab
2327c478bd9Sstevel@tonic-gate * pmtag - tag of port monitor we're looking for
2337c478bd9Sstevel@tonic-gate */
2347c478bd9Sstevel@tonic-gate
235*34e48580Sdp int
find_pm(FILE * fp,char * pmtag)236*34e48580Sdp find_pm(FILE *fp, char *pmtag)
2377c478bd9Sstevel@tonic-gate {
238*34e48580Sdp char *p; /* working pointer */
2397c478bd9Sstevel@tonic-gate int line = 0; /* line number we found entry on */
2407c478bd9Sstevel@tonic-gate struct sactab stab; /* place to hold parsed info */
2417c478bd9Sstevel@tonic-gate char buf[SIZE]; /* scratch buffer */
2427c478bd9Sstevel@tonic-gate
2437c478bd9Sstevel@tonic-gate while (fgets(buf, SIZE, fp)) {
2447c478bd9Sstevel@tonic-gate line++;
2457c478bd9Sstevel@tonic-gate p = trim(buf);
2467c478bd9Sstevel@tonic-gate if (*p == '\0')
2477c478bd9Sstevel@tonic-gate continue;
2487c478bd9Sstevel@tonic-gate parse(p, &stab);
2497c478bd9Sstevel@tonic-gate if (!(strcmp(stab.sc_tag, pmtag)))
2507c478bd9Sstevel@tonic-gate return(line);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate if (!feof(fp)) {
2537c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
2547c478bd9Sstevel@tonic-gate error("error reading _sactab");
2557c478bd9Sstevel@tonic-gate /* NOTREACHED */
256*34e48580Sdp return (0);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate else
2597c478bd9Sstevel@tonic-gate return(0);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate * do_config - take a config script and put it where it belongs or
2657c478bd9Sstevel@tonic-gate * output an existing one. Saferrno is set if any errors
2667c478bd9Sstevel@tonic-gate * are encountered. Calling routine may choose to quit or
2677c478bd9Sstevel@tonic-gate * continue, in which case Saferrno will stay set, but may
2687c478bd9Sstevel@tonic-gate * change value if another error is encountered.
2697c478bd9Sstevel@tonic-gate *
2707c478bd9Sstevel@tonic-gate * args: script - name of file containing script (if NULL, means output
2717c478bd9Sstevel@tonic-gate * existing one instead)
2727c478bd9Sstevel@tonic-gate * basename - name of script (relative to HOME (from misc.h))
2737c478bd9Sstevel@tonic-gate */
2747c478bd9Sstevel@tonic-gate
275*34e48580Sdp int
do_config(char * script,char * basename)276*34e48580Sdp do_config(char *script, char *basename)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate FILE *ifp; /* file pointer for source file */
2797c478bd9Sstevel@tonic-gate FILE *ofp; /* file pointer for target file */
2807c478bd9Sstevel@tonic-gate struct stat statbuf; /* file status info */
2817c478bd9Sstevel@tonic-gate char *tname; /* name of tempfile */
2827c478bd9Sstevel@tonic-gate char buf[SIZE]; /* scratch buffer */
2837c478bd9Sstevel@tonic-gate
2847c478bd9Sstevel@tonic-gate if (script) {
2857c478bd9Sstevel@tonic-gate /* we're installing a new configuration script */
2867c478bd9Sstevel@tonic-gate if (access(script, 0) == 0) {
2877c478bd9Sstevel@tonic-gate if (stat(script, &statbuf) < 0) {
2887c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
2897c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Could not stat <%s>\n", script);
2907c478bd9Sstevel@tonic-gate return(1);
2917c478bd9Sstevel@tonic-gate }
2927c478bd9Sstevel@tonic-gate if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
2937c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "warning - %s not a regular file - ignored\n", script);
2947c478bd9Sstevel@tonic-gate return(1);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate }
2977c478bd9Sstevel@tonic-gate else {
2987c478bd9Sstevel@tonic-gate Saferrno = E_NOEXIST;
2997c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Invalid request, %s does not exist\n", script);
3007c478bd9Sstevel@tonic-gate return(1);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate ifp = fopen(script, "r");
3037c478bd9Sstevel@tonic-gate if (ifp == NULL) {
3047c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Invalid request, can not open %s\n", script);
3057c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
3067c478bd9Sstevel@tonic-gate return(1);
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate tname = make_tempname(basename);
3097c478bd9Sstevel@tonic-gate /* note - open_temp only returns if successful */
3107c478bd9Sstevel@tonic-gate ofp = open_temp(tname);
3117c478bd9Sstevel@tonic-gate while(fgets(buf, SIZE, ifp)) {
3127c478bd9Sstevel@tonic-gate if (fputs(buf, ofp) == EOF) {
3137c478bd9Sstevel@tonic-gate (void) unlink(tname);
3147c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
3157c478bd9Sstevel@tonic-gate error("error in writing tempfile");
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate (void) fclose(ifp);
3197c478bd9Sstevel@tonic-gate if (fclose(ofp) == EOF) {
3207c478bd9Sstevel@tonic-gate (void) unlink(tname);
3217c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
3227c478bd9Sstevel@tonic-gate error("error closing tempfile");
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate /* note - replace only returns if successful */
3257c478bd9Sstevel@tonic-gate replace(basename, tname);
3267c478bd9Sstevel@tonic-gate return(0);
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate else {
3297c478bd9Sstevel@tonic-gate /* we're outputting a configuration script */
3307c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%s/%s", HOME, basename);
3317c478bd9Sstevel@tonic-gate if (access(buf, 0) < 0) {
3327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Invalid request, script does not exist\n");
3337c478bd9Sstevel@tonic-gate Saferrno = E_NOEXIST;
3347c478bd9Sstevel@tonic-gate return(1);
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate ifp = fopen(buf, "r");
3377c478bd9Sstevel@tonic-gate if (ifp == NULL) {
3387c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Invalid request, can not open script\n");
3397c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
3407c478bd9Sstevel@tonic-gate return(1);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate while (fgets(buf, SIZE, ifp))
3437c478bd9Sstevel@tonic-gate (void) fputs(buf, stdout);
3447c478bd9Sstevel@tonic-gate (void) fclose(ifp);
3457c478bd9Sstevel@tonic-gate return(0);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate }
348