1 /* $NetBSD: dict_env.c,v 1.1.1.2 2013/01/02 18:59:12 tron Exp $ */
2
3 /*++
4 /* NAME
5 /* dict_env 3
6 /* SUMMARY
7 /* dictionary manager interface to environment variables
8 /* SYNOPSIS
9 /* #include <dict_env.h>
10 /*
11 /* DICT *dict_env_open(name, dummy, dict_flags)
12 /* const char *name;
13 /* int dummy;
14 /* int dict_flags;
15 /* DESCRIPTION
16 /* dict_env_open() opens the environment variable array and
17 /* makes it accessible via the generic operations documented
18 /* in dict_open(3). The \fIname\fR and \fIdummy\fR arguments
19 /* are ignored.
20 /* SEE ALSO
21 /* dict(3) generic dictionary manager
22 /* safe_getenv(3) safe getenv() interface
23 /* LICENSE
24 /* .ad
25 /* .fi
26 /* The Secure Mailer license must be distributed with this software.
27 /* AUTHOR(S)
28 /* Wietse Venema
29 /* IBM T.J. Watson Research
30 /* P.O. Box 704
31 /* Yorktown Heights, NY 10598, USA
32 /*--*/
33
34 /* System library. */
35
36 #include "sys_defs.h"
37 #include <stdio.h> /* sprintf() prototype */
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
41
42 /* Utility library. */
43
44 #include "mymalloc.h"
45 #include "msg.h"
46 #include "safe.h"
47 #include "stringops.h"
48 #include "dict.h"
49 #include "dict_env.h"
50
51 /* dict_env_update - update environment array */
52
dict_env_update(DICT * dict,const char * name,const char * value)53 static int dict_env_update(DICT *dict, const char *name, const char *value)
54 {
55 dict->error = 0;
56
57 /*
58 * Optionally fold the key.
59 */
60 if (dict->flags & DICT_FLAG_FOLD_FIX) {
61 if (dict->fold_buf == 0)
62 dict->fold_buf = vstring_alloc(10);
63 vstring_strcpy(dict->fold_buf, name);
64 name = lowercase(vstring_str(dict->fold_buf));
65 }
66 if (setenv(name, value, 1))
67 msg_fatal("setenv: %m");
68
69 return (DICT_STAT_SUCCESS);
70 }
71
72 /* dict_env_lookup - access environment array */
73
dict_env_lookup(DICT * dict,const char * name)74 static const char *dict_env_lookup(DICT *dict, const char *name)
75 {
76 dict->error = 0;
77
78 /*
79 * Optionally fold the key.
80 */
81 if (dict->flags & DICT_FLAG_FOLD_FIX) {
82 if (dict->fold_buf == 0)
83 dict->fold_buf = vstring_alloc(10);
84 vstring_strcpy(dict->fold_buf, name);
85 name = lowercase(vstring_str(dict->fold_buf));
86 }
87 return (safe_getenv(name));
88 }
89
90 /* dict_env_close - close environment dictionary */
91
dict_env_close(DICT * dict)92 static void dict_env_close(DICT *dict)
93 {
94 if (dict->fold_buf)
95 vstring_free(dict->fold_buf);
96 dict_free(dict);
97 }
98
99 /* dict_env_open - make association with environment array */
100
dict_env_open(const char * name,int unused_flags,int dict_flags)101 DICT *dict_env_open(const char *name, int unused_flags, int dict_flags)
102 {
103 DICT *dict;
104
105 dict = dict_alloc(DICT_TYPE_ENVIRON, name, sizeof(*dict));
106 dict->lookup = dict_env_lookup;
107 dict->update = dict_env_update;
108 dict->close = dict_env_close;
109 dict->flags = dict_flags | DICT_FLAG_FIXED;
110 if (dict_flags & DICT_FLAG_FOLD_FIX)
111 dict->fold_buf = vstring_alloc(10);
112 dict->owner.status = DICT_OWNER_TRUSTED;
113 return (DICT_DEBUG (dict));
114 }
115