TR181-XPON  1.4.0
TR-181 PON manager.
dm_actions.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2022 SoftAtHome
6 **
7 ** Redistribution and use in source and binary forms, with or
8 ** without modification, are permitted provided that the following
9 ** conditions are met:
10 **
11 ** 1. Redistributions of source code must retain the above copyright
12 ** notice, this list of conditions and the following disclaimer.
13 **
14 ** 2. Redistributions in binary form must reproduce the above
15 ** copyright notice, this list of conditions and the following
16 ** disclaimer in the documentation and/or other materials provided
17 ** with the distribution.
18 **
19 ** Subject to the terms and conditions of this license, each
20 ** copyright holder and contributor hereby grants to those receiving
21 ** rights under this license a perpetual, worldwide, non-exclusive,
22 ** no-charge, royalty-free, irrevocable (except for failure to
23 ** satisfy the conditions of this license) patent license to make,
24 ** have made, use, offer to sell, sell, import, and otherwise
25 ** transfer this software, where such license applies only to those
26 ** patent claims, already acquired or hereafter acquired, licensable
27 ** by such copyright holder or contributor that are necessarily
28 ** infringed by:
29 **
30 ** (a) their Contribution(s) (the licensed copyrights of copyright
31 ** holders and non-copyrightable additions of contributors, in
32 ** source or binary form) alone; or
33 **
34 ** (b) combination of their Contribution(s) with the work of
35 ** authorship to which such Contribution(s) was added by such
36 ** copyright holder or contributor, if, at the time the Contribution
37 ** is added, such addition causes such combination to be necessarily
38 ** infringed. The patent license shall not apply to any other
39 ** combinations which include the Contribution.
40 **
41 ** Except as expressly stated above, no rights or licenses from any
42 ** copyright holder or contributor is granted under this license,
43 ** whether expressly, by implication, estoppel or otherwise.
44 **
45 ** DISCLAIMER
46 **
47 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
48 ** CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
49 ** INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
50 ** MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 ** DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
52 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
53 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
54 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
55 ** USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
56 ** AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
58 ** ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 ** POSSIBILITY OF SUCH DAMAGE.
60 **
61 ****************************************************************************/
62 
63 /* Related header */
64 #include "dm_actions.h"
65 
66 /* System headers */
67 #include <stdbool.h>
68 #ifdef _DEBUG_
69 #include <stdio.h>
70 #endif
71 #include <stdlib.h> /* free() */
72 #include <string.h> /* strlen() */
73 
74 /* Other libraries' headers */
75 #include <amxc/amxc_macros.h>
76 #include <amxc/amxc.h>
77 
78 #include <amxp/amxp.h> /* required by amxd_action.h */
79 #include <amxd/amxd_action.h> /* amxd_action_param_read() */
80 #include <amxd/amxd_object.h>
81 
82 /* Own headers */
83 #include "ani.h" /* ani_strip_tc_authentication() */
84 #include "object_intf_priv.h" /* object_intf_priv_t */
85 #include "onu_priv.h" /* onu_priv_t */
86 #include "password.h" /* passwd_check_password() */
87 #include "pon_ctrl.h" /* pon_ctrl_get_param_values() */
88 #include "utils_time.h" /* time_get_system_uptime() */
89 #include "xpon_trace.h"
90 
117 static bool s_ignore_param_reads = false;
118 
125  s_ignore_param_reads = ignore;
126 }
127 
128 static bool process_param_value(const char* const param_name,
129  const amxc_var_t* const ret,
130  amxc_var_t* const retval) {
131  bool rv = false;
132 
133  const amxc_var_t* const params =
134  amxc_var_get_key(ret, "parameters", AMXC_VAR_FLAG_DEFAULT);
135  when_null_trace(params, exit, ERROR, "Failed to extract 'parameters'");
136 
137  const amxc_var_t* const param_value =
138  amxc_var_get_key(params, param_name, AMXC_VAR_FLAG_DEFAULT);
139  when_null_trace(param_value, exit, ERROR, "Failed to get value for '%s'", param_name);
140 
141  const int rc = amxc_var_copy(retval, param_value);
142  when_failed_trace(rc, exit, ERROR, "Failed to copy value for '%s'", param_name);
143 
144  rv = true;
145 
146 exit:
147  return rv;
148 }
149 
150 
162 amxd_status_t _read_trx_param(amxd_object_t* object,
163  amxd_param_t* param,
164  amxd_action_t reason,
165  const amxc_var_t* const args,
166  amxc_var_t* const retval,
167  void* priv) {
168 
169  amxd_status_t rv = amxd_status_unknown_error;
170 
171  SAH_TRACEZ_DEBUG2(ME, "called");
172 
173  when_null_status(object, exit, rv = amxd_status_invalid_function_argument);
174  when_null_status(param, exit, rv = amxd_status_invalid_function_argument);
175  when_null_status(retval, exit, rv = amxd_status_invalid_function_argument);
176 
177  rv = amxd_action_param_read(object, param, reason, args, retval, priv);
178 
179  when_failed_trace(rv, exit, ERROR, "amxd_action_param_read() failed");
180 
181  when_true(s_ignore_param_reads, exit);
182 
192  const amxd_object_type_t type = amxd_object_get_type(object);
193  when_true(amxd_object_template == type, exit);
194 
195  const char* const param_name = amxd_param_get_name(param);
196  when_null_trace(param_name, exit, ERROR, "param_name is NULL");
197 
198  char* path = amxd_object_get_path(object, AMXD_OBJECT_INDEXED);
199  when_null_trace(path, exit, ERROR, "path is NULL");
200 
201  SAH_TRACEZ_DEBUG(ME, "path=%s param=%s", path, param_name);
202 
203  amxc_var_t ret;
204  amxc_var_init(&ret);
205 
206  if(pon_ctrl_get_param_values(path, param_name, &ret) != 0) {
207  SAH_TRACEZ_ERROR(ME, "Failed to query %s.%s", path, param_name);
208  goto exit_cleanup;
209  }
210 
211 #ifdef _DEBUG_
212  printf("tr181-xpon: read_trx_param():\n");
213  amxc_var_dump(&ret, STDOUT_FILENO);
214 #endif
215 
216  /* Extract the parameter value from 'ret' and assign it to 'retval'. */
217  if(!process_param_value(param_name, &ret, retval)) {
218  goto exit_cleanup;
219  }
220 
221  rv = 0;
222 
223 exit_cleanup:
224  free(path);
225  amxc_var_clean(&ret);
226 
227 exit:
228  return rv;
229 }
230 
241 amxd_status_t _lastchange_on_read(amxd_object_t* const object,
242  UNUSED amxd_param_t* const param,
243  amxd_action_t reason,
244  UNUSED const amxc_var_t* const args,
245  amxc_var_t* const retval,
246  UNUSED void* priv_unused) {
247  amxd_status_t rv = amxd_status_unknown_error;
248  object_intf_priv_t* priv = NULL;
249  uint32_t since_change = 0;
250  uint32_t uptime = 0;
251  char* path = NULL;
252 
253  if(reason != action_param_read) {
254  SAH_TRACEZ_WARNING(ME, "wrong reason, expected action_param_read(%d) got %d",
255  action_param_read, reason);
256  rv = amxd_status_invalid_action;
257  goto exit;
258  }
259 
260  when_null_trace(object, skip, ERROR, "object can not be NULL");
261  path = amxd_object_get_path(object, AMXD_OBJECT_INDEXED);
267  when_null_trace(object->priv, skip, DEBUG, "object %s has no private data", path);
268  priv = (object_intf_priv_t*) object->priv;
269  uptime = time_get_system_uptime();
270  when_true_trace(uptime < priv->last_change, skip, ERROR,
271  "uptime is smaller than LastChange");
272  since_change = uptime - priv->last_change;
273  rv = amxd_status_ok;
274 
275 skip:
276  when_failed_trace(amxc_var_set_uint32_t(retval, since_change),
277  exit, ERROR, "Failed to set parameter LastChange");
278 exit:
279  if(path) {
280  free(path);
281  }
282  return rv;
283 }
284 
285 typedef enum _private_data_type {
289 
290 
302 amxd_status_t object_destroyed_common(amxd_object_t* object,
303  amxd_action_t reason,
304  private_data_type_t private_data_type) {
305 
306  amxd_status_t rv = amxd_status_unknown_error;
307  char* path = NULL;
308 
309  if(reason != action_object_destroy) {
310  SAH_TRACEZ_WARNING(ME, "Wrong reason, expected action_object_destroy(%d) got %d",
311  action_object_destroy, reason);
312  rv = amxd_status_invalid_action;
313  goto exit;
314  }
315 
316  when_null_trace(object, exit, ERROR, "object is NULL");
317  path = amxd_object_get_path(object, AMXD_OBJECT_INDEXED);
318  when_null_trace(object->priv, exit, WARNING,
319  "object %s has no private data", path);
320  SAH_TRACEZ_DEBUG(ME, "Delete private data of %s", path);
321  switch(private_data_type) {
323  onu_priv_delete_private_data((onu_priv_t*) object->priv);
324  break;
327  break;
328  default:
329  break;
330  }
331  object->priv = NULL;
332  rv = amxd_status_ok;
333 
334 exit:
335  if(path) {
336  free(path);
337  }
338  return rv;
339 }
340 
351 amxd_status_t _onu_destroyed(amxd_object_t* object,
352  UNUSED amxd_param_t* param,
353  amxd_action_t reason,
354  UNUSED const amxc_var_t* const args,
355  UNUSED amxc_var_t* const retval,
356  UNUSED void* priv_unused) {
357 
358  return object_destroyed_common(object, reason, private_data_for_onu);
359 }
360 
361 
372 amxd_status_t _interface_object_destroyed(amxd_object_t* object,
373  UNUSED amxd_param_t* param,
374  amxd_action_t reason,
375  UNUSED const amxc_var_t* const args,
376  UNUSED amxc_var_t* const retval,
377  UNUSED void* priv_unused) {
378 
379  return object_destroyed_common(object, reason,
381 }
382 
383 
394 amxd_status_t _check_password(amxd_object_t* object,
395  UNUSED amxd_param_t* param,
396  amxd_action_t reason,
397  const amxc_var_t* const args,
398  UNUSED amxc_var_t* const retval,
399  UNUSED void* priv) {
400  amxd_status_t status = amxd_status_unknown_error;
401  const char* password = NULL;
402  char* path = NULL;
403  amxc_string_t ani_path;
404  amxc_string_init(&ani_path, 0);
405 
406  when_null_status(object, exit, status = amxd_status_invalid_function_argument);
407  when_null_status(args, exit, status = amxd_status_invalid_function_argument);
408 
409  when_true_status(reason != action_param_validate, exit,
410  status = amxd_status_function_not_implemented);
411 
412  password = amxc_var_constcast(cstring_t, args);
413  when_null_trace(password, exit, ERROR, "password is NULL");
414 
415  if(strlen(password) != 0) {
416 
417  path = amxd_object_get_path(object, AMXD_OBJECT_INDEXED);
418  when_null_trace(path, exit, ERROR, "path is NULL");
419  if(!ani_strip_tc_authentication(path, &ani_path)) {
420  SAH_TRACEZ_ERROR(ME, "%s: failed to check password", path);
421  goto exit;
422  }
423 
424  SAH_TRACEZ_DEBUG(ME, "path='%s' password='%s'", path, password);
425 
426  const char* const ani_path_cstr = amxc_string_get(&ani_path, 0);
427  if(!passwd_check_password(ani_path_cstr, password)) {
428  status = amxd_status_invalid_value;
429  goto exit;
430  }
431  }
432 
433  status = amxd_status_ok;
434 
435 exit:
436  if(path) {
437  free(path);
438  }
439  amxc_string_clean(&ani_path);
440  return status;
441 }
442 
bool ani_strip_tc_authentication(const char *const ani_auth_path, amxc_string_t *const ani_path)
Definition: ani.c:86
static bool process_param_value(const char *const param_name, const amxc_var_t *const ret, amxc_var_t *const retval)
Definition: dm_actions.c:128
amxd_status_t _check_password(amxd_object_t *object, UNUSED amxd_param_t *param, amxd_action_t reason, const amxc_var_t *const args, UNUSED amxc_var_t *const retval, UNUSED void *priv)
Definition: dm_actions.c:394
amxd_status_t _lastchange_on_read(amxd_object_t *const object, UNUSED amxd_param_t *const param, amxd_action_t reason, UNUSED const amxc_var_t *const args, amxc_var_t *const retval, UNUSED void *priv_unused)
Definition: dm_actions.c:241
amxd_status_t _read_trx_param(amxd_object_t *object, amxd_param_t *param, amxd_action_t reason, const amxc_var_t *const args, amxc_var_t *const retval, void *priv)
Definition: dm_actions.c:162
amxd_status_t object_destroyed_common(amxd_object_t *object, amxd_action_t reason, private_data_type_t private_data_type)
Definition: dm_actions.c:302
enum _private_data_type private_data_type_t
static bool s_ignore_param_reads
Definition: dm_actions.c:117
void dm_actions_set_ignore_param_reads(bool ignore)
Definition: dm_actions.c:124
_private_data_type
Definition: dm_actions.c:285
@ private_data_for_object_interface
Definition: dm_actions.c:287
@ private_data_for_onu
Definition: dm_actions.c:286
amxd_status_t _onu_destroyed(amxd_object_t *object, UNUSED amxd_param_t *param, amxd_action_t reason, UNUSED const amxc_var_t *const args, UNUSED amxc_var_t *const retval, UNUSED void *priv_unused)
Definition: dm_actions.c:351
amxd_status_t _interface_object_destroyed(amxd_object_t *object, UNUSED amxd_param_t *param, amxd_action_t reason, UNUSED const amxc_var_t *const args, UNUSED amxc_var_t *const retval, UNUSED void *priv_unused)
Definition: dm_actions.c:372
void oipriv_delete_private_data(object_intf_priv_t *priv)
void onu_priv_delete_private_data(onu_priv_t *priv)
Definition: onu_priv.c:89
bool passwd_check_password(const char *const ani_auth_path, const char *const password)
Definition: password.c:99
int pon_ctrl_get_param_values(const char *const path, const char *const names, amxc_var_t *ret)
Definition: pon_ctrl.c:248
uint32_t time_get_system_uptime(void)
Definition: utils_time.c:75
#define SAH_TRACEZ_DEBUG(zone, format,...)
Definition: xpon_trace.h:115
#define SAH_TRACEZ_DEBUG2(zone, format,...)
Definition: xpon_trace.h:127
#define ME
Definition: xpon_trace.h:78