summaryrefslogtreecommitdiff
path: root/src/cstore/cstore-varref.cpp
blob: f4a69187a6d5535094e9ae15952508d22294755a (plain)
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
/*
 * Copyright (C) 2010 Vyatta, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <cstdio>
#include <vector>
#include <string>
#include <algorithm>

#include <cli_cstore.h>
#include <cstore/cstore-varref.hpp>

using namespace cstore;

////// constructors/destructors
Cstore::VarRef::VarRef(Cstore *cstore, const string& ref_str, bool active)
  : _cstore(cstore), _active(active)
{
  /* NOTE: this class will change the paths in the cstore. caller must do
   *       save/restore for the cstore if necessary.
   */
  if (!_cstore) {
    // no cstore
    return;
  }

  _absolute = (ref_str[0] == '/');
  vector<string> tmp;
  while (!_absolute && !_cstore->cfg_path_at_root()) {
    string last;
    _cstore->pop_cfg_path(last);
    tmp.push_back(last);
  }
  while (tmp.size() > 0) {
    _orig_path_comps.push(tmp.back());
    tmp.pop_back();
  }
  _cstore->reset_paths();
  /* at this point, cstore paths are at root. _orig_path_comps contains
   * the path originally in cstore (or empty if _absolute).
   */

  size_t si = (_absolute ? 1 : 0);
  size_t sn = 0;
  Cpath rcomps;
  while (si < ref_str.length()
         && (sn = ref_str.find('/', si)) != ref_str.npos) {
    rcomps.push(ref_str.substr(si, sn - si));
    si = sn + 1;
  }
  if (si < ref_str.length()) {
    rcomps.push(ref_str.substr(si));
  }
  // NOTE: if path ends in '/', the trailing slash is ignored.

  // get the "at" string. this is set inside cli_new.c.
  _at_string = get_at_string();

  // process ref
  Cpath pcomps(_orig_path_comps);
  process_ref(rcomps, pcomps, ERROR_TYPE);
}

/* process the reference(s).
 * this is a recursive function and always keeps the cstore paths unchanged
 * between invocations.
 *
 * note: def_type is added into _paths along with the paths. when it's
 *       ERROR_TYPE, it means the path needs to be checked for existence.
 *       otherwise, the path is a "value" (or "values") read from the
 *       actual config (working or active).
 */
void
Cstore::VarRef::process_ref(const Cpath& ref_comps,
                            const Cpath& cur_path_comps,
                            vtw_type_e def_type)
{
  if (ref_comps.size() == 0) {
    // done
    _paths.push_back(pair<Cpath, vtw_type_e>(cur_path_comps, def_type));
    return;
  }

  Cpath rcomps;
  Cpath pcomps(cur_path_comps);
  string cr_comp = ref_comps[0];
  for (size_t i = 1; i < ref_comps.size(); i++) {
    rcomps.push(ref_comps[i]);
  }

  tr1::shared_ptr<Ctemplate> def(_cstore->parseTmpl(pcomps, false));
  bool got_tmpl = (def.get() != 0);
  bool handle_leaf = false;
  if (cr_comp == "@") {
    if (!got_tmpl) {
      // invalid path
      return;
    }
    if (def->isTypeless()) {
      /* no value for typeless node, so this should be invalid ref
       * according to the spec.
       * XXX however, the original implementation erroneously treats
       * this as valid ref and returns the node "name" as the "value".
       * for backward compatibility, keep the same behavior.
       */
      process_ref(rcomps, pcomps, ERROR_TYPE);
      return;
    }
    if (pcomps.size() == _orig_path_comps.size()) {
      if (pcomps.size() == 0 || pcomps == _orig_path_comps) {
        /* we are at the original path. this is a self-reference, e.g.,
         * $VAR(@), so use the "at string".
         */
        pcomps.push(_at_string);
        process_ref(rcomps, pcomps, def->getType(1));
        return;
      }
    }
    if (!def->isSingleLeafNode() && !def->isMultiLeafNode()) {
      if (pcomps.size() < _orig_path_comps.size()) {
        // within the original path. @ translates to the path comp.
        pcomps.push(_orig_path_comps[pcomps.size()]);
        process_ref(rcomps, pcomps, def->getType(1));
      }
      return;
    }
    // handle leaf node
    handle_leaf = true;
  } else if (cr_comp == ".") {
    process_ref(rcomps, pcomps, ERROR_TYPE);
  } else if (cr_comp == "..") {
    if (!got_tmpl || pcomps.size() == 0) {
      // invalid path
      return;
    }
    pcomps.pop();
    if (pcomps.size() > 0) {
      // not at root yet
      def = _cstore->parseTmpl(pcomps, false);
      if (!def.get()) {
        // invalid tmpl path
        return;
      }
      if (def->isTagValue()) {
        // at "tag value", need to pop one more.
        if (pcomps.size() == 0) {
          // invalid path
          return;
        }
        pcomps.pop();
      }
    }
    process_ref(rcomps, pcomps, ERROR_TYPE);
  } else if (cr_comp == "@@") {
    if (!got_tmpl) {
      // invalid path
      return;
    }
    if (def->isTypeless()) {
      // no value for typeless node
      return;
    }
    if (def->isValue()) {
      // invalid ref
      return;
    }
    if (def->isTag()) {
      // tag node
      vector<string> cnodes;
      _cstore->cfgPathGetChildNodes(pcomps, cnodes, _active);
      for (size_t i = 0; i < cnodes.size(); i++) {
        pcomps.push(cnodes[i]);
        process_ref(rcomps, pcomps, def->getType(1));
        pcomps.pop();
      }
    } else {
      // handle leaf node
      handle_leaf = true;
    }
  } else {
    // just text. go down 1 level.
    if (got_tmpl && def->isTagNode()) {
      // at "tag node". need to go down 1 more level.
      if (pcomps.size() > _orig_path_comps.size()) {
        // already under the original node. invalid ref.
        return;
      } else if (pcomps.size() == _orig_path_comps.size()) {
        // at the tag value. use the at_string.
        pcomps.push(_at_string);
      } else  {
        // within the original path. take the original tag value.
        pcomps.push(_orig_path_comps[pcomps.size()]);
      }
    }
    pcomps.push(cr_comp);
    process_ref(rcomps, pcomps, ERROR_TYPE);
  }

  if (handle_leaf) {
    if (def->isMulti()) {
      // multi-value node
      vector<string> vals;
      if (!_cstore->cfgPathGetValues(pcomps, vals, _active)) {
        return;
      }
      string val;
      for (size_t i = 0; i < vals.size(); i++) {
        if (val.length() > 0) {
          val += " ";
        }
        val += vals[i];
      }
      pcomps.push(val);
      // treat "joined" multi-values as TEXT_TYPE
      _paths.push_back(pair<Cpath, vtw_type_e>(pcomps, TEXT_TYPE));
      // at leaf. stop recursion.
    } else {
      // single-value node
      string val;
      vtw_type_e t = def->getType(1);
      if (!_cstore->cfgPathGetValue(pcomps, val, _active)) {
        /* can't get value => treat it as non-existent (empty value
         * and type ERROR_TYPE)
         */
        val = "";
        t = ERROR_TYPE;
      }
      pcomps.push(val);
      _paths.push_back(pair<Cpath, vtw_type_e>(pcomps, t));
      // at leaf. stop recursion.
    }
  }
}

bool
Cstore::VarRef::getValue(string& value, vtw_type_e& def_type)
{
  vector<string> result;
  MapT<string, bool> added;
  def_type = ERROR_TYPE;
  for (size_t i = 0; i < _paths.size(); i++) {
    if (_paths[i].first.size() == 0) {
      // empty path
      continue;
    }
    if (added.find(_paths[i].first.back()) != added.end()) {
      // already added
      continue;
    }
    if (_paths[i].second == ERROR_TYPE
        && !_cstore->cfgPathExists(_paths[i].first, _active)) {
      // path doesn't exist => empty string
      added[""] = true;
      result.push_back("");
      continue;
    }
    if (_paths[i].second != ERROR_TYPE) {
      // set def_type. all types should be the same if multiple entries exist.
      def_type = _paths[i].second;
    }
    added[_paths[i].first.back()] = true;
    result.push_back(_paths[i].first.back());
  }
  if (result.size() == 0) {
    // got nothing
    return false;
  }
  if (result.size() > 1 || def_type == ERROR_TYPE) {
    /* if no type is available or we are returning "joined" multiple values,
     * treat it as text type.
     */
    def_type = TEXT_TYPE;
  }
  value = "";
  for (size_t i = 0; i < result.size(); i++) {
    if (i > 0) {
      value += " ";
    }
    value += result[i];
  }
  return true;
}

bool
Cstore::VarRef::getSetPath(Cpath& path_comps)
{
  if (_paths.size() != 1) {
    // for set_var_ref operation, there can be only one path.
    return false;
  }
  path_comps = _paths[0].first;
  /* note that for "varref set" operation, the varref must refer to the
   * "value" of a single-value leaf node, e.g.,
   * "$VAR(plaintext-password/@)". so pop the last comp to give the
   * correct path for "set". the caller is responsible for verifying
   * whether the path points to a single-value leaf node.
   */
  path_comps.pop();
  return true;
}