xref: /illumos-gate/usr/src/cmd/cmd-inet/common/compat.c (revision 5328fc53d11d7151861fa272e4fb0248b8f0e145)
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 /*
23  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
27  */
28 
29 #include <deflt.h>
30 #include <string.h>
31 #include <stddef.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <compat.h>
35 
36 #define	DEFAULT_IP_LINE		DEFAULT_IP"="
37 
38 /*
39  * Code to handle /etc/default/ file for IPv4 command output compatibility
40  *
41  * Note: Handles BOTH the same as IP_VERSION6.
42  *
43  * Returns 1 if IP_VERSION4; 0 for other versions.
44  * Returns -1 if the value of DEFAULT_IP found in /etc/default/inet_type is
45  * invalid.
46  */
47 int
48 get_compat_flag(char **value)
49 {
50 	if (defopen(INET_DEFAULT_FILE) == 0) {
51 		char	*cp;
52 		int	flags;
53 
54 		/*
55 		 * ignore case
56 		 */
57 		flags = defcntl(DC_GETFLAGS, 0);
58 		TURNOFF(flags, DC_CASE);
59 		(void) defcntl(DC_SETFLAGS, flags);
60 
61 		if ((cp = defread(DEFAULT_IP_LINE)) != NULL)
62 			*value = strdup(cp);
63 
64 		/* close */
65 		(void) defopen((char *)NULL);
66 
67 		if (*value != NULL) {
68 			if (strcasecmp(*value, "IP_VERSION4") == 0) {
69 				return (DEFAULT_PROT_V4_ONLY);
70 			} else if (strcasecmp(*value, "BOTH") == 0 ||
71 			    strcasecmp(*value, "IP_VERSION6") == 0) {
72 				return (DEFAULT_PROT_BOTH);
73 			} else {
74 				return (DEFAULT_PROT_BAD_VALUE);
75 			}
76 		}
77 	}
78 	/* No value set */
79 	return (DEFAULT_PROT_BOTH);
80 }
81