xref: /titanic_50/usr/src/cmd/setuname/setuname.c (revision 8279b0c8ffe70e1861fe29f0215bbad0a2083252)
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
5*8279b0c8Selowe  * Common Development and Distribution License (the "License").
6*8279b0c8Selowe  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
227c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*8279b0c8Selowe  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
27*8279b0c8Selowe  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"       /* SVr4.0 1.3 */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  *  setuname [-t] [-s name] [-n node]
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  *  Header files referenced:
387c478bd9Sstevel@tonic-gate  *	<stdio.h>	Standard I/O
397c478bd9Sstevel@tonic-gate  *	<unistd.h>	Standard UNIX definitions
407c478bd9Sstevel@tonic-gate  *	<string.h>	String handling
417c478bd9Sstevel@tonic-gate  *	<fmtmsg.h>	Standard message generation
427c478bd9Sstevel@tonic-gate  *	<ctype.h>	Character types
437c478bd9Sstevel@tonic-gate  *	<errno.h>	Error handling
447c478bd9Sstevel@tonic-gate  *	<signal.h>	Signal handling
457c478bd9Sstevel@tonic-gate  *	<sys/types.h>	Data types
467c478bd9Sstevel@tonic-gate  *	<sys/fcntl.h>	File control
477c478bd9Sstevel@tonic-gate  *	<sys/utsname.h>	System Name
487c478bd9Sstevel@tonic-gate  *	<sys/sys3b.h>	sys3b() definitions
497c478bd9Sstevel@tonic-gate  *	<nlist.h>	Definitions for Sun symbol table entries
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #include	<stdio.h>
537c478bd9Sstevel@tonic-gate #include	<unistd.h>
547c478bd9Sstevel@tonic-gate #include	<string.h>
557c478bd9Sstevel@tonic-gate #include	<fmtmsg.h>
567c478bd9Sstevel@tonic-gate #include	<ctype.h>
577c478bd9Sstevel@tonic-gate #include	<errno.h>
587c478bd9Sstevel@tonic-gate #include	<signal.h>
597c478bd9Sstevel@tonic-gate #include	<sys/types.h>
607c478bd9Sstevel@tonic-gate #include	<sys/uio.h>
617c478bd9Sstevel@tonic-gate #include	<sys/fcntl.h>
627c478bd9Sstevel@tonic-gate #include	<sys/psw.h>
637c478bd9Sstevel@tonic-gate #include	<sys/utsname.h>
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate #if u3b || u3b15 || u3b2
667c478bd9Sstevel@tonic-gate #include	<sys/sys3b.h>
677c478bd9Sstevel@tonic-gate #endif
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #if sun
707c478bd9Sstevel@tonic-gate #include	<nlist.h>
717c478bd9Sstevel@tonic-gate #include	<kvm.h>
727c478bd9Sstevel@tonic-gate #endif
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /*
757c478bd9Sstevel@tonic-gate  * Externals referenced (and not defined in a header)
767c478bd9Sstevel@tonic-gate  *	optind		index to the next arg for getopt()
777c478bd9Sstevel@tonic-gate  *	opterr		FLAG, TRUE tells getopt() to write messages
787c478bd9Sstevel@tonic-gate  *	optarg		Ptr to an option's argument
797c478bd9Sstevel@tonic-gate  *	getopt()	Gets an option from the command line
807c478bd9Sstevel@tonic-gate  *	putenv()	Writes values into the environment
817c478bd9Sstevel@tonic-gate  *	exit()		Exit the process
827c478bd9Sstevel@tonic-gate  *	access()	Check accessibility of a file
837c478bd9Sstevel@tonic-gate  *	malloc()	Allocate a block of main memory
847c478bd9Sstevel@tonic-gate  *	free()		Free allocated space
857c478bd9Sstevel@tonic-gate  *	lseek()		Seek within a file
867c478bd9Sstevel@tonic-gate  *	open()		Open a file
877c478bd9Sstevel@tonic-gate  *	close()		Close an open file
887c478bd9Sstevel@tonic-gate  */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate extern	int		optind;		/* argv[] index of next arg */
917c478bd9Sstevel@tonic-gate extern	int		opterr;		/* TRUE if getopt() is to print msgs */
927c478bd9Sstevel@tonic-gate extern	char	       *optarg;		/* Argument to parsed option */
937c478bd9Sstevel@tonic-gate extern	int		getopt();	/* Get an option from the command line */
947c478bd9Sstevel@tonic-gate extern	int	       	putenv();	/* Put a value into the environment */
957c478bd9Sstevel@tonic-gate extern	void		exit();		/* Exit the process */
967c478bd9Sstevel@tonic-gate extern	int		access();	/* Check the accessibility of a file */
977c478bd9Sstevel@tonic-gate extern	void	       *malloc();	/* Get a chunk of main memory */
987c478bd9Sstevel@tonic-gate extern	void		free();		/* Free alloc'd space */
997c478bd9Sstevel@tonic-gate extern	long		lseek();	/* Seek within a file */
1007c478bd9Sstevel@tonic-gate extern	int		open();		/* Open a file */
1017c478bd9Sstevel@tonic-gate extern	int		close();	/* Close an open a file */
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate /*
1047c478bd9Sstevel@tonic-gate  *  L O C A L   D E F I N I T I O N S
1057c478bd9Sstevel@tonic-gate  */
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Constants
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate #ifndef	TRUE
1127c478bd9Sstevel@tonic-gate #define	TRUE		(1)
1137c478bd9Sstevel@tonic-gate #endif
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #ifndef	FALSE
1167c478bd9Sstevel@tonic-gate #define	FALSE		(0)
1177c478bd9Sstevel@tonic-gate #endif
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate #ifndef	NULL
1207c478bd9Sstevel@tonic-gate #define	NULL		(0)
1217c478bd9Sstevel@tonic-gate #endif
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #define	OPTSTRING	"tn:s:"
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #define	EX_OK		0
1267c478bd9Sstevel@tonic-gate #define	EX_ERROR	1
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate #define	RC_FILENAME	"/etc/rc2.d/S18setuname"
1297c478bd9Sstevel@tonic-gate #define RC_DIRNAME	"/etc/rc2.d"
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate /*
1337c478bd9Sstevel@tonic-gate  *  Messages
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #define	E_USAGE		"usage: setuname [-t] [-s name] [-n node]"
1377c478bd9Sstevel@tonic-gate #define	E_MISSING	"Either -s name or -n node must be specified"
1387c478bd9Sstevel@tonic-gate #define	E_UNAME		"Unable to get existing uname values"
1397c478bd9Sstevel@tonic-gate #define E_INVNAME	"System-name invalid: %s"
1407c478bd9Sstevel@tonic-gate #define E_LONGNAME	"System-name too long: %s"
1417c478bd9Sstevel@tonic-gate #define E_INVNODE	"Network node-name invalid: %s"
1427c478bd9Sstevel@tonic-gate #define E_LONGNODE	"Network node-name too long: %s"
1437c478bd9Sstevel@tonic-gate #define E_NOPERMS	"No permissions, request denied"
1447c478bd9Sstevel@tonic-gate #define E_NOSUCHDIR	"Directory doesn't exist: %s"
1457c478bd9Sstevel@tonic-gate #define	E_INTERNAL	"Internal error: %d"
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate /*
1487c478bd9Sstevel@tonic-gate  * Macros:
1497c478bd9Sstevel@tonic-gate  *	stdmsg(r,l,s,t)	    Write a standard message.
1507c478bd9Sstevel@tonic-gate  *				'r' is the recoverability flag
1517c478bd9Sstevel@tonic-gate  *				'l' is the label
1527c478bd9Sstevel@tonic-gate  *				's' is the severity
1537c478bd9Sstevel@tonic-gate  *				't' is the text.
1547c478bd9Sstevel@tonic-gate  *	strend(p)	    Return the address of the end of a string
1557c478bd9Sstevel@tonic-gate  *			    (This is supposed to be defined in <sys/inline.h>
1567c478bd9Sstevel@tonic-gate  *			    but that file has string-handing def'ns that
1577c478bd9Sstevel@tonic-gate  *			    conflict with <string.h>, so we can't use it!
1587c478bd9Sstevel@tonic-gate  *			    MR dn89-04701 requests this fix.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #define	stdmsg(r,l,s,t)	(void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
1627c478bd9Sstevel@tonic-gate #define strend(p)       strrchr(p,'\0')
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate  * Local functions:
1667c478bd9Sstevel@tonic-gate  *	setuname	Changes the system name and the network node name
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate static int	setuname();		/* This does the "real" work */
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate /*
1737c478bd9Sstevel@tonic-gate  * Local data
1747c478bd9Sstevel@tonic-gate  *	lbl		Buffer for the standard message label
1757c478bd9Sstevel@tonic-gate  *	txt		Buffer for the standard message text
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static	char		lbl[MM_MXLABELLN+1];	/* Space for std msg label */
1797c478bd9Sstevel@tonic-gate static	char		msg[MM_MXTXTLN+1];	/* Space for std msg text  */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  *  int main(argc, argv)
1837c478bd9Sstevel@tonic-gate  *	int	argc
1847c478bd9Sstevel@tonic-gate  *	char   *argv;
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate int
main(argc,argv)1887c478bd9Sstevel@tonic-gate main(argc, argv)
1897c478bd9Sstevel@tonic-gate 	int	argc;			/* Argument count  */
1907c478bd9Sstevel@tonic-gate 	char   *argv[];			/* Argument vector */
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	/* Automatic data */
1937c478bd9Sstevel@tonic-gate 	char	       *n_arg;			/* Ptr to arg for -n */
1947c478bd9Sstevel@tonic-gate 	char	       *s_arg;			/* Ptr to arg for -s */
1957c478bd9Sstevel@tonic-gate 	int		t_seen;			/* FLAG, -t option seen */
1967c478bd9Sstevel@tonic-gate 	char	       *cmdname;		/* Ptr to the command's name */
1977c478bd9Sstevel@tonic-gate 	char	       *p;			/* Temp pointer */
1987c478bd9Sstevel@tonic-gate 	int		usageerr;		/* FLAG, TRUE if usage error */
1997c478bd9Sstevel@tonic-gate 	int		exitcode;		/* Value to exit with */
2007c478bd9Sstevel@tonic-gate 	int		c;			/* Temp character */
2017c478bd9Sstevel@tonic-gate 	int		ok;			/* Flag, everything okay? */
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	/* Build the standard-message label */
2047c478bd9Sstevel@tonic-gate 	if (p = strrchr(argv[0], '/')) cmdname = p+1;
2057c478bd9Sstevel@tonic-gate 	else cmdname = argv[0];
2067c478bd9Sstevel@tonic-gate 	(void) strcat(strcpy(lbl, "UX:"), cmdname);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/* Make only the text in standard messages appear (SVR4.0 only) */
2097c478bd9Sstevel@tonic-gate 	(void) putenv("MSGVERB=text");
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* Initializations */
2137c478bd9Sstevel@tonic-gate 	n_arg = s_arg = (char *) NULL;
2147c478bd9Sstevel@tonic-gate 	t_seen = FALSE;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/*
2187c478bd9Sstevel@tonic-gate 	 * Parse command
2197c478bd9Sstevel@tonic-gate 	 */
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	usageerr = FALSE;
2227c478bd9Sstevel@tonic-gate 	opterr = FALSE;
2237c478bd9Sstevel@tonic-gate 	while (!usageerr && (c = getopt(argc, argv, OPTSTRING)) != EOF) switch(c) {
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	case 'n':			/* -n node */
2267c478bd9Sstevel@tonic-gate 	    if (n_arg) usageerr = TRUE;
2277c478bd9Sstevel@tonic-gate 	    else n_arg = optarg;
2287c478bd9Sstevel@tonic-gate 	    break;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 	case 's':			/* -s name */
2317c478bd9Sstevel@tonic-gate 	    if (s_arg) usageerr = TRUE;
2327c478bd9Sstevel@tonic-gate 	    else s_arg = optarg;
2337c478bd9Sstevel@tonic-gate 	    break;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	case 't':			/* -t */
2367c478bd9Sstevel@tonic-gate 	    if (t_seen) usageerr = TRUE;
2377c478bd9Sstevel@tonic-gate 	    else t_seen = TRUE;
2387c478bd9Sstevel@tonic-gate 	    break;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	default:			/* Something that doesn't exist */
2417c478bd9Sstevel@tonic-gate 	    usageerr = TRUE;
2427c478bd9Sstevel@tonic-gate 	}   /* switch() */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/* If there was a usage error, report the error and exit */
2457c478bd9Sstevel@tonic-gate 	if ((argc >= (optind+1)) || usageerr) {
2467c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, E_USAGE);
2477c478bd9Sstevel@tonic-gate 	    exit(EX_ERROR);
2487c478bd9Sstevel@tonic-gate 	}
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	/* Either -n <node> or -s <name> has to be specified */
2517c478bd9Sstevel@tonic-gate 	if (!(n_arg || s_arg)) {
2527c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, E_MISSING);
2537c478bd9Sstevel@tonic-gate 	    exit(EX_ERROR);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/*
2587c478bd9Sstevel@tonic-gate 	 * Validate arguments:
2597c478bd9Sstevel@tonic-gate 	 *  - The length of the system name must be less than SYS_NMLN-1
2607c478bd9Sstevel@tonic-gate 	 *    characters,
2617c478bd9Sstevel@tonic-gate 	 *  - The length of the network node-name must be less than
2627c478bd9Sstevel@tonic-gate 	 *    SYS_NMLN-1 characters,
2637c478bd9Sstevel@tonic-gate 	 *  - The system name must equal [a-zA-Z0-9-_]+,
2647c478bd9Sstevel@tonic-gate 	 *  - The network node-name must equal [a-zA-Z0-9-_]+.
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	/* Check the length and the character-set of the system name */
2687c478bd9Sstevel@tonic-gate 	if (s_arg) {
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	    /* Check length of the system name */
2717c478bd9Sstevel@tonic-gate 	    if (strlen(s_arg) > (size_t)(SYS_NMLN-1)) {
2727c478bd9Sstevel@tonic-gate 		(void) sprintf(msg, E_LONGNAME, s_arg);
2737c478bd9Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
2747c478bd9Sstevel@tonic-gate 		exit(EX_ERROR);
2757c478bd9Sstevel@tonic-gate 	    }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	    /* Check the character-set */
2787c478bd9Sstevel@tonic-gate 	    ok = TRUE;
2797c478bd9Sstevel@tonic-gate 	    for (p = s_arg ; ok && *p ; p++) {
2807c478bd9Sstevel@tonic-gate 		if (!isalnum(*p) && (*p != '-') && (*p != '_')) ok = FALSE;
2817c478bd9Sstevel@tonic-gate 	    }
2827c478bd9Sstevel@tonic-gate 	    if (!ok || (p == s_arg)) {
2837c478bd9Sstevel@tonic-gate 		(void) sprintf(msg, E_INVNAME, s_arg);
2847c478bd9Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
2857c478bd9Sstevel@tonic-gate 		exit(EX_ERROR);
2867c478bd9Sstevel@tonic-gate 	    }
2877c478bd9Sstevel@tonic-gate 	}
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 	/* Check the length and the character-set of the network node-name */
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 	if (n_arg) {
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 	    /* Check length of the network node-name */
2947c478bd9Sstevel@tonic-gate 	    if (strlen(n_arg) > (size_t)(SYS_NMLN-1)) {
2957c478bd9Sstevel@tonic-gate 		(void) sprintf(msg, E_LONGNODE, n_arg);
2967c478bd9Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
2977c478bd9Sstevel@tonic-gate 		exit(EX_ERROR);
2987c478bd9Sstevel@tonic-gate 	    }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	    /* Check the character-set */
3017c478bd9Sstevel@tonic-gate 	    ok = TRUE;
3027c478bd9Sstevel@tonic-gate 	    for (p = n_arg ; ok && *p ; p++) {
3037c478bd9Sstevel@tonic-gate 		if (!isalnum(*p) && (*p != '-') && (*p != '_')) ok = FALSE;
3047c478bd9Sstevel@tonic-gate 	    }
3057c478bd9Sstevel@tonic-gate 	    if (!ok || (p == n_arg)) {
3067c478bd9Sstevel@tonic-gate 		(void) sprintf(msg, E_INVNODE, n_arg);
3077c478bd9Sstevel@tonic-gate 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
3087c478bd9Sstevel@tonic-gate 		exit(EX_ERROR);
3097c478bd9Sstevel@tonic-gate 	    }
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	/*
3147c478bd9Sstevel@tonic-gate 	 * Make sure we have access to needed resources:
3157c478bd9Sstevel@tonic-gate 	 *   -  Read/write access to kernel memory (/dev/kmem)
3167c478bd9Sstevel@tonic-gate 	 *   -  If -t is not specified, read/write access to /etc/rc2.d
3177c478bd9Sstevel@tonic-gate 	 *   -  If -t is not specified, read access to /etc/rc2.d/S18setuname
3187c478bd9Sstevel@tonic-gate 	 */
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	if (access("/dev/kmem", R_OK|W_OK) == 0) {
3217c478bd9Sstevel@tonic-gate 	    if (access(RC_DIRNAME, R_OK|W_OK) == 0) {
3227c478bd9Sstevel@tonic-gate 		if ((access(RC_FILENAME, R_OK) != 0) &&
3237c478bd9Sstevel@tonic-gate 		    (access(RC_FILENAME, F_OK) == 0)) {
3247c478bd9Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, E_NOPERMS);
3257c478bd9Sstevel@tonic-gate 		    exit(EX_ERROR);
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 	    }
3287c478bd9Sstevel@tonic-gate 	    else {
3297c478bd9Sstevel@tonic-gate 		if (access(RC_DIRNAME, F_OK) == 0) {
3307c478bd9Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, E_NOPERMS);
3317c478bd9Sstevel@tonic-gate 		    exit(EX_ERROR);
3327c478bd9Sstevel@tonic-gate 		}
3337c478bd9Sstevel@tonic-gate 		else {
3347c478bd9Sstevel@tonic-gate 		    (void) sprintf(msg, E_NOSUCHDIR, RC_DIRNAME);
3357c478bd9Sstevel@tonic-gate 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
3367c478bd9Sstevel@tonic-gate 		    exit(EX_ERROR);
3377c478bd9Sstevel@tonic-gate 		}
3387c478bd9Sstevel@tonic-gate 	    }
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 	else {
3417c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, E_NOPERMS);
3427c478bd9Sstevel@tonic-gate 	    exit(EX_ERROR);
3437c478bd9Sstevel@tonic-gate 	}
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	/* Attempt the setuname */
3477c478bd9Sstevel@tonic-gate 	if (setuname(t_seen, s_arg, n_arg) == 0) exitcode = EX_OK;
3487c478bd9Sstevel@tonic-gate 	else {
3497c478bd9Sstevel@tonic-gate 	    (void) sprintf(msg, E_INTERNAL, errno);
3507c478bd9Sstevel@tonic-gate 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
3517c478bd9Sstevel@tonic-gate 	    exitcode = EX_ERROR;
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/* Finished */
3557c478bd9Sstevel@tonic-gate 	return (exitcode);
3567c478bd9Sstevel@tonic-gate }  /* main() */
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate /*
3597c478bd9Sstevel@tonic-gate  * int setuname(temp, name, node)
3607c478bd9Sstevel@tonic-gate  *	int	temp
3617c478bd9Sstevel@tonic-gate  *	char   *name
3627c478bd9Sstevel@tonic-gate  *	char   *node
3637c478bd9Sstevel@tonic-gate  *
3647c478bd9Sstevel@tonic-gate  *	Set any or all of the following machine parameters, either
3657c478bd9Sstevel@tonic-gate  *	temporarily or permanently, depending on <temp>.
3667c478bd9Sstevel@tonic-gate  *	    - System name
3677c478bd9Sstevel@tonic-gate  *	    - Network Node-name
3687c478bd9Sstevel@tonic-gate  */
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate static int
setuname(temp,sysname,nodename)3717c478bd9Sstevel@tonic-gate setuname(temp, sysname, nodename)
3727c478bd9Sstevel@tonic-gate 	int	temp;		/* Set in kernel only flag */
3737c478bd9Sstevel@tonic-gate 	char   *sysname;	/* System name */
3747c478bd9Sstevel@tonic-gate 	char   *nodename;	/* Network node-name */
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	/* Automatic Data */
3777c478bd9Sstevel@tonic-gate 	struct utsname	utsname;	/* Space for the kernel's utsname information */
3787c478bd9Sstevel@tonic-gate #if u3b || u3b15 || u3b2
3797c478bd9Sstevel@tonic-gate 	struct s3bsym  *symbtbl;	/* The kernel's symbol table */
3807c478bd9Sstevel@tonic-gate #endif
3817c478bd9Sstevel@tonic-gate #if sun
3827c478bd9Sstevel@tonic-gate 	struct nlist nl[] = {
3837c478bd9Sstevel@tonic-gate 		{"utsname", 0, 0, 0, 0, 0},
3847c478bd9Sstevel@tonic-gate 		{NULL}
3857c478bd9Sstevel@tonic-gate 	};
3867c478bd9Sstevel@tonic-gate 	kvm_t *kd;
3877c478bd9Sstevel@tonic-gate #endif
3887c478bd9Sstevel@tonic-gate 	uintptr_t	utsname_addr;	/* Addr of "utsname" in the kernel */
3897c478bd9Sstevel@tonic-gate 	char	       *sysnm = (char *)NULL;		/* System name to set (from file or arg) */
3907c478bd9Sstevel@tonic-gate 	char	       *nodenm = (char *)NULL;		/* Network node-name to set (from file or arg) */
3917c478bd9Sstevel@tonic-gate 	FILE	       *fd;		/* Std I/O File Descriptor for /etc/rc2.d/S18setuname */
3927c478bd9Sstevel@tonic-gate 	char	       *p;		/* Temp pointer */
3937c478bd9Sstevel@tonic-gate 	void	      (*oldsighup)();	/* Function to call for SIGHUP */
3947c478bd9Sstevel@tonic-gate 	void	      (*oldsigint)();	/* Function to call for SIGINT */
3957c478bd9Sstevel@tonic-gate 	int		rtncd;		/* Value to return to the caller */
3967c478bd9Sstevel@tonic-gate 	unsigned long	symbtblsz;	/* The size of the kernel's symbol table, in bytes */
3977c478bd9Sstevel@tonic-gate 	int		memfd;		/* File descriptor:  open kernel memory */
3987c478bd9Sstevel@tonic-gate 	int		i;		/* Temp counter */
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	/* Nothing's gone wrong yet (but we've only just begun!) */
4027c478bd9Sstevel@tonic-gate 	rtncd = 0;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	/*
4067c478bd9Sstevel@tonic-gate 	 * Get the virtual address of the symbol "utsname" in the kernel
4077c478bd9Sstevel@tonic-gate 	 * so we can get set the system name and/or the network node-name
4087c478bd9Sstevel@tonic-gate 	 * directly in the kernel's memory space.
4097c478bd9Sstevel@tonic-gate 	 */
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate #if u3b || u3b15 || u3b2
4127c478bd9Sstevel@tonic-gate 	if ((sys3b(S3BSYM, (struct s3bsym *) &symbtblsz, sizeof(symbtblsz)) == 0) &&
4137c478bd9Sstevel@tonic-gate 	    (symbtbl = (struct s3bsym *) malloc(symbtblsz))) {
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	    (void) sys3b(S3BSYM, symbtbl, symbtblsz);
4167c478bd9Sstevel@tonic-gate 	    p = (char *) symbtbl;
4177c478bd9Sstevel@tonic-gate 	    for (i = symbtbl->count; i-- && (strcmp(p, "utsname") != 0) ; p = S3BNXTSYM(p)) ;
4187c478bd9Sstevel@tonic-gate 	    if (i >= 0) utsname_addr = S3BSVAL(p);
4197c478bd9Sstevel@tonic-gate 	    else rtncd = -1;
4207c478bd9Sstevel@tonic-gate 	    free((void *) symbtbl);
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	} else rtncd = -1;
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate #elif sun
4257c478bd9Sstevel@tonic-gate         /* Check out namelist and memory files. */
4267c478bd9Sstevel@tonic-gate 	if ((kd = kvm_open(NULL, NULL, NULL, O_RDWR, NULL)) == NULL)
4277c478bd9Sstevel@tonic-gate 		rtncd = -1;
428*8279b0c8Selowe 	else if (kvm_nlist(kd, nl) != 0)
4297c478bd9Sstevel@tonic-gate 		rtncd = -1;
4307c478bd9Sstevel@tonic-gate 	else if (nl[0].n_value == 0)
4317c478bd9Sstevel@tonic-gate 		rtncd = -1;
4327c478bd9Sstevel@tonic-gate 	else
4337c478bd9Sstevel@tonic-gate 		utsname_addr = (uintptr_t)nl[0].n_value;
4347c478bd9Sstevel@tonic-gate #else
4357c478bd9Sstevel@tonic-gate 	if (nlist("/unix", nl) != 0)
4367c478bd9Sstevel@tonic-gate 		rtncd = -1;
4377c478bd9Sstevel@tonic-gate #endif
4387c478bd9Sstevel@tonic-gate 	if (rtncd != 0) return(rtncd);
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	/*
4417c478bd9Sstevel@tonic-gate 	 * Open the kernel's memory, get the existing "utsname" structure,
4427c478bd9Sstevel@tonic-gate 	 * change the system name and/or the network node-name in that struct,
4437c478bd9Sstevel@tonic-gate 	 * write it back out to kernel memory, then close kernel memory.
4447c478bd9Sstevel@tonic-gate 	 */
4457c478bd9Sstevel@tonic-gate #ifdef sun
4467c478bd9Sstevel@tonic-gate 	if (kvm_kread(kd, utsname_addr, &utsname, sizeof (utsname)) ==
4477c478bd9Sstevel@tonic-gate 	    sizeof (utsname)) {
4487c478bd9Sstevel@tonic-gate 		if (sysname)
4497c478bd9Sstevel@tonic-gate 			(void) strncpy(utsname.sysname, sysname,
4507c478bd9Sstevel@tonic-gate 			    sizeof (utsname.sysname));
4517c478bd9Sstevel@tonic-gate 		if (nodename)
4527c478bd9Sstevel@tonic-gate 			(void) strncpy(utsname.nodename, nodename,
4537c478bd9Sstevel@tonic-gate 			    sizeof (utsname.nodename));
4547c478bd9Sstevel@tonic-gate 		(void) kvm_kwrite(kd, utsname_addr, &utsname, sizeof (utsname));
4557c478bd9Sstevel@tonic-gate 		kvm_close(kd);
4567c478bd9Sstevel@tonic-gate 	} else
4577c478bd9Sstevel@tonic-gate 		return (-1);
4587c478bd9Sstevel@tonic-gate #else /* sun */
4597c478bd9Sstevel@tonic-gate 	if ((memfd = open("/dev/kmem", O_RDWR, 0)) > 0) {
4607c478bd9Sstevel@tonic-gate 	    if ((lseek(memfd, (long) utsname_addr, SEEK_SET) != -1) &&
4617c478bd9Sstevel@tonic-gate 		(read(memfd, &utsname, sizeof(utsname)) == sizeof(utsname))) {
4627c478bd9Sstevel@tonic-gate 		if (sysname) (void) strncpy(utsname.sysname, sysname, sizeof(utsname.sysname));
4637c478bd9Sstevel@tonic-gate 		if (nodename) (void) strncpy(utsname.nodename, nodename, sizeof(utsname.nodename));
4647c478bd9Sstevel@tonic-gate 		(void) lseek(memfd, (long) utsname_addr, SEEK_SET);
4657c478bd9Sstevel@tonic-gate 		(void) write(memfd, &utsname, sizeof(utsname));
4667c478bd9Sstevel@tonic-gate 		(void) close(memfd);
4677c478bd9Sstevel@tonic-gate 	    } else rtncd = -1;
4687c478bd9Sstevel@tonic-gate 	} else rtncd = -1;
4697c478bd9Sstevel@tonic-gate 	if (rtncd != 0) return(rtncd);
4707c478bd9Sstevel@tonic-gate #endif /* sun */
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	/*
4747c478bd9Sstevel@tonic-gate 	 * If the "temp" flag is FALSE, we need to permanently set the
4757c478bd9Sstevel@tonic-gate 	 * system name in the file  /etc/rc2.d/S18setuname
4767c478bd9Sstevel@tonic-gate 	 */
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	if (!temp) {
4797c478bd9Sstevel@tonic-gate 	    /*
4807c478bd9Sstevel@tonic-gate 	     * If a name was specified by the caller, use that, otherwise, use
4817c478bd9Sstevel@tonic-gate 	     * whatever was in the "rc" file.
4827c478bd9Sstevel@tonic-gate 	     */
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 		if (sysname) sysnm = sysname;
4857c478bd9Sstevel@tonic-gate 		if (nodename) nodenm = nodename;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 	    /*
4897c478bd9Sstevel@tonic-gate 	     * Write the file /etc/rc2.d/S18setuname so that the system name is
4907c478bd9Sstevel@tonic-gate 	     * set on boots and state changes.
4917c478bd9Sstevel@tonic-gate 	     *
4927c478bd9Sstevel@tonic-gate 	     * DISABLED SIGNALS: SIGHUP, SIGINT
4937c478bd9Sstevel@tonic-gate 	     */
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	    /* Give us a reasonable chance to complete without interruptions */
4967c478bd9Sstevel@tonic-gate 		oldsighup = signal(SIGHUP, SIG_IGN);
4977c478bd9Sstevel@tonic-gate 		oldsigint = signal(SIGINT, SIG_IGN);
4987c478bd9Sstevel@tonic-gate 
4997c478bd9Sstevel@tonic-gate 	    /* Write the new setuname "rc" file */
5007c478bd9Sstevel@tonic-gate 		if (sysname != NULL) {
5017c478bd9Sstevel@tonic-gate 			if ((fd = fopen(RC_FILENAME, "w")) != (FILE *) NULL) {
5027c478bd9Sstevel@tonic-gate 				(void) fprintf(fd, "# %s\n", sysnm);
5037c478bd9Sstevel@tonic-gate 				(void) fprintf(fd, "#\n");
5047c478bd9Sstevel@tonic-gate 				(void) fprintf(fd, "# This script, generated by the setuname command,\n");
5057c478bd9Sstevel@tonic-gate 				(void) fprintf(fd, "# sets the system's system-name\n");
5067c478bd9Sstevel@tonic-gate 				(void) fprintf(fd, "#\n");
5077c478bd9Sstevel@tonic-gate 			if (sysnm && *sysnm)
5087c478bd9Sstevel@tonic-gate 				(void) fprintf(fd, "setuname -t -s %s\n", sysnm);
5097c478bd9Sstevel@tonic-gate 			(void) fclose(fd);
5107c478bd9Sstevel@tonic-gate 			} else return(rtncd = -1);
5117c478bd9Sstevel@tonic-gate 		}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		if(nodename != NULL) {
5147c478bd9Sstevel@tonic-gate 			char curname[SYS_NMLN];
5157c478bd9Sstevel@tonic-gate 			int curlen;
5167c478bd9Sstevel@tonic-gate 			FILE *file;
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 			if ((file = fopen("/etc/nodename", "r")) != NULL) {
5197c478bd9Sstevel@tonic-gate 				curlen = fread(curname, sizeof(char), SYS_NMLN, file);
5207c478bd9Sstevel@tonic-gate 				for (i = 0; i < curlen; i++) {
5217c478bd9Sstevel@tonic-gate 					if (curname[i] == '\n') {
5227c478bd9Sstevel@tonic-gate 						curname[i] = '\0';
5237c478bd9Sstevel@tonic-gate 						break;
5247c478bd9Sstevel@tonic-gate 					}
5257c478bd9Sstevel@tonic-gate 				}
5267c478bd9Sstevel@tonic-gate 				if (i == curlen) {
5277c478bd9Sstevel@tonic-gate 					curname[curlen] = '\0';
5287c478bd9Sstevel@tonic-gate 				}
5297c478bd9Sstevel@tonic-gate 				(void)fclose(file);
5307c478bd9Sstevel@tonic-gate 			} else {
5317c478bd9Sstevel@tonic-gate 				curname[0] = '\0';
5327c478bd9Sstevel@tonic-gate 			}
5337c478bd9Sstevel@tonic-gate 			if (strcmp(curname, nodenm) != 0) {
5347c478bd9Sstevel@tonic-gate 				if ((file = fopen("/etc/nodename", "w")) == NULL) {
5357c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "setuname: error in writing name\n");
5367c478bd9Sstevel@tonic-gate 					exit(1);
5377c478bd9Sstevel@tonic-gate 				}
5387c478bd9Sstevel@tonic-gate 				if (fprintf(file, "%s\n", nodenm) < 0) {
5397c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, "setuname: error in writing name\n");
5407c478bd9Sstevel@tonic-gate 					exit(1);
5417c478bd9Sstevel@tonic-gate 				}
5427c478bd9Sstevel@tonic-gate 				(void)fclose(file);
5437c478bd9Sstevel@tonic-gate 			}
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 	    /* Restore signal handling */
5467c478bd9Sstevel@tonic-gate 		(void) signal(SIGHUP, oldsighup);
5477c478bd9Sstevel@tonic-gate 		(void) signal(SIGINT, oldsigint);
5487c478bd9Sstevel@tonic-gate 	}	/* if (!temp) */
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	/* Fini */
5517c478bd9Sstevel@tonic-gate 	return(rtncd);
5527c478bd9Sstevel@tonic-gate }
553