xref: /illumos-gate/usr/src/cmd/ypcmd/ypalias.c (revision a506a34c)
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  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /*	  All Rights Reserved   */
28 
29 /*
30  * Portions of this source code were derived from Berkeley
31  * under license from the Regents of the University of
32  * California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <stdio.h>
38 #include <string.h>
39 #include <limits.h>
40 #include <sys/types.h>
41 #include <sys/statvfs.h>
42 #include "ypsym.h"
43 
44 #ifdef SYSVCONFIG
45 extern void sysvconfig();
46 #endif
47 
48 extern int yp_getalias();
49 
50 /*
51  *	Given a domain name, return its system v alias.
52  *	If there is no alias name in the alias file,
53  *	create one. Rule of creation is to take the 1st
54  *	NAME_MAX-4 characters and concatenate the last 4 characters.
55  *	If the alias in the file is too long, trim off the end.
56  */
57 
58 static void
59 mkdomain_alias(name, result)
60 char *name, *result;
61 {
62 	int retval;
63 	char tmpbuf[MAXNAMLEN] = {NULL};
64 
65 	retval = yp_getalias(name, result, NAME_MAX);
66 	if (retval == -1) {
67 		if ((int)strlen(name) > NAME_MAX) {
68 			strncpy(result, name, NAME_MAX-4);
69 			strncpy(&result[NAME_MAX-4],
70 			    &name[strlen(name)-4], 4);
71 			result[NAME_MAX] = '\0';
72 		} else
73 			strcpy(result, name);
74 	} else if ((retval) && (int)strlen(result) > NAME_MAX) {
75 		strncpy(tmpbuf, result, NAME_MAX);
76 		strcpy(result, tmpbuf);
77 	}
78 }
79 
80 /*
81  *	Given a map name, return its system v alias .
82  *	If there is no alias name in the alias file,
83  *	create one. Rule of creation is to take the 1st
84  *	MAXALIASLEN-4 characters and concatenate the last 4 characters.
85  *	If the alias in the file is too long, trim off the end.
86  */
87 static void
88 mkmap_alias(name, result)
89 char *name, *result;
90 {
91 	int retval;
92 	char tmpbuf[MAXNAMLEN] = {NULL};
93 
94 	retval = yp_getalias(name, result, MAXALIASLEN);
95 
96 	if (retval == -1) {
97 		if ((int)strlen(name) > MAXALIASLEN) {
98 			(void) strncpy(result, name, MAXALIASLEN-4);
99 			(void) strncpy(&result[MAXALIASLEN-4],
100 			    &name[strlen(name)-4], 4);
101 			result[MAXALIASLEN] = '\0';
102 		} else
103 			(void) strcpy(result, name);
104 	} else if ((retval) && (int)strlen(result) > MAXALIASLEN) {
105 		(void) strncpy(tmpbuf, result, MAXALIASLEN);
106 		(void) strcpy(result, tmpbuf);
107 	}
108 }
109 
110 #ifdef MAIN
111 
112 /*
113  * executed only for the command ypalias
114  * and not when ypbind or ypserv make use
115  * of this file.
116  */
117 
118 static char usage[] =
119 "Usage:\n\
120 	ypalias -d domainname\n\
121 	ypalias mapname\n";
122 
123 int
124 main(argc, argv)
125 char **argv;
126 {
127 	char result[MAXNAMLEN] = {NULL};
128 
129 #ifdef SYSVCONFIG
130 	sysvconfig();
131 #endif
132 	if (argc <= 1)
133 		goto err;
134 	if (strcmp(argv[1], "-d") == 0)
135 		if (argc == 3) mkdomain_alias(argv[2], (char *)&result);
136 		else goto err;
137 	else if (argc == 2)
138 		mkmap_alias(argv[1], (char *)&result);
139 	else
140 		goto err;
141 	(void) printf("%s", result);
142 	return (0);
143 err:
144 	(void) fprintf(stderr, usage);
145 	return (1);
146 }
147 #endif /* MAIN */
148