xref: /illumos-gate/usr/src/lib/libmail/common/getdomain.c (revision 201ceb75ab95f9bf1f42ea1dc9ab363b43ba47cf)
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 #include "libmail.h"
31 #include <sys/types.h>
32 #include <ctype.h>
33 #ifdef SVR4
34 #include <sys/utsname.h>
35 #include <sys/systeminfo.h>
36 #endif
37 
38 #define	NMLN 512
39 #ifdef SVR4
40 #if SYS_NMLN > NMLN
41 #undef NMLN
42 #define	NMLN SYS_NMLN
43 #endif
44 #endif
45 
46 static char *look4domain(char *, char *, int);
47 static char *readdomain(char *, int);
48 
49 /*
50  *  NAME
51  *	maildomain() - retrieve the domain name
52  *
53  *  SYNOPSIS
54  *	char *maildomain()
55  *
56  *  DESCRIPTION
57  *	Retrieve the domain name from xgetenv("DOMAIN").
58  *	If that is not set, look in /etc/resolv.conf, /etc/inet/named.boot
59  *	and /etc/named.boot for "^domain[ ]+<domain>".
60  *	If that is not set, use sysinfo(SI_SRPC_DOMAIN) from
61  *	-lnsl. Otherwise, return an empty string.
62  */
63 
64 /* read a file for the domain */
65 static char *look4domain(char *file, char *buf, int size)
66 {
67 	char *ret = 0;
68 	FILE *fp = fopen(file, "r");
69 
70 	if (!fp)
71 		return (0);
72 
73 	while (fgets(buf, size, fp))
74 		if (strncmp(buf, "domain", 6) == 0)
75 	if (isspace(buf[6])) {
76 		char *x = skipspace(buf + 6);
77 		if (isgraph(*x)) {
78 			trimnl(x);
79 			strmove(buf, x);
80 			ret = buf;
81 			break;
82 		}
83 	}
84 
85 	(void) fclose(fp);
86 	return (ret);
87 }
88 
89 /* read the domain from the xenvironment or one of the files */
90 static char *readdomain(char *buf, int size)
91 {
92 	char *ret;
93 
94 	if ((ret = xgetenv("DOMAIN")) != 0) {
95 		(void) strncpy(buf, ret, size);
96 		return (buf);
97 	}
98 
99 	if (((ret = look4domain("/etc/resolv.conf", buf, size)) != 0) ||
100 	    ((ret = look4domain("/etc/inet/named.boot", buf, size)) != 0) ||
101 	    ((ret = look4domain("/etc/named.boot", buf, size)) != 0))
102 		return (ret);
103 
104 #ifdef SVR4
105 	if (sysinfo(SI_SRPC_DOMAIN, buf, size) >= 0)
106 		return (buf);
107 #endif
108 
109 	return (0);
110 }
111 
112 char *
113 maildomain(void)
114 {
115 	static char *domain = 0;
116 	static char dombuf[NMLN+1] = ".";
117 
118 	/* if we've already been here, return the info */
119 	if (domain != 0)
120 		return (domain);
121 
122 	domain = readdomain(dombuf+1, NMLN);
123 
124 	/* Make certain that the domain begins with a single dot */
125 	/* and does not have one at the end. */
126 	if (domain) {
127 		size_t len;
128 		domain = dombuf;
129 		while (*domain && *domain == '.')
130 			domain++;
131 		domain--;
132 		len = strlen(domain);
133 		while ((len > 0) && (domain[len-1] == '.'))
134 			domain[--len] = '\0';
135 	}
136 	/* no domain information */
137 	else
138 		domain = "";
139 
140 	return (domain);
141 }
142