xref: /titanic_44/usr/src/lib/libmail/common/getdomain.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #include "libmail.h"
33 #include <sys/types.h>
34 #include <ctype.h>
35 #ifdef SVR4
36 #include <sys/utsname.h>
37 #include <sys/systeminfo.h>
38 #endif
39 
40 #define	NMLN 512
41 #ifdef SVR4
42 #if SYS_NMLN > NMLN
43 #undef NMLN
44 #define	NMLN SYS_NMLN
45 #endif
46 #endif
47 
48 static char *look4domain(char *, char *, int);
49 static char *readdomain(char *, int);
50 
51 /*
52  *  NAME
53  *	maildomain() - retrieve the domain name
54  *
55  *  SYNOPSIS
56  *	char *maildomain()
57  *
58  *  DESCRIPTION
59  *	Retrieve the domain name from xgetenv("DOMAIN").
60  *	If that is not set, look in /etc/resolv.conf, /etc/inet/named.boot
61  *	and /etc/named.boot for "^domain[ ]+<domain>".
62  *	If that is not set, use sysinfo(SI_SRPC_DOMAIN) from
63  *	-lnsl. Otherwise, return an empty string.
64  */
65 
66 /* read a file for the domain */
look4domain(char * file,char * buf,int size)67 static char *look4domain(char *file, char *buf, int size)
68 {
69 	char *ret = 0;
70 	FILE *fp = fopen(file, "r");
71 
72 	if (!fp)
73 		return (0);
74 
75 	while (fgets(buf, size, fp))
76 		if (strncmp(buf, "domain", 6) == 0)
77 	if (isspace(buf[6])) {
78 		char *x = skipspace(buf + 6);
79 		if (isgraph(*x)) {
80 			trimnl(x);
81 			strmove(buf, x);
82 			ret = buf;
83 			break;
84 		}
85 	}
86 
87 	(void) fclose(fp);
88 	return (ret);
89 }
90 
91 /* read the domain from the xenvironment or one of the files */
readdomain(char * buf,int size)92 static char *readdomain(char *buf, int size)
93 {
94 	char *ret;
95 
96 	if ((ret = xgetenv("DOMAIN")) != 0) {
97 		(void) strncpy(buf, ret, size);
98 		return (buf);
99 	}
100 
101 	if (((ret = look4domain("/etc/resolv.conf", buf, size)) != 0) ||
102 	    ((ret = look4domain("/etc/inet/named.boot", buf, size)) != 0) ||
103 	    ((ret = look4domain("/etc/named.boot", buf, size)) != 0))
104 		return (ret);
105 
106 #ifdef SVR4
107 	if (sysinfo(SI_SRPC_DOMAIN, buf, size) >= 0)
108 		return (buf);
109 #endif
110 
111 	return (0);
112 }
113 
114 char *
maildomain(void)115 maildomain(void)
116 {
117 	static char *domain = 0;
118 	static char dombuf[NMLN+1] = ".";
119 
120 	/* if we've already been here, return the info */
121 	if (domain != 0)
122 		return (domain);
123 
124 	domain = readdomain(dombuf+1, NMLN);
125 
126 	/* Make certain that the domain begins with a single dot */
127 	/* and does not have one at the end. */
128 	if (domain) {
129 		size_t len;
130 		domain = dombuf;
131 		while (*domain && *domain == '.')
132 			domain++;
133 		domain--;
134 		len = strlen(domain);
135 		while ((len > 0) && (domain[len-1] == '.'))
136 			domain[--len] = '\0';
137 	}
138 	/* no domain information */
139 	else
140 		domain = "";
141 
142 	return (domain);
143 }
144