xref: /onnv-gate/usr/src/lib/libmail/common/getdomain.c (revision 1219:f89f56c2d9ac)
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  */
22*1219Sraf 
230Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
240Sstevel@tonic-gate /*	  All Rights Reserved  	*/
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
27*1219Sraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
28*1219Sraf  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate /*LINTLIBRARY*/
330Sstevel@tonic-gate 
34*1219Sraf #include "c_synonyms.h"
350Sstevel@tonic-gate #include "libmail.h"
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate #ifdef SVR4
390Sstevel@tonic-gate #include <sys/utsname.h>
400Sstevel@tonic-gate #include <sys/systeminfo.h>
410Sstevel@tonic-gate #endif
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #define	NMLN 512
440Sstevel@tonic-gate #ifdef SVR4
450Sstevel@tonic-gate #if SYS_NMLN > NMLN
460Sstevel@tonic-gate #undef NMLN
470Sstevel@tonic-gate #define	NMLN SYS_NMLN
480Sstevel@tonic-gate #endif
490Sstevel@tonic-gate #endif
500Sstevel@tonic-gate 
510Sstevel@tonic-gate static char *look4domain(char *, char *, int);
520Sstevel@tonic-gate static char *readdomain(char *, int);
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  *  NAME
560Sstevel@tonic-gate  *	maildomain() - retrieve the domain name
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  *  SYNOPSIS
590Sstevel@tonic-gate  *	char *maildomain()
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  *  DESCRIPTION
620Sstevel@tonic-gate  *	Retrieve the domain name from xgetenv("DOMAIN").
630Sstevel@tonic-gate  *	If that is not set, look in /etc/resolv.conf, /etc/inet/named.boot
640Sstevel@tonic-gate  *	and /etc/named.boot for "^domain[ ]+<domain>".
650Sstevel@tonic-gate  *	If that is not set, use sysinfo(SI_SRPC_DOMAIN) from
660Sstevel@tonic-gate  *	-lnsl. Otherwise, return an empty string.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /* read a file for the domain */
700Sstevel@tonic-gate static char *look4domain(char *file, char *buf, int size)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	char *ret = 0;
730Sstevel@tonic-gate 	FILE *fp = fopen(file, "r");
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (!fp)
760Sstevel@tonic-gate 		return (0);
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	while (fgets(buf, size, fp))
790Sstevel@tonic-gate 		if (strncmp(buf, "domain", 6) == 0)
800Sstevel@tonic-gate 	if (isspace(buf[6])) {
810Sstevel@tonic-gate 		char *x = skipspace(buf + 6);
820Sstevel@tonic-gate 		if (isgraph(*x)) {
830Sstevel@tonic-gate 			trimnl(x);
840Sstevel@tonic-gate 			strmove(buf, x);
850Sstevel@tonic-gate 			ret = buf;
860Sstevel@tonic-gate 			break;
870Sstevel@tonic-gate 		}
880Sstevel@tonic-gate 	}
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	(void) fclose(fp);
910Sstevel@tonic-gate 	return (ret);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate /* read the domain from the xenvironment or one of the files */
950Sstevel@tonic-gate static char *readdomain(char *buf, int size)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	char *ret;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	if ((ret = xgetenv("DOMAIN")) != 0) {
1000Sstevel@tonic-gate 		(void) strncpy(buf, ret, size);
1010Sstevel@tonic-gate 		return (buf);
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if (((ret = look4domain("/etc/resolv.conf", buf, size)) != 0) ||
1050Sstevel@tonic-gate 	    ((ret = look4domain("/etc/inet/named.boot", buf, size)) != 0) ||
1060Sstevel@tonic-gate 	    ((ret = look4domain("/etc/named.boot", buf, size)) != 0))
1070Sstevel@tonic-gate 		return (ret);
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate #ifdef SVR4
1100Sstevel@tonic-gate 	if (sysinfo(SI_SRPC_DOMAIN, buf, size) >= 0)
1110Sstevel@tonic-gate 		return (buf);
1120Sstevel@tonic-gate #endif
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	return (0);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate char *
1180Sstevel@tonic-gate maildomain(void)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	static char *domain = 0;
1210Sstevel@tonic-gate 	static char dombuf[NMLN+1] = ".";
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate 	/* if we've already been here, return the info */
1240Sstevel@tonic-gate 	if (domain != 0)
1250Sstevel@tonic-gate 	    return (domain);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	domain = readdomain(dombuf+1, NMLN);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	/* Make certain that the domain begins with a single dot */
1300Sstevel@tonic-gate 	/* and does not have one at the end. */
1310Sstevel@tonic-gate 	if (domain) {
1320Sstevel@tonic-gate 		size_t len;
1330Sstevel@tonic-gate 		domain = dombuf;
1340Sstevel@tonic-gate 		while (*domain && *domain == '.')
1350Sstevel@tonic-gate 			domain++;
1360Sstevel@tonic-gate 		domain--;
1370Sstevel@tonic-gate 		len = strlen(domain);
1380Sstevel@tonic-gate 		while ((len > 0) && (domain[len-1] == '.'))
1390Sstevel@tonic-gate 			domain[--len] = '\0';
1400Sstevel@tonic-gate 	}
1410Sstevel@tonic-gate 	/* no domain information */
1420Sstevel@tonic-gate 	else
1430Sstevel@tonic-gate 		domain = "";
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	return (domain);
1460Sstevel@tonic-gate }
147