xref: /titanic_51/usr/src/cmd/ndmpd/ndmp/ndmpd_prop.c (revision 07d06da50d310a325b457d6330165aebab1e0064)
1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * BSD 3 Clause License
8  *
9  * Copyright (c) 2007, The Storage Networking Industry Association.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 	- Redistributions of source code must retain the above copyright
15  *	  notice, this list of conditions and the following disclaimer.
16  *
17  * 	- Redistributions in binary form must reproduce the above copyright
18  *	  notice, this list of conditions and the following disclaimer in
19  *	  the documentation and/or other materials provided with the
20  *	  distribution.
21  *
22  *	- Neither the name of The Storage Networking Industry Association (SNIA)
23  *	  nor the names of its contributors may be used to endorse or promote
24  *	  products derived from this software without specific prior written
25  *	  permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * NDMP configuration management
42  */
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <synch.h>
46 #include <syslog.h>
47 #include <strings.h>
48 #include <ndmpd_prop.h>
49 #include <libndmp.h>
50 #include "ndmpd.h"
51 
52 typedef struct ndmpd_cfg_param {
53 	char		*sc_name;
54 	char		*sc_defval;
55 	char		*sc_value;
56 	uint32_t	sc_flags;
57 } ndmpd_cfg_param_t;
58 
59 
60 static int ndmpd_config_update(ndmpd_cfg_param_t *cfg, char *value);
61 
62 /*
63  * IMPORTANT: any changes to the order of this table's entries
64  * need to be reflected in the enum ndmpd_cfg_id_t.
65  */
66 ndmpd_cfg_param_t ndmpd_cfg_table[] =
67 {
68 	{"dar-support",			"",	0, NDMP_CF_NOTINIT},
69 	{"mover-nic",			"",	0, NDMP_CF_NOTINIT},
70 	{"dump-pathnode",		"",	0, NDMP_CF_NOTINIT},
71 	{"tar-pathnode",		"",	0, NDMP_CF_NOTINIT},
72 	{"fh-inode",			"",	0, NDMP_CF_NOTINIT},
73 	{"ignore-ctime",		"",	0, NDMP_CF_NOTINIT},
74 	{"include-lmtime",		"",	0, NDMP_CF_NOTINIT},
75 	{"token-maxseq",		"",	0, NDMP_CF_NOTINIT},
76 	{"version",			"",	0, NDMP_CF_NOTINIT},
77 	{"restore-fullpath",		"",	0, NDMP_CF_NOTINIT},
78 	{"debug-path",			"",	0, NDMP_CF_NOTINIT},
79 	{"plugin-path",			"",	0, NDMP_CF_NOTINIT},
80 	{"socket-css",			"",	0, NDMP_CF_NOTINIT},
81 	{"socket-crs",			"",	0, NDMP_CF_NOTINIT},
82 	{"mover-recordsize",		"",	0, NDMP_CF_NOTINIT},
83 	{"restore-wildcard-enable",	"",	0, NDMP_CF_NOTINIT},
84 	{"cram-md5-username",		"",	0, NDMP_CF_NOTINIT},
85 	{"cram-md5-password",		"",	0, NDMP_CF_NOTINIT},
86 	{"cleartext-username",		"",	0, NDMP_CF_NOTINIT},
87 	{"cleartext-password",		"",	0, NDMP_CF_NOTINIT},
88 	{"tcp-port",			"",	0, NDMP_CF_NOTINIT},
89 	{"backup-quarantine",		"",	0, NDMP_CF_NOTINIT},
90 	{"restore-quarantine",		"",	0, NDMP_CF_NOTINIT},
91 	{"overwrite-quarantine",	"",	0, NDMP_CF_NOTINIT},
92 };
93 
94 /*
95  * Loads all the NDMP configuration parameters and sets up the
96  * config table.
97  */
98 int
99 ndmpd_load_prop(void)
100 {
101 	ndmpd_cfg_id_t id;
102 	ndmpd_cfg_param_t *cfg;
103 	char *value;
104 
105 	for (id = 0; id < NDMP_MAXALL; id++) {
106 		cfg = &ndmpd_cfg_table[id];
107 		if ((ndmp_get_prop(cfg->sc_name, &value)) == -1) {
108 			syslog(LOG_DEBUG, "%s %s",
109 			    cfg->sc_name, ndmp_strerror(ndmp_errno));
110 			continue;
111 		}
112 		/*
113 		 * enval == 0 could mean two things, either the
114 		 * config param is not defined, or it has been
115 		 * removed. If the variable has already been defined
116 		 * and now enval is 0, it should be removed, otherwise
117 		 * we don't need to do anything in this case.
118 		 */
119 		if ((cfg->sc_flags & NDMP_CF_DEFINED) || value) {
120 			if (ndmpd_config_update(cfg, value)) {
121 				free(value);
122 				return (-1);
123 			}
124 		}
125 		free(value);
126 	}
127 	return (0);
128 }
129 
130 /*
131  * ndmpd_config_update
132  *
133  * Updates the specified config param with the given value.
134  * This function is called both on (re)load and set.
135  */
136 static int
137 ndmpd_config_update(ndmpd_cfg_param_t *cfg, char *value)
138 {
139 	char *curval;
140 	int rc = 0;
141 	int len;
142 
143 	if (value) {
144 		len = strlen(value);
145 		if (cfg->sc_value) {
146 			curval = realloc(cfg->sc_value, (len + 1));
147 		} else {
148 			curval = ndmp_malloc(len + 1);
149 		}
150 
151 		if (curval) {
152 			cfg->sc_value = curval;
153 			(void) strcpy(cfg->sc_value, value);
154 			cfg->sc_flags |= NDMP_CF_DEFINED;
155 		} else {
156 			syslog(LOG_ERR, "Out of memory.");
157 			rc = -1;
158 		}
159 	} else if (cfg->sc_value) {
160 		free(cfg->sc_value);
161 		cfg->sc_value = 0;
162 		cfg->sc_flags &= ~NDMP_CF_DEFINED;
163 	}
164 
165 	return (rc);
166 }
167 
168 /*
169  * Returns value of the specified config param.
170  * The return value is a string pointer to the locally
171  * allocated memory if the config param is defined
172  * otherwise it would be NULL.
173  */
174 char *
175 ndmpd_get_prop(ndmpd_cfg_id_t id)
176 {
177 	char *env_val;
178 
179 	if (id < NDMP_MAXALL) {
180 		env_val = ndmpd_cfg_table[id].sc_value;
181 		return (env_val);
182 	}
183 
184 	return (0);
185 }
186 
187 /*
188  * Similar to ndmpd_get_prop except it will return dflt value
189  * if env is not set.
190  */
191 char *
192 ndmpd_get_prop_default(ndmpd_cfg_id_t id, char *dflt)
193 {
194 	char *env;
195 
196 	env = ndmpd_get_prop(id);
197 
198 	if (env && *env != 0) {
199 		return (env);
200 	} else {
201 		return (dflt);
202 	}
203 }
204 
205 /*
206  * Returns the value of a yes/no config param.
207  * Returns 1 is config is set to "yes", otherwise 0.
208  */
209 int
210 ndmpd_get_prop_yorn(ndmpd_cfg_id_t id)
211 {
212 	char *val;
213 
214 	val = ndmpd_get_prop(id);
215 	if (val) {
216 		if (strcasecmp(val, "yes") == 0)
217 			return (1);
218 	}
219 
220 	return (0);
221 }
222