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