1*8bae5d40Schristos /* $NetBSD: sun_map.c,v 1.1.1.3 2015/01/17 16:34:15 christos Exp $ */
2a53f50b9Schristos
3a53f50b9Schristos /*
4*8bae5d40Schristos * Copyright (c) 1997-2014 Erez Zadok
5a53f50b9Schristos * Copyright (c) 2005 Daniel P. Ottavio
6a53f50b9Schristos * Copyright (c) 1990 Jan-Simon Pendry
7a53f50b9Schristos * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
8a53f50b9Schristos * Copyright (c) 1990 The Regents of the University of California.
9a53f50b9Schristos * All rights reserved.
10a53f50b9Schristos *
11a53f50b9Schristos * This code is derived from software contributed to Berkeley by
12a53f50b9Schristos * Jan-Simon Pendry at Imperial College, London.
13a53f50b9Schristos *
14a53f50b9Schristos * Redistribution and use in source and binary forms, with or without
15a53f50b9Schristos * modification, are permitted provided that the following conditions
16a53f50b9Schristos * are met:
17a53f50b9Schristos * 1. Redistributions of source code must retain the above copyright
18a53f50b9Schristos * notice, this list of conditions and the following disclaimer.
19a53f50b9Schristos * 2. Redistributions in binary form must reproduce the above copyright
20a53f50b9Schristos * notice, this list of conditions and the following disclaimer in the
21a53f50b9Schristos * documentation and/or other materials provided with the distribution.
22*8bae5d40Schristos * 3. Neither the name of the University nor the names of its contributors
23a53f50b9Schristos * may be used to endorse or promote products derived from this software
24a53f50b9Schristos * without specific prior written permission.
25a53f50b9Schristos *
26a53f50b9Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27a53f50b9Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28a53f50b9Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29a53f50b9Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30a53f50b9Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31a53f50b9Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32a53f50b9Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33a53f50b9Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34a53f50b9Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35a53f50b9Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36a53f50b9Schristos * SUCH DAMAGE.
37a53f50b9Schristos *
38a53f50b9Schristos *
39a53f50b9Schristos * File: am-utils/amd/sun_map.c
40a53f50b9Schristos *
41a53f50b9Schristos */
42a53f50b9Schristos
43a53f50b9Schristos #ifdef HAVE_CONFIG_H
44a53f50b9Schristos # include <config.h>
45a53f50b9Schristos #endif /* HAVE_CONFIG_H */
46a53f50b9Schristos #include <am_defs.h>
47a53f50b9Schristos #include <amd.h>
48a53f50b9Schristos #include <sun_map.h>
49a53f50b9Schristos
50a53f50b9Schristos
51a53f50b9Schristos
52a53f50b9Schristos /*
53a53f50b9Schristos * Add a data pointer to the end of the list.
54a53f50b9Schristos */
55a53f50b9Schristos void
sun_list_add(struct sun_list * list,qelem * item)56a53f50b9Schristos sun_list_add(struct sun_list *list, qelem *item)
57a53f50b9Schristos {
58a53f50b9Schristos if (list->last == NULL) {
59a53f50b9Schristos list->last = item;
60a53f50b9Schristos list->first = item;
61a53f50b9Schristos item->q_back = NULL;
62a53f50b9Schristos }
63a53f50b9Schristos else {
64a53f50b9Schristos list->last->q_forw = item;
65a53f50b9Schristos item->q_back = list->last;
66a53f50b9Schristos list->last = item;
67a53f50b9Schristos }
68a53f50b9Schristos
69a53f50b9Schristos item->q_forw = NULL;
70a53f50b9Schristos }
71a53f50b9Schristos
72a53f50b9Schristos
73a53f50b9Schristos /*
74a53f50b9Schristos * Sun2Amd conversion routines
75a53f50b9Schristos */
76a53f50b9Schristos
77a53f50b9Schristos /*
78a53f50b9Schristos * AMD entry keywords
79a53f50b9Schristos */
80a53f50b9Schristos #define AMD_OPTS_KW "addopts:=" /* add entry options */
81a53f50b9Schristos #define AMD_RHOST_KW "rhost:=" /* remote host */
82a53f50b9Schristos #define AMD_RFS_KW "rfs:=" /* remote file system */
83a53f50b9Schristos #define AMD_FS_KW "fs:=" /* local file system */
84a53f50b9Schristos #define AMD_DEV_KW "dev:=" /* device */
85a53f50b9Schristos #define AMD_TYPE_NFS_KW "type:=nfs;" /* fs type nfs */
86a53f50b9Schristos #define AMD_TYPE_AUTO_KW "type:=auto;" /* fs type auto */
87a53f50b9Schristos #define AMD_TYPE_CDFS_KW "type:=cdfs;" /* fs type cd */
88a53f50b9Schristos #define AMD_MAP_FS_KW "fs:=${map};" /* set the mount map as current map */
89a53f50b9Schristos #define AMD_MAP_PREF_KW "pref:=${key}/" /* set the mount map as current map */
90a53f50b9Schristos
91a53f50b9Schristos /*
92a53f50b9Schristos * A set of string Sun fstypes.
93a53f50b9Schristos */
94a53f50b9Schristos #define SUN_NFS_TYPE "nfs"
95a53f50b9Schristos #define SUN_HSFS_TYPE "hsfs" /* CD fs */
96a53f50b9Schristos #define SUN_AUTOFS_TYPE "autofs"
97a53f50b9Schristos #define SUN_CACHEFS_TYPE "cachefs"
98a53f50b9Schristos
99a53f50b9Schristos #define SUN_KEY_SUB "&" /* Sun key substitution */
100a53f50b9Schristos
101a53f50b9Schristos /* a set a Sun variable substitutions for map entries */
102a53f50b9Schristos #define SUN_ARCH "$ARCH" /* host architecture */
103a53f50b9Schristos #define SUN_CPU "$CPU" /* processor type */
104a53f50b9Schristos #define SUN_HOST "$HOST" /* host name */
105a53f50b9Schristos #define SUN_OSNAME "$OSNAME" /* OS name */
106a53f50b9Schristos #define SUN_OSREL "$OSREL" /* OS release */
107a53f50b9Schristos #define SUN_OSVERS "$OSVERS" /* OS version */
108a53f50b9Schristos #define SUN_NATISA "$NATISA" /* native instruction set */
109a53f50b9Schristos
110a53f50b9Schristos /* a set of Amd variable substitutions */
111a53f50b9Schristos #define AMD_ARCH "${arch}" /* host architecture */
112a53f50b9Schristos #define AMD_HOST "${host}" /* host name */
113a53f50b9Schristos #define AMD_OSNAME "${os}" /* OS name */
114a53f50b9Schristos #define AMD_OSVER "${osver}" /* OS version */
115a53f50b9Schristos
116a53f50b9Schristos
117a53f50b9Schristos /*
118a53f50b9Schristos * Return a copy of src that has all occurrences of 'str' replaced
119a53f50b9Schristos * with sub.
120a53f50b9Schristos *
121a53f50b9Schristos * param src - the original string
122a53f50b9Schristos * param str - string that is the replaced with str
123a53f50b9Schristos * param sub - string that replaces an occurrences of 'delim'
124a53f50b9Schristos *
125a53f50b9Schristos * return - new string with str substitutions, NULL on error
126a53f50b9Schristos */
127a53f50b9Schristos static char *
sun_strsub(const char * src,const char * str,const char * sub)128a53f50b9Schristos sun_strsub(const char *src, const char *str, const char *sub)
129a53f50b9Schristos {
130a53f50b9Schristos
131a53f50b9Schristos char *retval = NULL, *str_start, *str_end, *src_end;
132a53f50b9Schristos size_t total_size, first_half, second_half, sub_size;
133a53f50b9Schristos
134a53f50b9Schristos /* assign pointers to the start and end of str */
135a53f50b9Schristos if ((str_start = strstr(src, str)) == NULL) {
136a53f50b9Schristos return retval;
137a53f50b9Schristos }
138a53f50b9Schristos str_end = (strlen(str) - 1) + str_start;
139a53f50b9Schristos
140a53f50b9Schristos /* assign to the end of the src. */
141a53f50b9Schristos src_end = (strlen(src) - 1) + (char*)src;
142a53f50b9Schristos
143a53f50b9Schristos /* size from the beginning of src to the start of str */
144a53f50b9Schristos first_half = (size_t)(str_start - src);
145a53f50b9Schristos
146a53f50b9Schristos /* size from the end of str to the end of src */
147a53f50b9Schristos second_half = (size_t)(src_end - str_end);
148a53f50b9Schristos
149a53f50b9Schristos sub_size = strlen(sub);
150a53f50b9Schristos
151a53f50b9Schristos total_size = (first_half + sub_size + second_half + 1);
152a53f50b9Schristos
153a53f50b9Schristos retval = (char*)xmalloc(total_size);
154a53f50b9Schristos memset(retval, 0, total_size);
155a53f50b9Schristos
156a53f50b9Schristos /*
157a53f50b9Schristos * Put together the string such that the first half is copied
158a53f50b9Schristos * followed the sub and second half.
159a53f50b9Schristos *
160a53f50b9Schristos * We use strncpy instead of xstrlcpy because we are intentionally
161a53f50b9Schristos * causing truncation and we don't want this to cause errors in the
162a53f50b9Schristos * log.
163a53f50b9Schristos */
164a53f50b9Schristos (void)strncpy(retval, src, first_half);
165a53f50b9Schristos (void)strncat(retval, sub, sub_size);
166a53f50b9Schristos (void)strncat(retval, str_end + 1, second_half);
167a53f50b9Schristos
168*8bae5d40Schristos if (strstr(retval, str) != NULL) {
169a53f50b9Schristos /*
170a53f50b9Schristos * If there is another occurrences of str call this function
171a53f50b9Schristos * recursively.
172a53f50b9Schristos */
173a53f50b9Schristos char* tmp;
174a53f50b9Schristos if ((tmp = sun_strsub(retval, str, sub)) != NULL) {
175a53f50b9Schristos XFREE(retval);
176a53f50b9Schristos retval = tmp;
177a53f50b9Schristos }
178a53f50b9Schristos }
179a53f50b9Schristos return retval;
180a53f50b9Schristos }
181a53f50b9Schristos
182a53f50b9Schristos
183a53f50b9Schristos /*
184a53f50b9Schristos * Return a new string that is a copy of str, all occurrences of a Sun
185a53f50b9Schristos * variable substitutions are replaced by there equivalent Amd
186a53f50b9Schristos * substitutions.
187a53f50b9Schristos *
188a53f50b9Schristos * param str - source string
189a53f50b9Schristos *
190a53f50b9Schristos * return - A new string with the expansions, NULL if str does not
191a53f50b9Schristos * exist in src or error.
192a53f50b9Schristos */
193a53f50b9Schristos static char *
sun_expand2amd(const char * str)194a53f50b9Schristos sun_expand2amd(const char *str)
195a53f50b9Schristos {
196a53f50b9Schristos
197a53f50b9Schristos char *retval = NULL, *tmp = NULL, *tmp2 = NULL;
198a53f50b9Schristos const char *pos;
199a53f50b9Schristos
200a53f50b9Schristos /*
201a53f50b9Schristos * Iterator through the string looking for '$' chars. For each '$'
202a53f50b9Schristos * found try to replace it with Sun variable substitutions. If we
203a53f50b9Schristos * find a '$' that is not a substation each of the i.e $blah than
204a53f50b9Schristos * each of the replace attempt will fail and we'll move on to the
205a53f50b9Schristos * next char.
206a53f50b9Schristos */
207*8bae5d40Schristos tmp = xstrdup(str);
208a53f50b9Schristos for (pos = str; *pos != '\0'; pos++) {
209a53f50b9Schristos if (*pos != '$') {
210a53f50b9Schristos continue;
211a53f50b9Schristos }
212a53f50b9Schristos if (tmp2 != NULL) {
213a53f50b9Schristos XFREE(tmp);
214a53f50b9Schristos tmp = tmp2;
215a53f50b9Schristos }
216a53f50b9Schristos
217a53f50b9Schristos /*
218a53f50b9Schristos * If a 'replace' does not return NULL than a variable was
219a53f50b9Schristos * successfully substituted.
220a53f50b9Schristos */
221a53f50b9Schristos
222a53f50b9Schristos /* architecture */
223a53f50b9Schristos if ((tmp2 = sun_strsub(tmp, SUN_ARCH, AMD_ARCH)) != NULL) {
224a53f50b9Schristos continue;
225a53f50b9Schristos }
226a53f50b9Schristos /* cpu - there is not POSIX uname for cpu so just use machine */
227a53f50b9Schristos if ((tmp2 = sun_strsub(tmp, SUN_CPU, AMD_ARCH)) != NULL) {
228a53f50b9Schristos continue;
229a53f50b9Schristos }
230a53f50b9Schristos /* hostname */
231a53f50b9Schristos if ((tmp2 = sun_strsub(tmp, SUN_HOST, AMD_HOST)) != NULL) {
232a53f50b9Schristos continue;
233a53f50b9Schristos }
234a53f50b9Schristos /* os name */
235a53f50b9Schristos if ((tmp2 = sun_strsub(tmp, SUN_OSNAME, AMD_OSNAME)) != NULL) {
236a53f50b9Schristos continue;
237a53f50b9Schristos }
238a53f50b9Schristos /*
239a53f50b9Schristos * os release - Amd doesn't hava a OS release var just usr os
240a53f50b9Schristos * version or now.
241a53f50b9Schristos */
242a53f50b9Schristos if ((tmp2 = sun_strsub(tmp, SUN_OSREL, AMD_OSVER)) != NULL) {
243a53f50b9Schristos continue;
244a53f50b9Schristos }
245a53f50b9Schristos /* os version */
246a53f50b9Schristos if ((tmp2 = sun_strsub(tmp, SUN_OSVERS, AMD_OSVER)) != NULL) {
247a53f50b9Schristos continue;
248a53f50b9Schristos }
249a53f50b9Schristos /* native instruction set - there is no POSIX natisa so just use system */
250a53f50b9Schristos if ((tmp2 = sun_strsub(tmp, SUN_NATISA, AMD_ARCH)) != NULL) {
251a53f50b9Schristos continue;
252a53f50b9Schristos }
253a53f50b9Schristos }
254a53f50b9Schristos if (tmp2 == NULL) {
255a53f50b9Schristos retval = tmp;
256a53f50b9Schristos }
257a53f50b9Schristos else {
258a53f50b9Schristos retval = tmp2;
259a53f50b9Schristos XFREE(tmp);
260a53f50b9Schristos }
261a53f50b9Schristos
262a53f50b9Schristos return retval;
263a53f50b9Schristos }
264a53f50b9Schristos
265a53f50b9Schristos
266a53f50b9Schristos /*
267a53f50b9Schristos * This is a wrapper function for appending Amd entry information to a
268a53f50b9Schristos * buffer. Any Sun variable substitutions will be converted into Amd
269a53f50b9Schristos * equivalents.
270a53f50b9Schristos *
271a53f50b9Schristos * param dest - destination buffer
272a53f50b9Schristos * param deslen - destination buffer length
273a53f50b9Schristos * param key - entry key, this might be needed for key substitutions
274a53f50b9Schristos * param str - string to append
275a53f50b9Schristos */
276a53f50b9Schristos static void
sun_append_str(char * dest,size_t destlen,const char * key,const char * str)277a53f50b9Schristos sun_append_str(char *dest,
278a53f50b9Schristos size_t destlen,
279a53f50b9Schristos const char *key,
280a53f50b9Schristos const char *str)
281a53f50b9Schristos {
282a53f50b9Schristos char *sub = NULL, *sub2 = NULL, *out = NULL;
283a53f50b9Schristos
284a53f50b9Schristos /* By default we are going to just write the original string. */
285a53f50b9Schristos out = (char*)str;
286a53f50b9Schristos
287a53f50b9Schristos /*
288a53f50b9Schristos * Resolve variable substitutions in two steps; 1) replace any key
289a53f50b9Schristos * map substitutions with the entry key 2) expand any variable
290a53f50b9Schristos * substitutions i.e $HOST.
291a53f50b9Schristos *
292a53f50b9Schristos * Try to replace the key substitution '&'. If this function returns
293a53f50b9Schristos * with a new string, one or more key subs. where replaced with the
294a53f50b9Schristos * entry key.
295a53f50b9Schristos */
296a53f50b9Schristos if ((sub = sun_strsub(str, SUN_KEY_SUB, "${key}")) != NULL) {
297a53f50b9Schristos out = sub;
298a53f50b9Schristos /*
299a53f50b9Schristos * Try to convert any variable substitutions. If this function
300a53f50b9Schristos * returns a new string one or more var subs where expanded.
301a53f50b9Schristos */
302a53f50b9Schristos if ((sub2 = sun_expand2amd(sub)) != NULL) {
303a53f50b9Schristos out = sub2;
304a53f50b9Schristos }
305a53f50b9Schristos }
306a53f50b9Schristos /*
307a53f50b9Schristos * Try to convert any variable substitutions. If this function
308a53f50b9Schristos * returns a new string one or more var subs where expanded.
309a53f50b9Schristos */
310*8bae5d40Schristos else if (out != NULL && (sub = sun_expand2amd(out)) != NULL) {
311a53f50b9Schristos out = sub;
312a53f50b9Schristos }
313a53f50b9Schristos
314a53f50b9Schristos if (out != NULL) {
315a53f50b9Schristos xstrlcat(dest, out, destlen);
316a53f50b9Schristos }
317a53f50b9Schristos XFREE(sub);
318a53f50b9Schristos XFREE(sub2);
319a53f50b9Schristos }
320a53f50b9Schristos
321a53f50b9Schristos
322a53f50b9Schristos /*
323a53f50b9Schristos * Convert the list of Sun mount options to Amd mount options. The
324a53f50b9Schristos * result is concatenated to dest.
325a53f50b9Schristos *
326a53f50b9Schristos * param dest - destination buffer
327a53f50b9Schristos * param destlen - destination buffer length
328a53f50b9Schristos * param key - automount key
329a53f50b9Schristos * param opt_list - list of Sun mount options
330a53f50b9Schristos */
331a53f50b9Schristos static void
sun_opts2amd(char * dest,size_t destlen,const char * key,const struct sun_opt * opt_list)332a53f50b9Schristos sun_opts2amd(char *dest,
333a53f50b9Schristos size_t destlen,
334a53f50b9Schristos const char *key,
335a53f50b9Schristos const struct sun_opt *opt_list)
336a53f50b9Schristos {
337a53f50b9Schristos const struct sun_opt *opt;
338a53f50b9Schristos
339a53f50b9Schristos xstrlcat(dest, AMD_OPTS_KW, destlen);
340a53f50b9Schristos
341a53f50b9Schristos /* Iterate through each option and append it to the buffer. */
342a53f50b9Schristos for(opt = opt_list; opt != NULL; opt = NEXT(struct sun_opt, opt)) {
343a53f50b9Schristos sun_append_str(dest, destlen, key, opt->str);
344a53f50b9Schristos /* If there are more options add some commas. */
345a53f50b9Schristos if (NEXT(struct sun_opt, opt) != NULL) {
346a53f50b9Schristos xstrlcat(dest, ",", destlen);
347a53f50b9Schristos }
348a53f50b9Schristos }
349a53f50b9Schristos xstrlcat(dest, ";", destlen);
350a53f50b9Schristos }
351a53f50b9Schristos
352a53f50b9Schristos
353a53f50b9Schristos /*
354a53f50b9Schristos * Convert the list of Sun mount locations to a list of Amd mount
355a53f50b9Schristos * locations. The result is concatenated to dest.
356a53f50b9Schristos *
357a53f50b9Schristos * param dest - destination buffer
358a53f50b9Schristos * param destlen - destination buffer length
359a53f50b9Schristos * param key - automount key
360a53f50b9Schristos * param local_list - list of Sun mount locations
361a53f50b9Schristos */
362a53f50b9Schristos static void
sun_locations2amd(char * dest,size_t destlen,const char * key,const struct sun_location * local_list)363a53f50b9Schristos sun_locations2amd(char *dest,
364a53f50b9Schristos size_t destlen,
365a53f50b9Schristos const char *key,
366a53f50b9Schristos const struct sun_location *local_list)
367a53f50b9Schristos {
368a53f50b9Schristos const struct sun_location *local;
369a53f50b9Schristos const struct sun_host *host;
370a53f50b9Schristos
371a53f50b9Schristos for (local = local_list;
372a53f50b9Schristos local != NULL;
373a53f50b9Schristos local = NEXT(struct sun_location,local)) {
374a53f50b9Schristos /*
375a53f50b9Schristos * Check to see if the list of hosts is empty. Some mount types
376a53f50b9Schristos * i.e cd-rom may have mount location with no host.
377a53f50b9Schristos */
378a53f50b9Schristos if (local->host_list != NULL) {
379a53f50b9Schristos /* Write each host that belongs to this location. */
380a53f50b9Schristos for (host = local->host_list;
381a53f50b9Schristos host != NULL;
382a53f50b9Schristos host = NEXT(struct sun_host, host)) {
383a53f50b9Schristos /* set fstype NFS */
384a53f50b9Schristos xstrlcat(dest, AMD_TYPE_NFS_KW, destlen);
385a53f50b9Schristos /* add rhost key word */
386a53f50b9Schristos xstrlcat(dest, AMD_RHOST_KW, destlen);
387a53f50b9Schristos /* add host name */
388a53f50b9Schristos sun_append_str(dest, destlen, key, host->name);
389a53f50b9Schristos xstrlcat(dest, ";", destlen);
390a53f50b9Schristos /* add remote fs key word */
391a53f50b9Schristos xstrlcat(dest, AMD_RFS_KW, destlen);
392a53f50b9Schristos /* add local path */
393a53f50b9Schristos sun_append_str(dest, destlen, key, local->path);
394a53f50b9Schristos if (NEXT(struct sun_host, host) != NULL) {
395a53f50b9Schristos xstrlcat(dest, ";", destlen);
396a53f50b9Schristos xstrlcat(dest, " ", destlen);
397a53f50b9Schristos }
398a53f50b9Schristos }
399a53f50b9Schristos }
400a53f50b9Schristos else {
401a53f50b9Schristos /* no host location */
402a53f50b9Schristos xstrlcat(dest, AMD_FS_KW, destlen);
403a53f50b9Schristos sun_append_str(dest, destlen, key, local->path);
404a53f50b9Schristos }
405a53f50b9Schristos if (NEXT(struct sun_location, local) != NULL) {
406a53f50b9Schristos /* add a space to separate each location */
407a53f50b9Schristos xstrlcat(dest, " ", destlen);
408a53f50b9Schristos }
409a53f50b9Schristos }
410a53f50b9Schristos }
411a53f50b9Schristos
412a53f50b9Schristos
413a53f50b9Schristos /*
414a53f50b9Schristos * Convert a Sun HSFS mount point to an Amd. The result is
415a53f50b9Schristos * concatenated intp dest.
416a53f50b9Schristos *
417a53f50b9Schristos * param dest - destination buffer
418a53f50b9Schristos * param destlen - destination buffer length
419a53f50b9Schristos * param key - automount key
420a53f50b9Schristos * param s_entry - Sun entry
421a53f50b9Schristos */
422a53f50b9Schristos static void
sun_hsfs2amd(char * dest,size_t destlen,const char * key,const struct sun_entry * s_entry)423a53f50b9Schristos sun_hsfs2amd(char *dest,
424a53f50b9Schristos size_t destlen,
425a53f50b9Schristos const char *key,
426a53f50b9Schristos const struct sun_entry *s_entry)
427a53f50b9Schristos {
428a53f50b9Schristos /* set fstype CDFS */
429a53f50b9Schristos xstrlcat(dest, AMD_TYPE_CDFS_KW, destlen);
430a53f50b9Schristos /* set the cdrom device */
431a53f50b9Schristos xstrlcat(dest, AMD_DEV_KW, destlen);
432a53f50b9Schristos /* XXX: For now just assume that there is only one device. */
433a53f50b9Schristos xstrlcat(dest, s_entry->location_list->path, destlen);
434a53f50b9Schristos }
435a53f50b9Schristos
436a53f50b9Schristos
437a53f50b9Schristos /*
438a53f50b9Schristos * Convert a Sun NFS automount entry to an Amd. The result is concatenated
439a53f50b9Schristos * into dest.
440a53f50b9Schristos *
441a53f50b9Schristos * param dest - destination buffer
442a53f50b9Schristos * param destlen - destination buffer length
443a53f50b9Schristos * param key - automount key
444a53f50b9Schristos * param s_entry - Sun entry
445a53f50b9Schristos */
446a53f50b9Schristos static void
sun_nfs2amd(char * dest,size_t destlen,const char * key,const struct sun_entry * s_entry)447a53f50b9Schristos sun_nfs2amd(char *dest,
448a53f50b9Schristos size_t destlen,
449a53f50b9Schristos const char *key,
450a53f50b9Schristos const struct sun_entry *s_entry)
451a53f50b9Schristos {
452a53f50b9Schristos if (s_entry->location_list != NULL) {
453a53f50b9Schristos /* write out the list of mountpoint locations */
454a53f50b9Schristos sun_locations2amd(dest, destlen, key, s_entry->location_list);
455a53f50b9Schristos }
456a53f50b9Schristos }
457a53f50b9Schristos
458a53f50b9Schristos
459a53f50b9Schristos /*
460a53f50b9Schristos * Convert a Sun multi-mount point entry to an Amd. This is done
461a53f50b9Schristos * using the Amd type auto. Each auto entry is separated with a \n.
462a53f50b9Schristos *
463a53f50b9Schristos * param dest - destination buffer
464a53f50b9Schristos * param destlen - destination buffer length
465a53f50b9Schristos * param key - automount key
466a53f50b9Schristos * param s_entry - Sun entry
467a53f50b9Schristos */
468a53f50b9Schristos static void
sun_multi2amd(char * dest,size_t destlen,const char * key,const struct sun_entry * s_entry)469a53f50b9Schristos sun_multi2amd(char *dest,
470a53f50b9Schristos size_t destlen,
471a53f50b9Schristos const char *key,
472a53f50b9Schristos const struct sun_entry *s_entry)
473a53f50b9Schristos {
474a53f50b9Schristos const struct sun_mountpt *mountpt;
475a53f50b9Schristos
476a53f50b9Schristos /* We need to setup a auto fs Amd automount point. */
477a53f50b9Schristos xstrlcat(dest, AMD_TYPE_AUTO_KW, destlen);
478a53f50b9Schristos xstrlcat(dest, AMD_MAP_FS_KW, destlen);
479a53f50b9Schristos xstrlcat(dest, AMD_MAP_PREF_KW, destlen);
480a53f50b9Schristos
481a53f50b9Schristos /* write the mountpts to dest */
482a53f50b9Schristos for (mountpt = s_entry->mountpt_list;
483a53f50b9Schristos mountpt != NULL;
484a53f50b9Schristos mountpt = NEXT(struct sun_mountpt, mountpt)) {
485a53f50b9Schristos xstrlcat(dest, "\n", destlen);
486a53f50b9Schristos /* write the key */
487a53f50b9Schristos xstrlcat(dest, key, destlen);
488a53f50b9Schristos /* write the mount path */
489a53f50b9Schristos sun_append_str(dest, destlen, key, mountpt->path);
490a53f50b9Schristos /* space */
491a53f50b9Schristos xstrlcat(dest, " ", destlen);
492a53f50b9Schristos /* Write all the host locations for this mount point. */
493a53f50b9Schristos sun_locations2amd(dest, destlen, key, mountpt->location_list);
494a53f50b9Schristos }
495a53f50b9Schristos }
496a53f50b9Schristos
497a53f50b9Schristos
498a53f50b9Schristos /*
499a53f50b9Schristos * Convert the sun_entry into an Amd equivalent string.
500a53f50b9Schristos *
501a53f50b9Schristos * param key - automount key
502a53f50b9Schristos * param s_entry - Sun style automap entry
503a53f50b9Schristos *
504a53f50b9Schristos * return - Amd entry on succes, NULL on error
505a53f50b9Schristos */
506a53f50b9Schristos char *
sun_entry2amd(const char * key,const char * s_entry_str)507a53f50b9Schristos sun_entry2amd(const char *key, const char *s_entry_str)
508a53f50b9Schristos {
509a53f50b9Schristos char *retval = NULL;
510a53f50b9Schristos char line_buff[INFO_MAX_LINE_LEN];
511a53f50b9Schristos int ws;
512a53f50b9Schristos struct sun_entry *s_entry = NULL;
513a53f50b9Schristos
514a53f50b9Schristos /* The key should not be NULL. */
515a53f50b9Schristos if (key == NULL) {
516a53f50b9Schristos plog(XLOG_ERROR,"Sun key value was null");
517a53f50b9Schristos goto err;
518a53f50b9Schristos }
519a53f50b9Schristos /* The Sun entry string should never be NULL. */
520a53f50b9Schristos if (s_entry_str == NULL) {
521a53f50b9Schristos plog(XLOG_ERROR,"Sun entry value was null");
522a53f50b9Schristos goto err;
523a53f50b9Schristos }
524a53f50b9Schristos
525a53f50b9Schristos /* Make sure there are no trailing white spaces or '\n'. */
526a53f50b9Schristos xstrlcpy(line_buff, s_entry_str, sizeof(line_buff));
527a53f50b9Schristos ws = strlen(line_buff) - 1;
528a53f50b9Schristos while (ws >= 0 && (isspace((unsigned char)line_buff[ws]) || line_buff[ws] == '\n')) {
529a53f50b9Schristos line_buff[ws--] = '\0';
530a53f50b9Schristos }
531a53f50b9Schristos
532a53f50b9Schristos /* Parse the sun entry line. */
533a53f50b9Schristos s_entry = sun_map_parse_read(line_buff);
534a53f50b9Schristos if (s_entry == NULL) {
535a53f50b9Schristos plog(XLOG_ERROR,"could not parse Sun style map");
536a53f50b9Schristos goto err;
537a53f50b9Schristos }
538a53f50b9Schristos
539a53f50b9Schristos memset(line_buff, 0, sizeof(line_buff));
540a53f50b9Schristos
541a53f50b9Schristos if (s_entry->opt_list != NULL) {
542a53f50b9Schristos /* write the mount options to the buffer */
543a53f50b9Schristos sun_opts2amd(line_buff, sizeof(line_buff), key, s_entry->opt_list);
544a53f50b9Schristos }
545a53f50b9Schristos
546a53f50b9Schristos /* Check if this is a multi-mount entry. */
547a53f50b9Schristos if (s_entry->mountpt_list != NULL) {
548a53f50b9Schristos /* multi-mount point */
549a53f50b9Schristos sun_multi2amd(line_buff, sizeof(line_buff), key, s_entry);
550*8bae5d40Schristos retval = xstrdup(line_buff);
551a53f50b9Schristos }
552a53f50b9Schristos else {
553a53f50b9Schristos /* single mount point */
554a53f50b9Schristos if (s_entry->fstype != NULL) {
555a53f50b9Schristos if (NSTREQ(s_entry->fstype, SUN_NFS_TYPE, strlen(SUN_NFS_TYPE))) {
556a53f50b9Schristos /* NFS Type */
557a53f50b9Schristos sun_nfs2amd(line_buff, sizeof(line_buff), key, s_entry);
558*8bae5d40Schristos retval = xstrdup(line_buff);
559a53f50b9Schristos }
560a53f50b9Schristos else if (NSTREQ(s_entry->fstype, SUN_HSFS_TYPE, strlen(SUN_HSFS_TYPE))) {
561a53f50b9Schristos /* HSFS Type (CD fs) */
562a53f50b9Schristos sun_hsfs2amd(line_buff, sizeof(line_buff), key, s_entry);
563*8bae5d40Schristos retval = xstrdup(line_buff);
564a53f50b9Schristos }
565a53f50b9Schristos /*
566a53f50b9Schristos * XXX: The following fstypes are not yet supported.
567a53f50b9Schristos */
568a53f50b9Schristos else if (NSTREQ(s_entry->fstype, SUN_AUTOFS_TYPE, strlen(SUN_AUTOFS_TYPE))) {
569a53f50b9Schristos /* AutoFS Type */
570a53f50b9Schristos plog(XLOG_ERROR, "Sun fstype %s is currently not supported by Amd.",
571a53f50b9Schristos s_entry->fstype);
572a53f50b9Schristos goto err;
573a53f50b9Schristos
574a53f50b9Schristos }
575a53f50b9Schristos else if (NSTREQ(s_entry->fstype, SUN_CACHEFS_TYPE, strlen(SUN_CACHEFS_TYPE))) {
576a53f50b9Schristos /* CacheFS Type */
577a53f50b9Schristos plog(XLOG_ERROR, "Sun fstype %s is currently not supported by Amd.",
578a53f50b9Schristos s_entry->fstype);
579a53f50b9Schristos goto err;
580a53f50b9Schristos }
581a53f50b9Schristos else {
582a53f50b9Schristos plog(XLOG_ERROR, "Sun fstype %s is currently not supported by Amd.",
583a53f50b9Schristos s_entry->fstype);
584a53f50b9Schristos goto err;
585a53f50b9Schristos }
586a53f50b9Schristos }
587a53f50b9Schristos else {
588a53f50b9Schristos plog(XLOG_INFO, "No SUN fstype specified defaulting to NFS.");
589a53f50b9Schristos sun_nfs2amd(line_buff, sizeof(line_buff), key, s_entry);
590*8bae5d40Schristos retval = xstrdup(line_buff);
591a53f50b9Schristos }
592a53f50b9Schristos }
593a53f50b9Schristos
594a53f50b9Schristos err:
595a53f50b9Schristos XFREE(s_entry);
596a53f50b9Schristos return retval;
597a53f50b9Schristos }
598