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