2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include "libmail.h"
2N/A#include <sys/types.h>
2N/A#include <ctype.h>
2N/A#ifdef SVR4
2N/A#include <sys/utsname.h>
2N/A#include <sys/systeminfo.h>
2N/A#endif
2N/A
2N/A#define NMLN 512
2N/A#ifdef SVR4
2N/A#if SYS_NMLN > NMLN
2N/A#undef NMLN
2N/A#define NMLN SYS_NMLN
2N/A#endif
2N/A#endif
2N/A
2N/Astatic char *look4domain(char *, char *, int);
2N/Astatic char *readdomain(char *, int);
2N/A
2N/A/*
2N/A * NAME
2N/A * maildomain() - retrieve the domain name
2N/A *
2N/A * SYNOPSIS
2N/A * char *maildomain()
2N/A *
2N/A * DESCRIPTION
2N/A * Retrieve the domain name from xgetenv("DOMAIN").
2N/A * If that is not set, look in /etc/resolv.conf, /etc/inet/named.boot
2N/A * and /etc/named.boot for "^domain[ ]+<domain>".
2N/A * If that is not set, use sysinfo(SI_SRPC_DOMAIN) from
2N/A * -lnsl. Otherwise, return an empty string.
2N/A */
2N/A
2N/A/* read a file for the domain */
2N/Astatic char *look4domain(char *file, char *buf, int size)
2N/A{
2N/A char *ret = 0;
2N/A FILE *fp = fopen(file, "r");
2N/A
2N/A if (!fp)
2N/A return (0);
2N/A
2N/A while (fgets(buf, size, fp))
2N/A if (strncmp(buf, "domain", 6) == 0)
2N/A if (isspace(buf[6])) {
2N/A char *x = skipspace(buf + 6);
2N/A if (isgraph(*x)) {
2N/A trimnl(x);
2N/A strmove(buf, x);
2N/A ret = buf;
2N/A break;
2N/A }
2N/A }
2N/A
2N/A (void) fclose(fp);
2N/A return (ret);
2N/A}
2N/A
2N/A/* read the domain from the xenvironment or one of the files */
2N/Astatic char *readdomain(char *buf, int size)
2N/A{
2N/A char *ret;
2N/A
2N/A if ((ret = xgetenv("DOMAIN")) != 0) {
2N/A (void) strncpy(buf, ret, size);
2N/A return (buf);
2N/A }
2N/A
2N/A if (((ret = look4domain("/etc/resolv.conf", buf, size)) != 0) ||
2N/A ((ret = look4domain("/etc/inet/named.boot", buf, size)) != 0) ||
2N/A ((ret = look4domain("/etc/named.boot", buf, size)) != 0))
2N/A return (ret);
2N/A
2N/A#ifdef SVR4
2N/A if (sysinfo(SI_SRPC_DOMAIN, buf, size) >= 0)
2N/A return (buf);
2N/A#endif
2N/A
2N/A return (0);
2N/A}
2N/A
2N/Achar *
2N/Amaildomain(void)
2N/A{
2N/A static char *domain = 0;
2N/A static char dombuf[NMLN+1] = ".";
2N/A
2N/A /* if we've already been here, return the info */
2N/A if (domain != 0)
2N/A return (domain);
2N/A
2N/A domain = readdomain(dombuf+1, NMLN);
2N/A
2N/A /* Make certain that the domain begins with a single dot */
2N/A /* and does not have one at the end. */
2N/A if (domain) {
2N/A size_t len;
2N/A domain = dombuf;
2N/A while (*domain && *domain == '.')
2N/A domain++;
2N/A domain--;
2N/A len = strlen(domain);
2N/A while ((len > 0) && (domain[len-1] == '.'))
2N/A domain[--len] = '\0';
2N/A }
2N/A /* no domain information */
2N/A else
2N/A domain = "";
2N/A
2N/A return (domain);
2N/A}