194575Sdes/*-
294575Sdes * Copyright (c) 2002 Networks Associates Technology, Inc.
394575Sdes * All rights reserved.
494575Sdes *
594575Sdes * This software was developed for the FreeBSD Project by ThinkSec AS and
694575Sdes * NAI Labs, the Security Research Division of Network Associates, Inc.
794575Sdes * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
894575Sdes * DARPA CHATS research program.
994575Sdes *
1094575Sdes * Redistribution and use in source and binary forms, with or without
1194575Sdes * modification, are permitted provided that the following conditions
1294575Sdes * are met:
1394575Sdes * 1. Redistributions of source code must retain the above copyright
1494575Sdes *    notice, this list of conditions and the following disclaimer.
1594575Sdes * 2. Redistributions in binary form must reproduce the above copyright
1694575Sdes *    notice, this list of conditions and the following disclaimer in the
1794575Sdes *    documentation and/or other materials provided with the distribution.
1894575Sdes * 3. The name of the author may not be used to endorse or promote
1994575Sdes *    products derived from this software without specific prior written
2094575Sdes *    permission.
2194575Sdes *
2294575Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
2394575Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2494575Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2594575Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2694575Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2794575Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2894575Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2994575Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3094575Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3194575Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3294575Sdes * SUCH DAMAGE.
3394575Sdes *
3494575Sdes * $FreeBSD$
3594575Sdes */
3694575Sdes
3794575Sdes#include <sys/param.h>
3894575Sdes
3994575Sdes#include <err.h>
4094575Sdes#include <errno.h>
4194575Sdes#include <stdlib.h>
4294575Sdes#include <string.h>
4394575Sdes#include <unistd.h>
4494575Sdes
4594575Sdes#include <rpcsvc/ypclnt.h>
4694575Sdes
4794575Sdes#include "ypclnt.h"
4894575Sdes
4994575Sdesint
5094575Sdesypclnt_connect(ypclnt_t *ypclnt)
5194575Sdes{
5294575Sdes	int r;
5394575Sdes
5494575Sdes	/* get default domain name unless specified */
5594575Sdes	if (ypclnt->domain == NULL) {
5694575Sdes		if ((ypclnt->domain = malloc(MAXHOSTNAMELEN)) == NULL) {
5794575Sdes			ypclnt_error(ypclnt, __func__,
5894575Sdes			    "%s", strerror(errno));
5994575Sdes			return (-1);
6094575Sdes		}
6194575Sdes		if (getdomainname(ypclnt->domain, MAXHOSTNAMELEN) != 0) {
6294575Sdes			ypclnt_error(ypclnt, __func__,
6394575Sdes			    "can't get NIS domain name");
6494575Sdes			return (-1);
6594575Sdes		}
6694575Sdes	}
6794575Sdes
6894575Sdes	/* map must be specified */
6994575Sdes	if (ypclnt->map == NULL) {
7094575Sdes		ypclnt_error(ypclnt, __func__,
7194575Sdes		    "caller must specify map name");
7294575Sdes		return (-1);
7394575Sdes	}
7494575Sdes
7594575Sdes	/* get master server for requested map unless specified */
7694575Sdes	if (ypclnt->server == NULL) {
7794575Sdes		r = yp_master(ypclnt->domain, ypclnt->map, &ypclnt->server);
7894575Sdes		if (r != 0) {
7994575Sdes			ypclnt_error(ypclnt, __func__,
8094575Sdes			    "can't get NIS server name: %s", yperr_string(r));
8194575Sdes			return (-1);
8294575Sdes		}
8394575Sdes	}
8494575Sdes
8594575Sdes	ypclnt_error(ypclnt, NULL, NULL);
8694575Sdes	return (0);
8794575Sdes}
88