Home | History | Annotate | Line # | Download | only in ypcat
ypcat.c revision 1.15
      1  1.15  christos /* $NetBSD: ypcat.c,v 1.15 2011/01/12 18:28:19 christos Exp $	*/
      2   1.6   thorpej 
      3   1.2   deraadt /*
      4   1.3   deraadt  * Copyright (c) 1992, 1993 Theo de Raadt <deraadt (at) fsa.ca>
      5   1.2   deraadt  * All rights reserved.
      6   1.2   deraadt  *
      7   1.2   deraadt  * Redistribution and use in source and binary forms, with or without
      8   1.2   deraadt  * modification, are permitted provided that the following conditions
      9   1.2   deraadt  * are met:
     10   1.2   deraadt  * 1. Redistributions of source code must retain the above copyright
     11   1.2   deraadt  *    notice, this list of conditions and the following disclaimer.
     12   1.2   deraadt  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.2   deraadt  *    notice, this list of conditions and the following disclaimer in the
     14   1.2   deraadt  *    documentation and/or other materials provided with the distribution.
     15   1.2   deraadt  *
     16   1.2   deraadt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     17   1.2   deraadt  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18   1.2   deraadt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19   1.2   deraadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     20   1.2   deraadt  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21   1.2   deraadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22   1.2   deraadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23   1.2   deraadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24   1.2   deraadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25   1.2   deraadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26   1.2   deraadt  * SUCH DAMAGE.
     27   1.2   deraadt  */
     28   1.2   deraadt 
     29   1.7   thorpej #include <sys/cdefs.h>
     30   1.7   thorpej #ifndef lint
     31  1.15  christos __RCSID("$NetBSD: ypcat.c,v 1.15 2011/01/12 18:28:19 christos Exp $");
     32   1.2   deraadt #endif
     33   1.2   deraadt 
     34   1.1   deraadt #include <sys/param.h>
     35   1.1   deraadt #include <sys/types.h>
     36   1.1   deraadt #include <sys/socket.h>
     37  1.13  christos #include <sys/errno.h>
     38   1.7   thorpej #include <ctype.h>
     39   1.7   thorpej #include <err.h>
     40   1.1   deraadt #include <stdio.h>
     41   1.9      matt #include <stdlib.h>
     42   1.9      matt #include <string.h>
     43   1.7   thorpej #include <unistd.h>
     44   1.1   deraadt 
     45   1.1   deraadt #include <rpc/rpc.h>
     46   1.1   deraadt #include <rpc/xdr.h>
     47   1.1   deraadt #include <rpcsvc/yp_prot.h>
     48   1.1   deraadt #include <rpcsvc/ypclnt.h>
     49   1.1   deraadt 
     50  1.13  christos #include "ypalias_init.h"
     51  1.13  christos 
     52  1.13  christos static int	printit(int, char *, int, char *, int, char *);
     53  1.13  christos static void	usage(void) __attribute__((__noreturn__));
     54   1.7   thorpej 
     55  1.15  christos static int	compressspace;
     56  1.15  christos 
     57   1.1   deraadt 
     58   1.1   deraadt int
     59  1.13  christos main(int argc, char *argv[])
     60   1.1   deraadt {
     61   1.1   deraadt 	char *domainname;
     62   1.1   deraadt 	struct ypall_callback ypcb;
     63  1.12     lukem 	const char *inmap;
     64   1.1   deraadt 	int notrans;
     65  1.12     lukem 	int c, r;
     66  1.12     lukem 	size_t i;
     67  1.13  christos 	const struct ypalias *ypaliases;
     68  1.13  christos 	int key;
     69   1.1   deraadt 
     70  1.13  christos 	setprogname(*argv);
     71   1.5       jtc 	domainname = NULL;
     72   1.1   deraadt 	notrans = key = 0;
     73  1.13  christos 	ypaliases = ypalias_init();
     74  1.15  christos 	while((c = getopt(argc, argv, "d:kstx")) != -1) {
     75   1.7   thorpej 		switch (c) {
     76  1.15  christos 		case 'd':
     77  1.15  christos 			domainname = optarg;
     78  1.15  christos 			break;
     79  1.15  christos 
     80  1.15  christos 		case 'k':
     81  1.15  christos 			key++;
     82  1.15  christos 			break;
     83  1.15  christos 
     84  1.15  christos 		case 's':
     85  1.15  christos 			compressspace++;
     86  1.15  christos 			break;
     87  1.15  christos 
     88   1.1   deraadt 		case 'x':
     89  1.13  christos 			for (i = 0; ypaliases[i].alias; i++)
     90   1.1   deraadt 				printf("Use \"%s\" for \"%s\"\n",
     91  1.13  christos 				    ypaliases[i].alias,
     92  1.13  christos 				    ypaliases[i].name);
     93  1.13  christos 			return 0;
     94   1.7   thorpej 
     95   1.1   deraadt 		case 't':
     96   1.1   deraadt 			notrans++;
     97   1.1   deraadt 			break;
     98   1.7   thorpej 
     99   1.1   deraadt 		default:
    100   1.1   deraadt 			usage();
    101   1.1   deraadt 		}
    102   1.7   thorpej 	}
    103   1.7   thorpej 
    104   1.7   thorpej 	argc -= optind;
    105   1.7   thorpej 	argv += optind;
    106   1.1   deraadt 
    107   1.7   thorpej 	if (argc != 1)
    108   1.1   deraadt 		usage();
    109   1.5       jtc 
    110   1.7   thorpej 	if (domainname == NULL)
    111   1.5       jtc 		yp_get_default_domain(&domainname);
    112   1.1   deraadt 
    113   1.7   thorpej 	inmap = argv[0];
    114   1.7   thorpej 	if (notrans == 0) {
    115  1.13  christos 		for (i = 0; ypaliases[i].alias; i++)
    116   1.7   thorpej 			if (strcmp(inmap, ypaliases[i].alias) == 0)
    117   1.4       jtc 				inmap = ypaliases[i].name;
    118   1.4       jtc 	}
    119   1.7   thorpej 
    120   1.1   deraadt 	ypcb.foreach = printit;
    121  1.13  christos 	ypcb.data = key ? (void *)&key : NULL;
    122   1.1   deraadt 
    123   1.1   deraadt 	r = yp_all(domainname, inmap, &ypcb);
    124   1.7   thorpej 	switch (r) {
    125   1.1   deraadt 	case 0:
    126   1.1   deraadt 		break;
    127   1.7   thorpej 
    128   1.1   deraadt 	case YPERR_YPBIND:
    129   1.7   thorpej 		errx(1, "not running ypbind");
    130   1.7   thorpej 
    131   1.1   deraadt 	default:
    132   1.7   thorpej 		errx(1, "no such map %s.  Reason: %s", inmap, yperr_string(r));
    133   1.1   deraadt 	}
    134  1.13  christos 	return 0;
    135   1.7   thorpej }
    136   1.7   thorpej 
    137  1.13  christos static int
    138  1.13  christos printit(int instatus, char *inkey, int inkeylen, char *inval,
    139  1.13  christos     int invallen, char *indata)
    140   1.7   thorpej {
    141   1.7   thorpej 
    142   1.7   thorpej 	if (instatus != YP_TRUE)
    143   1.7   thorpej 		return instatus;
    144  1.13  christos 	if (indata)
    145  1.13  christos 		(void)printf("%*.*s", inkeylen, inkeylen, inkey);
    146  1.15  christos 	if (invallen) {
    147  1.15  christos 		if (indata)
    148  1.15  christos 			(void)putc(' ', stdout);
    149  1.15  christos 		if (compressspace) {
    150  1.15  christos 			int i;
    151  1.15  christos 			int hadspace = 0;
    152  1.15  christos 
    153  1.15  christos 			for (i = 0; i < invallen; i++) {
    154  1.15  christos 				if (isspace((unsigned char)inval[i])) {
    155  1.15  christos 					if (hadspace)
    156  1.15  christos 						continue;
    157  1.15  christos 					hadspace = 1;
    158  1.15  christos 					(void)putc(' ', stdout);
    159  1.15  christos 				} else {
    160  1.15  christos 					hadspace = 0;
    161  1.15  christos 					(void)putc(inval[i], stdout);
    162  1.15  christos 				}
    163  1.15  christos 			}
    164  1.15  christos 		} else
    165  1.15  christos 			(void)printf("%*.*s", invallen, invallen, inval);
    166  1.15  christos 	}
    167  1.15  christos 	(void)putc('\n', stdout);
    168   1.7   thorpej 	return 0;
    169   1.7   thorpej }
    170   1.7   thorpej 
    171  1.13  christos static void
    172  1.13  christos usage(void)
    173   1.7   thorpej {
    174   1.7   thorpej 
    175  1.14       wiz 	(void)fprintf(stderr, "Usage: %s [-kt] [-d domainname] mapname\n",
    176  1.10       cgd 	    getprogname());
    177  1.13  christos 	(void)fprintf(stderr, "       %s -x\n", getprogname());
    178   1.7   thorpej 	exit(1);
    179   1.1   deraadt }
    180