10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22132Srobinson
230Sstevel@tonic-gate /*
24*1219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
250Sstevel@tonic-gate * Use is subject to license terms.
260Sstevel@tonic-gate */
27*1219Sraf
280Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
290Sstevel@tonic-gate /* All Rights Reserved */
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
320Sstevel@tonic-gate * 4.3 BSD under license from the Regents of the University of
330Sstevel@tonic-gate * California.
340Sstevel@tonic-gate */
350Sstevel@tonic-gate
360Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
370Sstevel@tonic-gate
380Sstevel@tonic-gate /*
39*1219Sraf * Gets and sets the domain name of the system
400Sstevel@tonic-gate */
410Sstevel@tonic-gate
42*1219Sraf #include "mt.h"
430Sstevel@tonic-gate #include "rpc_mt.h"
440Sstevel@tonic-gate #include <stdio.h>
45132Srobinson #include <stdlib.h>
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <sys/utsname.h>
480Sstevel@tonic-gate #include <sys/systeminfo.h>
490Sstevel@tonic-gate #include <sys/time.h>
500Sstevel@tonic-gate #include <syslog.h>
510Sstevel@tonic-gate
520Sstevel@tonic-gate #ifndef SI_SRPC_DOMAIN
530Sstevel@tonic-gate #define use_file
540Sstevel@tonic-gate #endif
550Sstevel@tonic-gate
560Sstevel@tonic-gate #ifdef use_file
570Sstevel@tonic-gate char DOMAIN[] = "/etc/domain";
580Sstevel@tonic-gate #endif
590Sstevel@tonic-gate
600Sstevel@tonic-gate int setdomainname();
610Sstevel@tonic-gate
620Sstevel@tonic-gate #ifdef use_file
630Sstevel@tonic-gate static char *domainname;
640Sstevel@tonic-gate #endif
650Sstevel@tonic-gate
660Sstevel@tonic-gate extern mutex_t dname_lock;
670Sstevel@tonic-gate
680Sstevel@tonic-gate int
getdomainname(name,namelen)690Sstevel@tonic-gate getdomainname(name, namelen)
700Sstevel@tonic-gate char *name;
710Sstevel@tonic-gate int namelen;
720Sstevel@tonic-gate {
730Sstevel@tonic-gate #ifdef use_file
740Sstevel@tonic-gate FILE *domain_fd;
750Sstevel@tonic-gate char *line;
760Sstevel@tonic-gate
77132Srobinson (void) mutex_lock(&dname_lock);
780Sstevel@tonic-gate if (domainname) {
79132Srobinson (void) strncpy(name, domainname, namelen);
80132Srobinson (void) mutex_unlock(&dname_lock);
810Sstevel@tonic-gate return (0);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate
84132Srobinson domainname = calloc(1, 256);
850Sstevel@tonic-gate if (domainname == NULL) {
860Sstevel@tonic-gate syslog(LOG_ERR, "getdomainname : out of memory.");
87132Srobinson (void) mutex_unlock(&dname_lock);
880Sstevel@tonic-gate return (-1);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate
910Sstevel@tonic-gate if ((domain_fd = fopen(DOMAIN, "r")) == NULL) {
920Sstevel@tonic-gate
93132Srobinson (void) mutex_unlock(&dname_lock);
940Sstevel@tonic-gate return (-1);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate if (fscanf(domain_fd, "%s", domainname) == NULL) {
97132Srobinson (void) fclose(domain_fd);
98132Srobinson (void) mutex_unlock(&dname_lock);
990Sstevel@tonic-gate return (-1);
1000Sstevel@tonic-gate }
101132Srobinson (void) fclose(domain_fd);
1020Sstevel@tonic-gate (void) strncpy(name, domainname, namelen);
103132Srobinson (void) mutex_unlock(&dname_lock);
1040Sstevel@tonic-gate return (0);
1050Sstevel@tonic-gate #else
1060Sstevel@tonic-gate int sysinfostatus;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate sysinfostatus = sysinfo(SI_SRPC_DOMAIN, name, namelen);
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate return ((sysinfostatus < 0) ? -1 : 0);
1110Sstevel@tonic-gate #endif
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate int
setdomainname(domain,len)1150Sstevel@tonic-gate setdomainname(domain, len)
1160Sstevel@tonic-gate char *domain;
1170Sstevel@tonic-gate int len;
1180Sstevel@tonic-gate {
1190Sstevel@tonic-gate #ifdef use_file
1200Sstevel@tonic-gate
1210Sstevel@tonic-gate FILE *domain_fd;
1220Sstevel@tonic-gate
123132Srobinson (void) mutex_lock(&dname_lock);
1240Sstevel@tonic-gate if (domainname)
1250Sstevel@tonic-gate free(domainname);
1260Sstevel@tonic-gate
1270Sstevel@tonic-gate if ((domain_fd = fopen(DOMAIN, "w")) == NULL) {
128132Srobinson (void) mutex_unlock(&dname_lock);
1290Sstevel@tonic-gate return (-1);
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate if (fputs(domain, domain_fd) == NULL) {
132132Srobinson (void) mutex_unlock(&dname_lock);
1330Sstevel@tonic-gate return (-1);
1340Sstevel@tonic-gate }
135132Srobinson (void) fclose(domain_fd);
136132Srobinson domainname = calloc(1, 256);
1370Sstevel@tonic-gate if (domainname == NULL) {
1380Sstevel@tonic-gate syslog(LOG_ERR, "setdomainname : out of memory.");
139132Srobinson (void) mutex_unlock(&dname_lock);
1400Sstevel@tonic-gate return (-1);
1410Sstevel@tonic-gate }
1420Sstevel@tonic-gate (void) strncpy(domainname, domain, len);
143132Srobinson (void) mutex_unlock(&dname_lock);
1440Sstevel@tonic-gate return (0);
1450Sstevel@tonic-gate #else
1460Sstevel@tonic-gate int sysinfostatus;
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate sysinfostatus = sysinfo(SI_SET_SRPC_DOMAIN,
1490Sstevel@tonic-gate domain, len + 1); /* add null */
1500Sstevel@tonic-gate return ((sysinfostatus < 0) ? -1 : 0);
1510Sstevel@tonic-gate #endif
1520Sstevel@tonic-gate }
153