xref: /onnv-gate/usr/src/lib/libmail/common/getdomain.c (revision 6812:febeba71273d)
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
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211219Sraf 
22*6812Sraf /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*6812Sraf  * Use is subject to license terms.
25*6812Sraf  */
26*6812Sraf 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include "libmail.h"
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <ctype.h>
350Sstevel@tonic-gate #ifdef SVR4
360Sstevel@tonic-gate #include <sys/utsname.h>
370Sstevel@tonic-gate #include <sys/systeminfo.h>
380Sstevel@tonic-gate #endif
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #define	NMLN 512
410Sstevel@tonic-gate #ifdef SVR4
420Sstevel@tonic-gate #if SYS_NMLN > NMLN
430Sstevel@tonic-gate #undef NMLN
440Sstevel@tonic-gate #define	NMLN SYS_NMLN
450Sstevel@tonic-gate #endif
460Sstevel@tonic-gate #endif
470Sstevel@tonic-gate 
480Sstevel@tonic-gate static char *look4domain(char *, char *, int);
490Sstevel@tonic-gate static char *readdomain(char *, int);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate  *  NAME
530Sstevel@tonic-gate  *	maildomain() - retrieve the domain name
540Sstevel@tonic-gate  *
550Sstevel@tonic-gate  *  SYNOPSIS
560Sstevel@tonic-gate  *	char *maildomain()
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  *  DESCRIPTION
590Sstevel@tonic-gate  *	Retrieve the domain name from xgetenv("DOMAIN").
600Sstevel@tonic-gate  *	If that is not set, look in /etc/resolv.conf, /etc/inet/named.boot
610Sstevel@tonic-gate  *	and /etc/named.boot for "^domain[ ]+<domain>".
620Sstevel@tonic-gate  *	If that is not set, use sysinfo(SI_SRPC_DOMAIN) from
630Sstevel@tonic-gate  *	-lnsl. Otherwise, return an empty string.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /* read a file for the domain */
look4domain(char * file,char * buf,int size)670Sstevel@tonic-gate static char *look4domain(char *file, char *buf, int size)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate 	char *ret = 0;
700Sstevel@tonic-gate 	FILE *fp = fopen(file, "r");
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	if (!fp)
730Sstevel@tonic-gate 		return (0);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	while (fgets(buf, size, fp))
760Sstevel@tonic-gate 		if (strncmp(buf, "domain", 6) == 0)
770Sstevel@tonic-gate 	if (isspace(buf[6])) {
780Sstevel@tonic-gate 		char *x = skipspace(buf + 6);
790Sstevel@tonic-gate 		if (isgraph(*x)) {
800Sstevel@tonic-gate 			trimnl(x);
810Sstevel@tonic-gate 			strmove(buf, x);
820Sstevel@tonic-gate 			ret = buf;
830Sstevel@tonic-gate 			break;
840Sstevel@tonic-gate 		}
850Sstevel@tonic-gate 	}
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	(void) fclose(fp);
880Sstevel@tonic-gate 	return (ret);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /* read the domain from the xenvironment or one of the files */
readdomain(char * buf,int size)920Sstevel@tonic-gate static char *readdomain(char *buf, int size)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	char *ret;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if ((ret = xgetenv("DOMAIN")) != 0) {
970Sstevel@tonic-gate 		(void) strncpy(buf, ret, size);
980Sstevel@tonic-gate 		return (buf);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	if (((ret = look4domain("/etc/resolv.conf", buf, size)) != 0) ||
1020Sstevel@tonic-gate 	    ((ret = look4domain("/etc/inet/named.boot", buf, size)) != 0) ||
1030Sstevel@tonic-gate 	    ((ret = look4domain("/etc/named.boot", buf, size)) != 0))
1040Sstevel@tonic-gate 		return (ret);
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #ifdef SVR4
1070Sstevel@tonic-gate 	if (sysinfo(SI_SRPC_DOMAIN, buf, size) >= 0)
1080Sstevel@tonic-gate 		return (buf);
1090Sstevel@tonic-gate #endif
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	return (0);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate char *
maildomain(void)1150Sstevel@tonic-gate maildomain(void)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	static char *domain = 0;
1180Sstevel@tonic-gate 	static char dombuf[NMLN+1] = ".";
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	/* if we've already been here, return the info */
1210Sstevel@tonic-gate 	if (domain != 0)
122*6812Sraf 		return (domain);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	domain = readdomain(dombuf+1, NMLN);
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	/* Make certain that the domain begins with a single dot */
1270Sstevel@tonic-gate 	/* and does not have one at the end. */
1280Sstevel@tonic-gate 	if (domain) {
1290Sstevel@tonic-gate 		size_t len;
1300Sstevel@tonic-gate 		domain = dombuf;
1310Sstevel@tonic-gate 		while (*domain && *domain == '.')
1320Sstevel@tonic-gate 			domain++;
1330Sstevel@tonic-gate 		domain--;
1340Sstevel@tonic-gate 		len = strlen(domain);
1350Sstevel@tonic-gate 		while ((len > 0) && (domain[len-1] == '.'))
1360Sstevel@tonic-gate 			domain[--len] = '\0';
1370Sstevel@tonic-gate 	}
1380Sstevel@tonic-gate 	/* no domain information */
1390Sstevel@tonic-gate 	else
1400Sstevel@tonic-gate 		domain = "";
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate 	return (domain);
1430Sstevel@tonic-gate }
144