xref: /titanic_51/usr/src/cmd/setuname/setuname.c (revision 06e46062ef4f5f4b687cbafb4518fb123fe23920)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1997, by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"       /* SVr4.0 1.3 */
32 
33 /*
34  *  setuname [-t] [-s name] [-n node]
35  */
36 
37 /*
38  *  Header files referenced:
39  *	<stdio.h>	Standard I/O
40  *	<unistd.h>	Standard UNIX definitions
41  *	<string.h>	String handling
42  *	<fmtmsg.h>	Standard message generation
43  *	<ctype.h>	Character types
44  *	<errno.h>	Error handling
45  *	<signal.h>	Signal handling
46  *	<sys/types.h>	Data types
47  *	<sys/fcntl.h>	File control
48  *	<sys/utsname.h>	System Name
49  *	<sys/sys3b.h>	sys3b() definitions
50  *	<nlist.h>	Definitions for Sun symbol table entries
51  */
52 
53 #include	<stdio.h>
54 #include	<unistd.h>
55 #include	<string.h>
56 #include	<fmtmsg.h>
57 #include	<ctype.h>
58 #include	<errno.h>
59 #include	<signal.h>
60 #include	<sys/types.h>
61 #include	<sys/uio.h>
62 #include	<sys/fcntl.h>
63 #include	<sys/psw.h>
64 #include	<sys/utsname.h>
65 
66 #if u3b || u3b15 || u3b2
67 #include	<sys/sys3b.h>
68 #endif
69 
70 #if sun
71 #include	<nlist.h>
72 #include	<kvm.h>
73 #endif
74 
75 /*
76  * Externals referenced (and not defined in a header)
77  *	optind		index to the next arg for getopt()
78  *	opterr		FLAG, TRUE tells getopt() to write messages
79  *	optarg		Ptr to an option's argument
80  *	getopt()	Gets an option from the command line
81  *	putenv()	Writes values into the environment
82  *	exit()		Exit the process
83  *	access()	Check accessibility of a file
84  *	malloc()	Allocate a block of main memory
85  *	free()		Free allocated space
86  *	lseek()		Seek within a file
87  *	open()		Open a file
88  *	close()		Close an open file
89  */
90 
91 extern	int		optind;		/* argv[] index of next arg */
92 extern	int		opterr;		/* TRUE if getopt() is to print msgs */
93 extern	char	       *optarg;		/* Argument to parsed option */
94 extern	int		getopt();	/* Get an option from the command line */
95 extern	int	       	putenv();	/* Put a value into the environment */
96 extern	void		exit();		/* Exit the process */
97 extern	int		access();	/* Check the accessibility of a file */
98 extern	void	       *malloc();	/* Get a chunk of main memory */
99 extern	void		free();		/* Free alloc'd space */
100 extern	long		lseek();	/* Seek within a file */
101 extern	int		open();		/* Open a file */
102 extern	int		close();	/* Close an open a file */
103 
104 /*
105  *  L O C A L   D E F I N I T I O N S
106  */
107 
108 /*
109  * Constants
110  */
111 
112 #ifndef	TRUE
113 #define	TRUE		(1)
114 #endif
115 
116 #ifndef	FALSE
117 #define	FALSE		(0)
118 #endif
119 
120 #ifndef	NULL
121 #define	NULL		(0)
122 #endif
123 
124 #define	OPTSTRING	"tn:s:"
125 
126 #define	EX_OK		0
127 #define	EX_ERROR	1
128 
129 #define	RC_FILENAME	"/etc/rc2.d/S18setuname"
130 #define RC_DIRNAME	"/etc/rc2.d"
131 
132 
133 /*
134  *  Messages
135  */
136 
137 #define	E_USAGE		"usage: setuname [-t] [-s name] [-n node]"
138 #define	E_MISSING	"Either -s name or -n node must be specified"
139 #define	E_UNAME		"Unable to get existing uname values"
140 #define E_INVNAME	"System-name invalid: %s"
141 #define E_LONGNAME	"System-name too long: %s"
142 #define E_INVNODE	"Network node-name invalid: %s"
143 #define E_LONGNODE	"Network node-name too long: %s"
144 #define E_NOPERMS	"No permissions, request denied"
145 #define E_NOSUCHDIR	"Directory doesn't exist: %s"
146 #define	E_INTERNAL	"Internal error: %d"
147 
148 /*
149  * Macros:
150  *	stdmsg(r,l,s,t)	    Write a standard message.
151  *				'r' is the recoverability flag
152  *				'l' is the label
153  *				's' is the severity
154  *				't' is the text.
155  *	strend(p)	    Return the address of the end of a string
156  *			    (This is supposed to be defined in <sys/inline.h>
157  *			    but that file has string-handing def'ns that
158  *			    conflict with <string.h>, so we can't use it!
159  *			    MR dn89-04701 requests this fix.
160  */
161 
162 #define	stdmsg(r,l,s,t)	(void) fmtmsg(MM_PRINT|MM_UTIL|r,l,s,t,MM_NULLACT,MM_NULLTAG)
163 #define strend(p)       strrchr(p,'\0')
164 
165 /*
166  * Local functions:
167  *	setuname	Changes the system name and the network node name
168  */
169 
170 static int	setuname();		/* This does the "real" work */
171 
172 
173 /*
174  * Local data
175  *	lbl		Buffer for the standard message label
176  *	txt		Buffer for the standard message text
177  */
178 
179 static	char		lbl[MM_MXLABELLN+1];	/* Space for std msg label */
180 static	char		msg[MM_MXTXTLN+1];	/* Space for std msg text  */
181 
182 /*
183  *  int main(argc, argv)
184  *	int	argc
185  *	char   *argv;
186  */
187 
188 int
189 main(argc, argv)
190 	int	argc;			/* Argument count  */
191 	char   *argv[];			/* Argument vector */
192 {
193 	/* Automatic data */
194 	char	       *n_arg;			/* Ptr to arg for -n */
195 	char	       *s_arg;			/* Ptr to arg for -s */
196 	int		t_seen;			/* FLAG, -t option seen */
197 	char	       *cmdname;		/* Ptr to the command's name */
198 	char	       *p;			/* Temp pointer */
199 	int		usageerr;		/* FLAG, TRUE if usage error */
200 	int		exitcode;		/* Value to exit with */
201 	int		c;			/* Temp character */
202 	int		ok;			/* Flag, everything okay? */
203 
204 	/* Build the standard-message label */
205 	if (p = strrchr(argv[0], '/')) cmdname = p+1;
206 	else cmdname = argv[0];
207 	(void) strcat(strcpy(lbl, "UX:"), cmdname);
208 
209 	/* Make only the text in standard messages appear (SVR4.0 only) */
210 	(void) putenv("MSGVERB=text");
211 
212 
213 	/* Initializations */
214 	n_arg = s_arg = (char *) NULL;
215 	t_seen = FALSE;
216 
217 
218 	/*
219 	 * Parse command
220 	 */
221 
222 	usageerr = FALSE;
223 	opterr = FALSE;
224 	while (!usageerr && (c = getopt(argc, argv, OPTSTRING)) != EOF) switch(c) {
225 
226 	case 'n':			/* -n node */
227 	    if (n_arg) usageerr = TRUE;
228 	    else n_arg = optarg;
229 	    break;
230 
231 	case 's':			/* -s name */
232 	    if (s_arg) usageerr = TRUE;
233 	    else s_arg = optarg;
234 	    break;
235 
236 	case 't':			/* -t */
237 	    if (t_seen) usageerr = TRUE;
238 	    else t_seen = TRUE;
239 	    break;
240 
241 	default:			/* Something that doesn't exist */
242 	    usageerr = TRUE;
243 	}   /* switch() */
244 
245 	/* If there was a usage error, report the error and exit */
246 	if ((argc >= (optind+1)) || usageerr) {
247 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, E_USAGE);
248 	    exit(EX_ERROR);
249 	}
250 
251 	/* Either -n <node> or -s <name> has to be specified */
252 	if (!(n_arg || s_arg)) {
253 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, E_MISSING);
254 	    exit(EX_ERROR);
255 	}
256 
257 
258 	/*
259 	 * Validate arguments:
260 	 *  - The length of the system name must be less than SYS_NMLN-1
261 	 *    characters,
262 	 *  - The length of the network node-name must be less than
263 	 *    SYS_NMLN-1 characters,
264 	 *  - The system name must equal [a-zA-Z0-9-_]+,
265 	 *  - The network node-name must equal [a-zA-Z0-9-_]+.
266 	 */
267 
268 	/* Check the length and the character-set of the system name */
269 	if (s_arg) {
270 
271 	    /* Check length of the system name */
272 	    if (strlen(s_arg) > (size_t)(SYS_NMLN-1)) {
273 		(void) sprintf(msg, E_LONGNAME, s_arg);
274 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
275 		exit(EX_ERROR);
276 	    }
277 
278 	    /* Check the character-set */
279 	    ok = TRUE;
280 	    for (p = s_arg ; ok && *p ; p++) {
281 		if (!isalnum(*p) && (*p != '-') && (*p != '_')) ok = FALSE;
282 	    }
283 	    if (!ok || (p == s_arg)) {
284 		(void) sprintf(msg, E_INVNAME, s_arg);
285 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
286 		exit(EX_ERROR);
287 	    }
288 	}
289 
290 	/* Check the length and the character-set of the network node-name */
291 
292 	if (n_arg) {
293 
294 	    /* Check length of the network node-name */
295 	    if (strlen(n_arg) > (size_t)(SYS_NMLN-1)) {
296 		(void) sprintf(msg, E_LONGNODE, n_arg);
297 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
298 		exit(EX_ERROR);
299 	    }
300 
301 	    /* Check the character-set */
302 	    ok = TRUE;
303 	    for (p = n_arg ; ok && *p ; p++) {
304 		if (!isalnum(*p) && (*p != '-') && (*p != '_')) ok = FALSE;
305 	    }
306 	    if (!ok || (p == n_arg)) {
307 		(void) sprintf(msg, E_INVNODE, n_arg);
308 		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
309 		exit(EX_ERROR);
310 	    }
311 	}
312 
313 
314 	/*
315 	 * Make sure we have access to needed resources:
316 	 *   -  Read/write access to kernel memory (/dev/kmem)
317 	 *   -  If -t is not specified, read/write access to /etc/rc2.d
318 	 *   -  If -t is not specified, read access to /etc/rc2.d/S18setuname
319 	 */
320 
321 	if (access("/dev/kmem", R_OK|W_OK) == 0) {
322 	    if (access(RC_DIRNAME, R_OK|W_OK) == 0) {
323 		if ((access(RC_FILENAME, R_OK) != 0) &&
324 		    (access(RC_FILENAME, F_OK) == 0)) {
325 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, E_NOPERMS);
326 		    exit(EX_ERROR);
327 		}
328 	    }
329 	    else {
330 		if (access(RC_DIRNAME, F_OK) == 0) {
331 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, E_NOPERMS);
332 		    exit(EX_ERROR);
333 		}
334 		else {
335 		    (void) sprintf(msg, E_NOSUCHDIR, RC_DIRNAME);
336 		    stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
337 		    exit(EX_ERROR);
338 		}
339 	    }
340 	}
341 	else {
342 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, E_NOPERMS);
343 	    exit(EX_ERROR);
344 	}
345 
346 
347 	/* Attempt the setuname */
348 	if (setuname(t_seen, s_arg, n_arg) == 0) exitcode = EX_OK;
349 	else {
350 	    (void) sprintf(msg, E_INTERNAL, errno);
351 	    stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
352 	    exitcode = EX_ERROR;
353 	}
354 
355 	/* Finished */
356 	return (exitcode);
357 }  /* main() */
358 
359 /*
360  * int setuname(temp, name, node)
361  *	int	temp
362  *	char   *name
363  *	char   *node
364  *
365  *	Set any or all of the following machine parameters, either
366  *	temporarily or permanently, depending on <temp>.
367  *	    - System name
368  *	    - Network Node-name
369  */
370 
371 static int
372 setuname(temp, sysname, nodename)
373 	int	temp;		/* Set in kernel only flag */
374 	char   *sysname;	/* System name */
375 	char   *nodename;	/* Network node-name */
376 {
377 	/* Automatic Data */
378 	struct utsname	utsname;	/* Space for the kernel's utsname information */
379 #if u3b || u3b15 || u3b2
380 	struct s3bsym  *symbtbl;	/* The kernel's symbol table */
381 #endif
382 #if sun
383 	struct nlist nl[] = {
384 		{"utsname", 0, 0, 0, 0, 0},
385 		{NULL}
386 	};
387 	kvm_t *kd;
388 #endif
389 	uintptr_t	utsname_addr;	/* Addr of "utsname" in the kernel */
390 	char	       *sysnm = (char *)NULL;		/* System name to set (from file or arg) */
391 	char	       *nodenm = (char *)NULL;		/* Network node-name to set (from file or arg) */
392 	FILE	       *fd;		/* Std I/O File Descriptor for /etc/rc2.d/S18setuname */
393 	char	       *p;		/* Temp pointer */
394 	void	      (*oldsighup)();	/* Function to call for SIGHUP */
395 	void	      (*oldsigint)();	/* Function to call for SIGINT */
396 	int		rtncd;		/* Value to return to the caller */
397 	unsigned long	symbtblsz;	/* The size of the kernel's symbol table, in bytes */
398 	int		memfd;		/* File descriptor:  open kernel memory */
399 	int		i;		/* Temp counter */
400 
401 
402 	/* Nothing's gone wrong yet (but we've only just begun!) */
403 	rtncd = 0;
404 
405 
406 	/*
407 	 * Get the virtual address of the symbol "utsname" in the kernel
408 	 * so we can get set the system name and/or the network node-name
409 	 * directly in the kernel's memory space.
410 	 */
411 
412 #if u3b || u3b15 || u3b2
413 	if ((sys3b(S3BSYM, (struct s3bsym *) &symbtblsz, sizeof(symbtblsz)) == 0) &&
414 	    (symbtbl = (struct s3bsym *) malloc(symbtblsz))) {
415 
416 	    (void) sys3b(S3BSYM, symbtbl, symbtblsz);
417 	    p = (char *) symbtbl;
418 	    for (i = symbtbl->count; i-- && (strcmp(p, "utsname") != 0) ; p = S3BNXTSYM(p)) ;
419 	    if (i >= 0) utsname_addr = S3BSVAL(p);
420 	    else rtncd = -1;
421 	    free((void *) symbtbl);
422 
423 	} else rtncd = -1;
424 
425 #elif sun
426         /* Check out namelist and memory files. */
427 	if ((kd = kvm_open(NULL, NULL, NULL, O_RDWR, NULL)) == NULL)
428 		rtncd = -1;
429 	if (kvm_nlist(kd, nl) != 0)
430 		rtncd = -1;
431 	else if (nl[0].n_value == 0)
432 		rtncd = -1;
433 	else
434 		utsname_addr = (uintptr_t)nl[0].n_value;
435 #else
436 	if (nlist("/unix", nl) != 0)
437 		rtncd = -1;
438 #endif
439 	if (rtncd != 0) return(rtncd);
440 
441 	/*
442 	 * Open the kernel's memory, get the existing "utsname" structure,
443 	 * change the system name and/or the network node-name in that struct,
444 	 * write it back out to kernel memory, then close kernel memory.
445 	 */
446 #ifdef sun
447 	if (kvm_kread(kd, utsname_addr, &utsname, sizeof (utsname)) ==
448 	    sizeof (utsname)) {
449 		if (sysname)
450 			(void) strncpy(utsname.sysname, sysname,
451 			    sizeof (utsname.sysname));
452 		if (nodename)
453 			(void) strncpy(utsname.nodename, nodename,
454 			    sizeof (utsname.nodename));
455 		(void) kvm_kwrite(kd, utsname_addr, &utsname, sizeof (utsname));
456 		kvm_close(kd);
457 	} else
458 		return (-1);
459 #else /* sun */
460 	if ((memfd = open("/dev/kmem", O_RDWR, 0)) > 0) {
461 	    if ((lseek(memfd, (long) utsname_addr, SEEK_SET) != -1) &&
462 		(read(memfd, &utsname, sizeof(utsname)) == sizeof(utsname))) {
463 		if (sysname) (void) strncpy(utsname.sysname, sysname, sizeof(utsname.sysname));
464 		if (nodename) (void) strncpy(utsname.nodename, nodename, sizeof(utsname.nodename));
465 		(void) lseek(memfd, (long) utsname_addr, SEEK_SET);
466 		(void) write(memfd, &utsname, sizeof(utsname));
467 		(void) close(memfd);
468 	    } else rtncd = -1;
469 	} else rtncd = -1;
470 	if (rtncd != 0) return(rtncd);
471 #endif /* sun */
472 
473 
474 	/*
475 	 * If the "temp" flag is FALSE, we need to permanently set the
476 	 * system name in the file  /etc/rc2.d/S18setuname
477 	 */
478 
479 	if (!temp) {
480 	    /*
481 	     * If a name was specified by the caller, use that, otherwise, use
482 	     * whatever was in the "rc" file.
483 	     */
484 
485 		if (sysname) sysnm = sysname;
486 		if (nodename) nodenm = nodename;
487 
488 
489 	    /*
490 	     * Write the file /etc/rc2.d/S18setuname so that the system name is
491 	     * set on boots and state changes.
492 	     *
493 	     * DISABLED SIGNALS: SIGHUP, SIGINT
494 	     */
495 
496 	    /* Give us a reasonable chance to complete without interruptions */
497 		oldsighup = signal(SIGHUP, SIG_IGN);
498 		oldsigint = signal(SIGINT, SIG_IGN);
499 
500 	    /* Write the new setuname "rc" file */
501 		if (sysname != NULL) {
502 			if ((fd = fopen(RC_FILENAME, "w")) != (FILE *) NULL) {
503 				(void) fprintf(fd, "# %s\n", sysnm);
504 				(void) fprintf(fd, "#\n");
505 				(void) fprintf(fd, "# This script, generated by the setuname command,\n");
506 				(void) fprintf(fd, "# sets the system's system-name\n");
507 				(void) fprintf(fd, "#\n");
508 			if (sysnm && *sysnm)
509 				(void) fprintf(fd, "setuname -t -s %s\n", sysnm);
510 			(void) fclose(fd);
511 			} else return(rtncd = -1);
512 		}
513 
514 		if(nodename != NULL) {
515 			char curname[SYS_NMLN];
516 			int curlen;
517 			FILE *file;
518 
519 			if ((file = fopen("/etc/nodename", "r")) != NULL) {
520 				curlen = fread(curname, sizeof(char), SYS_NMLN, file);
521 				for (i = 0; i < curlen; i++) {
522 					if (curname[i] == '\n') {
523 						curname[i] = '\0';
524 						break;
525 					}
526 				}
527 				if (i == curlen) {
528 					curname[curlen] = '\0';
529 				}
530 				(void)fclose(file);
531 			} else {
532 				curname[0] = '\0';
533 			}
534 			if (strcmp(curname, nodenm) != 0) {
535 				if ((file = fopen("/etc/nodename", "w")) == NULL) {
536 					(void) fprintf(stderr, "setuname: error in writing name\n");
537 					exit(1);
538 				}
539 				if (fprintf(file, "%s\n", nodenm) < 0) {
540 					(void) fprintf(stderr, "setuname: error in writing name\n");
541 					exit(1);
542 				}
543 				(void)fclose(file);
544 			}
545 		}
546 	    /* Restore signal handling */
547 		(void) signal(SIGHUP, oldsighup);
548 		(void) signal(SIGINT, oldsigint);
549 	}	/* if (!temp) */
550 
551 	/* Fini */
552 	return(rtncd);
553 }
554