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