aboutsummaryrefslogtreecommitdiffstats
path: root/amd/sun_map.c
blob: 8e3559960cd31a1ba2cbfed6840fd0c1bb449103 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
/*
 * Copyright (c) 1997-2014 Erez Zadok
 * Copyright (c) 2005 Daniel P. Ottavio
 * Copyright (c) 1990 Jan-Simon Pendry
 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
 * Copyright (c) 1990 The Regents of the University of California.
 * All rights reserved.
 *
 * This code is derived from software contributed to Berkeley by
 * Jan-Simon Pendry at Imperial College, London.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the University nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 *
 * File: am-utils/amd/sun_map.c
 *
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <am_defs.h>
#include <amd.h>
#include <sun_map.h>



/*
 * Add a data pointer to the end of the list.
 */
void
sun_list_add(struct sun_list *list, qelem *item)
{
  if (list->last == NULL) {
    list->last = item;
    list->first = item;
    item->q_back = NULL;
  }
  else {
    list->last->q_forw = item;
    item->q_back = list->last;
    list->last = item;
  }

  item->q_forw = NULL;
}


/*
 * Sun2Amd conversion routines
 */

/*
 * AMD entry keywords
 */
#define AMD_OPTS_KW      "addopts:="     /* add entry options */
#define AMD_RHOST_KW     "rhost:="       /* remote host */
#define AMD_RFS_KW       "rfs:="         /* remote file system */
#define AMD_FS_KW        "fs:="          /* local file system */
#define AMD_DEV_KW       "dev:="         /* device */
#define AMD_TYPE_NFS_KW  "type:=nfs;"    /* fs type nfs */
#define AMD_TYPE_AUTO_KW "type:=auto;"   /* fs type auto */
#define AMD_TYPE_CDFS_KW "type:=cdfs;"   /* fs type cd */
#define AMD_MAP_FS_KW    "fs:=${map};"   /* set the mount map as current map */
#define AMD_MAP_PREF_KW  "pref:=${key}/" /* set the mount map as current map */

/*
 * A set of string Sun fstypes.
 */
#define SUN_NFS_TYPE     "nfs"
#define SUN_HSFS_TYPE    "hsfs" /* CD fs */
#define SUN_AUTOFS_TYPE  "autofs"
#define SUN_CACHEFS_TYPE "cachefs"

#define SUN_KEY_SUB      "&"         /* Sun key substitution */

/* a set a Sun variable substitutions for map entries */
#define SUN_ARCH         "$ARCH"     /* host architecture */
#define SUN_CPU          "$CPU"      /* processor type */
#define SUN_HOST         "$HOST"     /* host name */
#define SUN_OSNAME       "$OSNAME"   /* OS name */
#define SUN_OSREL        "$OSREL"    /* OS release */
#define SUN_OSVERS       "$OSVERS"   /* OS version */
#define SUN_NATISA       "$NATISA"   /* native instruction set */

/* a set of Amd variable substitutions */
#define AMD_ARCH         "${arch}"   /* host architecture */
#define AMD_HOST         "${host}"   /* host name */
#define AMD_OSNAME       "${os}"     /* OS name */
#define AMD_OSVER        "${osver}"  /* OS version */


/*
 * Return a copy of src that has all occurrences of 'str' replaced
 * with sub.
 *
 * param src - the original string
 * param str - string that is the replaced with str
 * param sub - string that replaces an occurrences of 'delim'
 *
 * return - new string with str substitutions, NULL on error
 */
static char *
sun_strsub(const char *src, const char *str, const char *sub)
{

  char *retval = NULL, *str_start, *str_end, *src_end;
  size_t total_size, first_half, second_half, sub_size;

  /* assign pointers to the start and end of str */
  if ((str_start = strstr(src, str)) == NULL) {
    return retval;
  }
  str_end = (strlen(str) - 1) + str_start;

  /* assign to the end of the src. */
  src_end = (strlen(src) - 1) + (char*)src;

  /* size from the beginning of src to the start of str */
  first_half = (size_t)(str_start - src);

  /* size from the end of str to the end of src */
  second_half = (size_t)(src_end - str_end);

  sub_size = strlen(sub);

  total_size = (first_half + sub_size + second_half + 1);

  retval = (char*)xmalloc(total_size);
  memset(retval, 0, total_size);

  /*
   * Put together the string such that the first half is copied
   * followed the sub and second half.
   *
   * We use strncpy instead of xstrlcpy because we are intentionally
   * causing truncation and we don't want this to cause errors in the
   * log.
   */
  (void)strncpy(retval, src, first_half);
  (void)strncat(retval, sub, sub_size);
  (void)strncat(retval, str_end + 1, second_half);

  if (strstr(retval, str) != NULL) {
    /*
     * If there is another occurrences of str call this function
     * recursively.
     */
    char* tmp;
    if ((tmp = sun_strsub(retval, str, sub)) != NULL) {
      XFREE(retval);
      retval = tmp;
    }
  }
  return retval;
}


/*
 * Return a new string that is a copy of str, all occurrences of a Sun
 * variable substitutions are replaced by there equivalent Amd
 * substitutions.
 *
 * param str - source string
 *
 * return - A new string with the expansions, NULL if str does not
 * exist in src or error.
 */
static char *
sun_expand2amd(const char *str)
{

  char *retval = NULL, *tmp = NULL, *tmp2 = NULL;
  const char *pos;

  /*
   * Iterator through the string looking for '$' chars.  For each '$'
   * found try to replace it with Sun variable substitutions.  If we
   * find a '$' that is not a substation each of the i.e $blah than
   * each of the replace attempt will fail and we'll move on to the
   * next char.
   */
  tmp = xstrdup(str);
  for (pos = str; *pos != '\0'; pos++) {
    if (*pos != '$') {
      continue;
    }
    if (tmp2 != NULL) {
      XFREE(tmp);
      tmp = tmp2;
    }

    /*
     * If a 'replace' does not return NULL than a variable was
     * successfully substituted.
     */

    /* architecture */
    if ((tmp2 = sun_strsub(tmp, SUN_ARCH, AMD_ARCH)) != NULL) {
      continue;
    }
    /* cpu - there is not POSIX uname for cpu so just use machine */
    if ((tmp2 = sun_strsub(tmp, SUN_CPU, AMD_ARCH)) != NULL) {
      continue;
    }
    /* hostname */
    if ((tmp2 = sun_strsub(tmp, SUN_HOST, AMD_HOST)) != NULL) {
      continue;
    }
    /* os name */
    if ((tmp2 = sun_strsub(tmp, SUN_OSNAME, AMD_OSNAME)) != NULL) {
      continue;
    }
    /*
     * os release - Amd doesn't hava a OS release var just usr os
     * version or now.
     */
    if ((tmp2 = sun_strsub(tmp, SUN_OSREL, AMD_OSVER)) != NULL) {
      continue;
    }
    /* os version */
    if ((tmp2 = sun_strsub(tmp, SUN_OSVERS, AMD_OSVER)) != NULL) {
      continue;
    }
    /* native instruction set - there is no POSIX natisa so just use system */
    if ((tmp2 = sun_strsub(tmp, SUN_NATISA, AMD_ARCH)) != NULL) {
      continue;
    }
  }
  if (tmp2 == NULL) {
    retval = tmp;
  }
  else {
    retval = tmp2;
    XFREE(tmp);
  }

  return retval;
}


/*
 * This is a wrapper function for appending Amd entry information to a
 * buffer.  Any Sun variable substitutions will be converted into Amd
 * equivalents.
 *
 * param dest   - destination buffer
 * param deslen - destination buffer length
 * param key    - entry key, this might be needed for key substitutions
 * param str    - string to append
 */
static void
sun_append_str(char *dest,
	       size_t destlen,
	       const char *key,
	       const char *str)
{
  char *sub = NULL, *sub2 = NULL, *out = NULL;

  /* By default we are going to just write the original string. */
  out = (char*)str;

  /*
   * Resolve variable substitutions in two steps; 1) replace any key
   * map substitutions with the entry key 2) expand any variable
   * substitutions i.e $HOST.
   *
   * Try to replace the key substitution '&'. If this function returns
   * with a new string, one or more key subs. where replaced with the
   * entry key.
   */
  if ((sub = sun_strsub(str, SUN_KEY_SUB, "${key}")) != NULL) {
    out = sub;
    /*
     * Try to convert any variable substitutions. If this function
     * returns a new string one or more var subs where expanded.
     */
    if ((sub2 = sun_expand2amd(sub)) != NULL) {
      out = sub2;
    }
  }
  /*
   * Try to convert any variable substitutions. If this function
   * returns a new string one or more var subs where expanded.
   */
  else if (out != NULL && (sub = sun_expand2amd(out)) != NULL) {
    out = sub;
  }

  if (out != NULL) {
    xstrlcat(dest, out, destlen);
  }
  XFREE(sub);
  XFREE(sub2);
}


/*
 * Convert the list of Sun mount options to Amd mount options.  The
 * result is concatenated to dest.
 *
 * param dest     - destination buffer
 * param destlen  - destination buffer length
 * param key      - automount key
 * param opt_list - list of Sun mount options
 */
static void
sun_opts2amd(char *dest,
	     size_t destlen,
	     const char *key,
	     const struct sun_opt *opt_list)
{
  const struct sun_opt *opt;

  xstrlcat(dest, AMD_OPTS_KW, destlen);

  /* Iterate through each option and append it to the buffer. */
  for(opt = opt_list; opt != NULL; opt = NEXT(struct sun_opt, opt)) {
    sun_append_str(dest, destlen, key, opt->str);
    /* If there are more options add some commas. */
    if (NEXT(struct sun_opt, opt) != NULL) {
      xstrlcat(dest, ",", destlen);
    }
  }
  xstrlcat(dest, ";", destlen);
}


/*
 * Convert the list of Sun mount locations to a list of Amd mount
 * locations.  The result is concatenated to dest.
 *
 * param dest       - destination buffer
 * param destlen    - destination buffer length
 * param key        - automount key
 * param local_list - list of Sun mount locations
 */
static void
sun_locations2amd(char *dest,
		  size_t destlen,
		  const char *key,
		  const struct sun_location *local_list)
{
  const struct sun_location *local;
  const struct sun_host *host;

  for (local = local_list;
       local != NULL;
       local = NEXT(struct sun_location,local)) {
    /*
     * Check to see if the list of hosts is empty.  Some mount types
     * i.e cd-rom may have mount location with no host.
     */
    if (local->host_list != NULL) {
      /* Write each host that belongs to this location. */
      for (host = local->host_list;
	   host != NULL;
	   host = NEXT(struct sun_host, host)) {
	/* set fstype NFS */
	xstrlcat(dest, AMD_TYPE_NFS_KW, destlen);
	/* add rhost key word */
	xstrlcat(dest, AMD_RHOST_KW, destlen);
	/* add host name */
	sun_append_str(dest, destlen, key, host->name);
	xstrlcat(dest, ";", destlen);
	/* add remote fs key word */
	xstrlcat(dest, AMD_RFS_KW, destlen);
	/* add local path */
	sun_append_str(dest, destlen, key, local->path);
	if (NEXT(struct sun_host, host) != NULL) {
	  xstrlcat(dest, ";", destlen);
	  xstrlcat(dest, " ", destlen);
	}
      }
    }
    else {
      /* no host location */
      xstrlcat(dest, AMD_FS_KW, destlen);
      sun_append_str(dest, destlen, key, local->path);
    }
    if (NEXT(struct sun_location, local) != NULL) {
      /* add a space to separate each location */
      xstrlcat(dest, " ", destlen);
    }
  }
}


/*
 * Convert a Sun HSFS mount point to an Amd.  The result is
 * concatenated intp dest.
 *
 * param dest    - destination buffer
 * param destlen - destination buffer length
 * param key     - automount key
 * param s_entry - Sun entry
 */
static void
sun_hsfs2amd(char *dest,
	     size_t destlen,
	     const char *key,
	     const struct sun_entry *s_entry)
{
  /* set fstype CDFS */
  xstrlcat(dest, AMD_TYPE_CDFS_KW, destlen);
  /* set the cdrom device */
  xstrlcat(dest, AMD_DEV_KW, destlen);
  /* XXX: For now just assume that there is only one device. */
  xstrlcat(dest, s_entry->location_list->path, destlen);
}


/*
 * Convert a Sun NFS automount entry to an Amd.  The result is concatenated
 * into dest.
 *
 * param dest    - destination buffer
 * param destlen - destination buffer length
 * param key     - automount key
 * param s_entry - Sun entry
 */
static void
sun_nfs2amd(char *dest,
	    size_t destlen,
	    const char *key,
	    const struct sun_entry *s_entry)
{
  if (s_entry->location_list != NULL) {
    /* write out the list of mountpoint locations */
    sun_locations2amd(dest, destlen, key, s_entry->location_list);
  }
}


/*
 * Convert a Sun multi-mount point entry to an Amd.  This is done
 * using the Amd type auto.  Each auto entry is separated with a \n.
 *
 * param dest    - destination buffer
 * param destlen - destination buffer length
 * param key     - automount key
 * param s_entry - Sun entry
 */
static void
sun_multi2amd(char *dest,
	      size_t destlen,
	      const char *key,
	      const struct sun_entry *s_entry)
{
  const struct sun_mountpt *mountpt;

  /* We need to setup a auto fs Amd automount point. */
  xstrlcat(dest, AMD_TYPE_AUTO_KW, destlen);
  xstrlcat(dest, AMD_MAP_FS_KW, destlen);
  xstrlcat(dest, AMD_MAP_PREF_KW, destlen);

  /* write the mountpts to dest */
  for (mountpt = s_entry->mountpt_list;
       mountpt != NULL;
       mountpt = NEXT(struct sun_mountpt, mountpt)) {
    xstrlcat(dest, "\n", destlen);
    /* write the key */
    xstrlcat(dest, key, destlen);
    /* write the mount path */
    sun_append_str(dest, destlen, key, mountpt->path);
    /* space */
    xstrlcat(dest, " ", destlen);
    /* Write all the host locations for this mount point. */
    sun_locations2amd(dest, destlen, key, mountpt->location_list);
  }
}


/*
 * Convert the sun_entry into an Amd equivalent string.
 *
 * param key     - automount key
 * param s_entry - Sun style automap entry
 *
 * return - Amd entry on succes, NULL on error
 */
char *
sun_entry2amd(const char *key, const char *s_entry_str)
{
  char *retval = NULL;
  char line_buff[INFO_MAX_LINE_LEN];
  int ws;
  struct sun_entry *s_entry = NULL;

  /* The key should not be NULL. */
  if (key == NULL) {
    plog(XLOG_ERROR,"Sun key value was null");
    goto err;
  }
  /* The Sun entry string should never be NULL. */
  if (s_entry_str == NULL) {
    plog(XLOG_ERROR,"Sun entry value was null");
    goto err;
  }

  /* Make sure there are no trailing white spaces or '\n'. */
  xstrlcpy(line_buff, s_entry_str, sizeof(line_buff));
  ws = strlen(line_buff) - 1;
  while (ws >= 0 && (isspace((unsigned char)line_buff[ws]) || line_buff[ws] == '\n')) {
    line_buff[ws--] = '\0';
  }

  /* Parse the sun entry line. */
  s_entry = sun_map_parse_read(line_buff);
  if (s_entry == NULL) {
    plog(XLOG_ERROR,"could not parse Sun style map");
    goto err;
  }

  memset(line_buff, 0, sizeof(line_buff));

  if (s_entry->opt_list != NULL) {
    /* write the mount options to the buffer  */
    sun_opts2amd(line_buff, sizeof(line_buff), key, s_entry->opt_list);
  }

  /* Check if this is a multi-mount entry. */
  if (s_entry->mountpt_list != NULL) {
    /* multi-mount point */
    sun_multi2amd(line_buff, sizeof(line_buff), key, s_entry);
    retval = xstrdup(line_buff);
  }
  else {
    /* single mount point */
    if (s_entry->fstype != NULL) {
      if (NSTREQ(s_entry->fstype, SUN_NFS_TYPE, strlen(SUN_NFS_TYPE))) {
	/* NFS Type */
	sun_nfs2amd(line_buff, sizeof(line_buff), key, s_entry);
	retval = xstrdup(line_buff);
      }
      else if (NSTREQ(s_entry->fstype, SUN_HSFS_TYPE, strlen(SUN_HSFS_TYPE))) {
	/* HSFS Type (CD fs) */
	sun_hsfs2amd(line_buff, sizeof(line_buff), key, s_entry);
	retval = xstrdup(line_buff);
      }
      /*
       * XXX: The following fstypes are not yet supported.
       */
      else if (NSTREQ(s_entry->fstype, SUN_AUTOFS_TYPE, strlen(SUN_AUTOFS_TYPE))) {
	/* AutoFS Type */
	plog(XLOG_ERROR, "Sun fstype %s is currently not supported by Amd.",
	     s_entry->fstype);
	goto err;

      }
      else if (NSTREQ(s_entry->fstype, SUN_CACHEFS_TYPE, strlen(SUN_CACHEFS_TYPE))) {
	/* CacheFS Type */
	plog(XLOG_ERROR, "Sun fstype %s is currently not supported by Amd.",
	     s_entry->fstype);
	goto err;
      }
      else {
	plog(XLOG_ERROR, "Sun fstype %s is currently not supported by Amd.",
	     s_entry->fstype);
	goto err;
      }
    }
    else {
      plog(XLOG_INFO, "No SUN fstype specified defaulting to NFS.");
      sun_nfs2amd(line_buff, sizeof(line_buff), key, s_entry);
      retval = xstrdup(line_buff);
    }
  }

 err:
  XFREE(s_entry);
  return retval;
}