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