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 #include <stdio.h>
30*34e48580Sdp #include <stdlib.h>
31*34e48580Sdp #include <strings.h>
32*34e48580Sdp #include <signal.h>
33*34e48580Sdp #include <sac.h>
34*34e48580Sdp #include <sys/types.h>
35*34e48580Sdp #include <sys/stat.h>
36*34e48580Sdp #include <unistd.h>
37*34e48580Sdp #include "misc.h"
38*34e48580Sdp #include "structs.h"
39*34e48580Sdp #include "extern.h"
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate * error - print out an error message and die
447c478bd9Sstevel@tonic-gate *
457c478bd9Sstevel@tonic-gate * args: msg - message to be printed, Saferrno previously set
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate void
error(msg)497c478bd9Sstevel@tonic-gate error(msg)
507c478bd9Sstevel@tonic-gate char *msg;
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", msg);
537c478bd9Sstevel@tonic-gate quit();
547c478bd9Sstevel@tonic-gate }
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate * quit - exit the program with the status in Saferrno
597c478bd9Sstevel@tonic-gate */
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate void
quit()627c478bd9Sstevel@tonic-gate quit()
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate exit(Saferrno);
657c478bd9Sstevel@tonic-gate }
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate * make_tempname - generate a temp name to be used for updating files.
707c478bd9Sstevel@tonic-gate * Names will be of the form HOME/xxx/.name, where HOME
717c478bd9Sstevel@tonic-gate * is from misc.h
727c478bd9Sstevel@tonic-gate *
737c478bd9Sstevel@tonic-gate * args: bname - the basename of the file. For example foo/_config
747c478bd9Sstevel@tonic-gate * will generate a tempname of HOME/foo/._config
757c478bd9Sstevel@tonic-gate */
767c478bd9Sstevel@tonic-gate
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate char *
make_tempname(bname)797c478bd9Sstevel@tonic-gate make_tempname(bname)
807c478bd9Sstevel@tonic-gate char *bname;
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate static char buf[SIZE]; /* this is where we put the new name */
837c478bd9Sstevel@tonic-gate char *p; /* work pointer */
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate p = strrchr(bname, '/');
867c478bd9Sstevel@tonic-gate if (p == NULL)
877c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%s/.%s", HOME, bname);
887c478bd9Sstevel@tonic-gate else {
897c478bd9Sstevel@tonic-gate (void) strcpy(buf, HOME);
907c478bd9Sstevel@tonic-gate /* this zaps the trailing slash so the '.' can be stuck in */
917c478bd9Sstevel@tonic-gate *p = '\0';
927c478bd9Sstevel@tonic-gate (void) strcat(buf, "/");
937c478bd9Sstevel@tonic-gate (void) strcat(buf, bname);
947c478bd9Sstevel@tonic-gate (void) strcat(buf, "/.");
957c478bd9Sstevel@tonic-gate (void) strcat(buf, (p + 1));
967c478bd9Sstevel@tonic-gate *p = '/';
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate return(buf);
997c478bd9Sstevel@tonic-gate }
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate * open_temp - open up a temp file
1047c478bd9Sstevel@tonic-gate *
1057c478bd9Sstevel@tonic-gate * args: tname - temp file name
1067c478bd9Sstevel@tonic-gate */
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate FILE *
open_temp(tname)1117c478bd9Sstevel@tonic-gate open_temp(tname)
1127c478bd9Sstevel@tonic-gate char *tname;
1137c478bd9Sstevel@tonic-gate {
1147c478bd9Sstevel@tonic-gate FILE *fp; /* fp associated with tname */
1157c478bd9Sstevel@tonic-gate struct sigaction sigact; /* for signal handling */
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate sigact.sa_flags = 0;
1187c478bd9Sstevel@tonic-gate sigact.sa_handler = SIG_IGN;
1197c478bd9Sstevel@tonic-gate (void) sigemptyset(&sigact.sa_mask);
1207c478bd9Sstevel@tonic-gate (void) sigaddset(&sigact.sa_mask, SIGHUP);
1217c478bd9Sstevel@tonic-gate (void) sigaddset(&sigact.sa_mask, SIGINT);
1227c478bd9Sstevel@tonic-gate (void) sigaddset(&sigact.sa_mask, SIGQUIT);
1237c478bd9Sstevel@tonic-gate (void) sigaction(SIGHUP, &sigact, NULL);
1247c478bd9Sstevel@tonic-gate (void) sigaction(SIGINT, &sigact, NULL);
1257c478bd9Sstevel@tonic-gate (void) sigaction(SIGQUIT, &sigact, NULL);
1267c478bd9Sstevel@tonic-gate (void) umask(0333);
1277c478bd9Sstevel@tonic-gate if (access(tname, 0) != -1) {
1287c478bd9Sstevel@tonic-gate Saferrno = E_SAFERR;
1297c478bd9Sstevel@tonic-gate error("tempfile busy; try again later");
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate fp = fopen(tname, "w");
1327c478bd9Sstevel@tonic-gate if (fp == NULL) {
1337c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
1347c478bd9Sstevel@tonic-gate error("cannot create tempfile");
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate return(fp);
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate
1397c478bd9Sstevel@tonic-gate
1407c478bd9Sstevel@tonic-gate /*
1417c478bd9Sstevel@tonic-gate * replace - replace one file with another, only returns on success
1427c478bd9Sstevel@tonic-gate *
1437c478bd9Sstevel@tonic-gate * args: fname - name of target file
1447c478bd9Sstevel@tonic-gate * tname - name of source file
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate void
replace(fname,tname)1497c478bd9Sstevel@tonic-gate replace(fname, tname)
1507c478bd9Sstevel@tonic-gate char *fname;
1517c478bd9Sstevel@tonic-gate char *tname;
1527c478bd9Sstevel@tonic-gate {
1537c478bd9Sstevel@tonic-gate char buf[SIZE]; /* scratch buffer */
1547c478bd9Sstevel@tonic-gate
1557c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%s/%s", HOME, fname);
1567c478bd9Sstevel@tonic-gate (void) unlink(buf);
1577c478bd9Sstevel@tonic-gate if (rename(tname, buf) < 0) {
1587c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
1597c478bd9Sstevel@tonic-gate (void) unlink(tname);
1607c478bd9Sstevel@tonic-gate quit();
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate * copy_file - copy information from one file to another, return 0 on
1677c478bd9Sstevel@tonic-gate * success, -1 on failure
1687c478bd9Sstevel@tonic-gate *
1697c478bd9Sstevel@tonic-gate * args: fp - source file's file pointer
1707c478bd9Sstevel@tonic-gate * tfp - destination file's file pointer
1717c478bd9Sstevel@tonic-gate * start - starting line number
1727c478bd9Sstevel@tonic-gate * finish - ending line number (-1 indicates entire file)
1737c478bd9Sstevel@tonic-gate */
1747c478bd9Sstevel@tonic-gate
175*34e48580Sdp int
copy_file(FILE * fp,FILE * tfp,int start,int finish)176*34e48580Sdp copy_file(FILE *fp, FILE *tfp, int start, int finish)
1777c478bd9Sstevel@tonic-gate {
178*34e48580Sdp int i; /* loop variable */
1797c478bd9Sstevel@tonic-gate char dummy[SIZE]; /* scratch buffer */
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate * always start from the beginning because line numbers are absolute
1837c478bd9Sstevel@tonic-gate */
1847c478bd9Sstevel@tonic-gate
1857c478bd9Sstevel@tonic-gate rewind(fp);
1867c478bd9Sstevel@tonic-gate
1877c478bd9Sstevel@tonic-gate /*
1887c478bd9Sstevel@tonic-gate * get to the starting point of interest
1897c478bd9Sstevel@tonic-gate */
1907c478bd9Sstevel@tonic-gate
1917c478bd9Sstevel@tonic-gate if (start != 1) {
1927c478bd9Sstevel@tonic-gate for (i = 1; i < start; i++)
1937c478bd9Sstevel@tonic-gate if (!fgets(dummy, SIZE, fp))
1947c478bd9Sstevel@tonic-gate return(-1);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate * copy as much as was requested
1997c478bd9Sstevel@tonic-gate */
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate if (finish != -1) {
2027c478bd9Sstevel@tonic-gate for (i = start; i <= finish; i++) {
2037c478bd9Sstevel@tonic-gate if (!fgets(dummy, SIZE, fp))
2047c478bd9Sstevel@tonic-gate return(-1);
2057c478bd9Sstevel@tonic-gate if (fputs(dummy, tfp) == EOF)
2067c478bd9Sstevel@tonic-gate return(-1);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate else {
2107c478bd9Sstevel@tonic-gate for (;;) {
2117c478bd9Sstevel@tonic-gate if (fgets(dummy, SIZE, fp) == NULL) {
2127c478bd9Sstevel@tonic-gate if (feof(fp))
2137c478bd9Sstevel@tonic-gate break;
2147c478bd9Sstevel@tonic-gate else
2157c478bd9Sstevel@tonic-gate return(-1);
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate if (fputs(dummy, tfp) == EOF)
2187c478bd9Sstevel@tonic-gate return(-1);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate return(0);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate * find_pm - find an entry in _sactab for a particular port monitor
2277c478bd9Sstevel@tonic-gate *
2287c478bd9Sstevel@tonic-gate * args: fp - file pointer for _sactab
2297c478bd9Sstevel@tonic-gate * pmtag - tag of port monitor we're looking for
2307c478bd9Sstevel@tonic-gate */
2317c478bd9Sstevel@tonic-gate
232*34e48580Sdp int
find_pm(FILE * fp,char * pmtag)233*34e48580Sdp find_pm(FILE *fp, char *pmtag)
2347c478bd9Sstevel@tonic-gate {
235*34e48580Sdp char *p; /* working pointer */
2367c478bd9Sstevel@tonic-gate int line = 0; /* line number we found entry on */
2377c478bd9Sstevel@tonic-gate struct sactab stab; /* place to hold parsed info */
2387c478bd9Sstevel@tonic-gate char buf[SIZE]; /* scratch buffer */
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate while (fgets(buf, SIZE, fp)) {
2417c478bd9Sstevel@tonic-gate line++;
2427c478bd9Sstevel@tonic-gate p = trim(buf);
2437c478bd9Sstevel@tonic-gate if (*p == '\0')
2447c478bd9Sstevel@tonic-gate continue;
2457c478bd9Sstevel@tonic-gate parse(p, &stab);
2467c478bd9Sstevel@tonic-gate if (!(strcmp(stab.sc_tag, pmtag)))
2477c478bd9Sstevel@tonic-gate return(line);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate if (!feof(fp)) {
2507c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
2517c478bd9Sstevel@tonic-gate error("error reading _sactab");
2527c478bd9Sstevel@tonic-gate /* NOTREACHED */
253*34e48580Sdp return (0);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate else
2567c478bd9Sstevel@tonic-gate return(0);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate
2597c478bd9Sstevel@tonic-gate
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate * do_config - take a config script and put it where it belongs or
2627c478bd9Sstevel@tonic-gate * output an existing one. Saferrno is set if any errors
2637c478bd9Sstevel@tonic-gate * are encountered. Calling routine may choose to quit or
2647c478bd9Sstevel@tonic-gate * continue, in which case Saferrno will stay set, but may
2657c478bd9Sstevel@tonic-gate * change value if another error is encountered.
2667c478bd9Sstevel@tonic-gate *
2677c478bd9Sstevel@tonic-gate * args: script - name of file containing script (if NULL, means output
2687c478bd9Sstevel@tonic-gate * existing one instead)
2697c478bd9Sstevel@tonic-gate * basename - name of script (relative to HOME (from misc.h))
2707c478bd9Sstevel@tonic-gate */
2717c478bd9Sstevel@tonic-gate
272*34e48580Sdp int
do_config(char * script,char * basename)273*34e48580Sdp do_config(char *script, char *basename)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate FILE *ifp; /* file pointer for source file */
2767c478bd9Sstevel@tonic-gate FILE *ofp; /* file pointer for target file */
2777c478bd9Sstevel@tonic-gate struct stat statbuf; /* file status info */
2787c478bd9Sstevel@tonic-gate char *tname; /* name of tempfile */
2797c478bd9Sstevel@tonic-gate char buf[SIZE]; /* scratch buffer */
2807c478bd9Sstevel@tonic-gate
2817c478bd9Sstevel@tonic-gate if (script) {
2827c478bd9Sstevel@tonic-gate /* we're installing a new configuration script */
2837c478bd9Sstevel@tonic-gate if (access(script, 0) == 0) {
2847c478bd9Sstevel@tonic-gate if (stat(script, &statbuf) < 0) {
2857c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
2867c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Could not stat <%s>\n", script);
2877c478bd9Sstevel@tonic-gate return(1);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
2907c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "warning - %s not a regular file - ignored\n", script);
2917c478bd9Sstevel@tonic-gate return(1);
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate else {
2957c478bd9Sstevel@tonic-gate Saferrno = E_NOEXIST;
2967c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Invalid request, %s does not exist\n", script);
2977c478bd9Sstevel@tonic-gate return(1);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate ifp = fopen(script, "r");
3007c478bd9Sstevel@tonic-gate if (ifp == NULL) {
3017c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Invalid request, can not open %s\n", script);
3027c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
3037c478bd9Sstevel@tonic-gate return(1);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate tname = make_tempname(basename);
3067c478bd9Sstevel@tonic-gate /* note - open_temp only returns if successful */
3077c478bd9Sstevel@tonic-gate ofp = open_temp(tname);
3087c478bd9Sstevel@tonic-gate while(fgets(buf, SIZE, ifp)) {
3097c478bd9Sstevel@tonic-gate if (fputs(buf, ofp) == EOF) {
3107c478bd9Sstevel@tonic-gate (void) unlink(tname);
3117c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
3127c478bd9Sstevel@tonic-gate error("error in writing tempfile");
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate (void) fclose(ifp);
3167c478bd9Sstevel@tonic-gate if (fclose(ofp) == EOF) {
3177c478bd9Sstevel@tonic-gate (void) unlink(tname);
3187c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
3197c478bd9Sstevel@tonic-gate error("error closing tempfile");
3207c478bd9Sstevel@tonic-gate }
3217c478bd9Sstevel@tonic-gate /* note - replace only returns if successful */
3227c478bd9Sstevel@tonic-gate replace(basename, tname);
3237c478bd9Sstevel@tonic-gate return(0);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate else {
3267c478bd9Sstevel@tonic-gate /* we're outputting a configuration script */
3277c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%s/%s", HOME, basename);
3287c478bd9Sstevel@tonic-gate if (access(buf, 0) < 0) {
3297c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Invalid request, script does not exist\n");
3307c478bd9Sstevel@tonic-gate Saferrno = E_NOEXIST;
3317c478bd9Sstevel@tonic-gate return(1);
3327c478bd9Sstevel@tonic-gate }
3337c478bd9Sstevel@tonic-gate ifp = fopen(buf, "r");
3347c478bd9Sstevel@tonic-gate if (ifp == NULL) {
3357c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "Invalid request, can not open script\n");
3367c478bd9Sstevel@tonic-gate Saferrno = E_SYSERR;
3377c478bd9Sstevel@tonic-gate return(1);
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate while (fgets(buf, SIZE, ifp))
3407c478bd9Sstevel@tonic-gate (void) fputs(buf, stdout);
3417c478bd9Sstevel@tonic-gate (void) fclose(ifp);
3427c478bd9Sstevel@tonic-gate return(0);
3437c478bd9Sstevel@tonic-gate }
3447c478bd9Sstevel@tonic-gate }
345