TR181-XPON  1.4.0
TR-181 PON manager.
password.c
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** SPDX-License-Identifier: BSD-2-Clause-Patent
4 **
5 ** SPDX-FileCopyrightText: Copyright (c) 2023 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 "password.h"
65 
66 /* System headers */
67 #include <ctype.h> /* isxdigit() */
68 #include <string.h> /* strlen() */
69 
70 /* Other libraries' headers */
71 #include <amxc/amxc_macros.h> /* when_true() */
72 #include <amxc/amxc_string.h>
73 #include <amxc/amxc_variant.h>
74 
75 /* Own headers */
76 #include "ani.h" /* ani_append_tc_authentication() */
77 #include "data_model.h" /* dm_is_hex_password() */
78 #include "password_constants.h" /* MAX_GPON_PASSWORD_LEN */
79 #include "pon_ctrl.h" /* pon_ctrl_set_password() */
80 #include "upgrade_persistency.h" /* upgr_persistency_set_password() */
81 #include "xpon_trace.h"
82 
83 
99 bool passwd_check_password(const char* const ani_path, const char* const password) {
100 
101  bool rv = false;
102  size_t i;
103 
104  SAH_TRACEZ_DEBUG(ME, "ani_path='%s' password='%s'", ani_path, password);
105 
106  bool is_hex = false;
107  if(!dm_is_hex_password(ani_path, &is_hex)) {
108  SAH_TRACEZ_ERROR(ME, "Failed to determine if password is in hex");
109  goto exit;
110  }
111 
112  const size_t len = strlen(password);
113  if(is_hex) {
114  if((len % 2) != 0) {
115  SAH_TRACEZ_ERROR(ME, "hex password does not have even number of chars");
116  goto exit;
117  }
118  for(i = 0; i < len; i++) {
119  if(!isxdigit(password[i])) {
120  SAH_TRACEZ_ERROR(ME, "hex password has non-hexadecimal digit");
121  goto exit;
122  }
123  }
124  }
125 
126  pon_mode_t pon_mode = pon_mode_unknown;
127  if(!dm_get_ani_pon_mode(ani_path, &pon_mode)) {
128  SAH_TRACEZ_ERROR(ME, "Failed to get PON mode");
129  goto exit;
130  }
131  when_true_trace(pon_mode_unknown == pon_mode, exit, ERROR,
132  "Unknown PON mode");
133 
134  size_t maxlen =
135  (pon_mode_gpon == pon_mode) ?
137 
138  if(is_hex) {
139  maxlen *= 2; /* hex value is twice the size of an ASCII value */
140  if(len != maxlen) {
141  SAH_TRACEZ_ERROR(ME, "Length of password = %zd != %zd [mode='%s', hex=1]",
142  len, maxlen, pon_mode_to_string(pon_mode));
143  goto exit;
144  }
145  } else if(len > maxlen) {
146  SAH_TRACEZ_ERROR(ME, "Length of password = %zd > %zd [mode='%s', hex=0]",
147  len, maxlen, pon_mode_to_string(pon_mode));
148  goto exit;
149  }
150 
151  rv = true;
152 
153 exit:
154  return rv;
155 }
156 
168 void passwd_set_password(const char* const ani_path,
169  const char* const password) {
170 
171  SAH_TRACEZ_DEBUG(ME, "ani_path='%s' password='%s'", ani_path, password);
172 
173  bool is_hex = false;
174  if(strlen(password) != 0) {
175  if(!dm_is_hex_password(ani_path, &is_hex)) {
176  SAH_TRACEZ_ERROR(ME, "%s: failed to determine if password is in "
177  "hex", ani_path);
178  goto exit;
179  }
180  }
181 
182  upgr_persistency_set_password(ani_path, password, is_hex);
183  pon_ctrl_set_password(ani_path, password, is_hex);
184 
185 exit:
186  return;
187 }
188 
189 static void update_password_in_dm(const char* const ani_path,
190  const char* const password, bool hex) {
191 
192  amxc_string_t ani_auth_path;
193  amxc_var_t args;
194 
195  amxc_string_init(&ani_auth_path, 0);
196  amxc_var_init(&args);
197 
198  ani_append_tc_authentication(ani_path, &ani_auth_path);
199  const char* const ani_auth_path_cstr = amxc_string_get(&ani_auth_path, 0);
200 
201  const int rc = amxc_var_set_type(&args, AMXC_VAR_ID_HTABLE);
202  when_false_trace(0 == rc, exit, ERROR, "Failed to set type to htable");
203 
204  amxc_var_t* const path_var = amxc_var_add_key(cstring_t, &args, "path", ani_auth_path_cstr);
205  when_null_trace(path_var, exit, ERROR, "Failed to add 'path' to 'args'");
206 
207  amxc_var_t* const params = amxc_var_add_key(amxc_htable_t, &args, "parameters", NULL);
208  when_null_trace(params, exit, ERROR, "Failed to add 'parameters' to 'args'");
209 
210  amxc_var_t* const password_var = amxc_var_add_key(cstring_t, params, "Password", password);
211  when_null_trace(password_var, exit, ERROR, "Failed to add Password to 'parameters'");
212 
213  if(hex) {
214  amxc_var_t* const hex_var = amxc_var_add_key(bool, params, "HexadecimalPassword", hex);
215  when_null_trace(hex_var, exit, ERROR, "Failed to add HexadecimalPassword to 'parameters'");
216  }
217 
218  dm_change_object(&args);
219 
220 exit:
221  amxc_string_clean(&ani_auth_path);
222  amxc_var_clean(&args);
223 }
224 
237 void passwd_restore_password(const char* const ani_path) {
238 
239  SAH_TRACEZ_DEBUG(ME, "ani_path='%s'", ani_path);
240  char password[MAX_PASSWORD_LEN_PLUS_ONE] = { 0 };
241  bool hex = false;
242  if(!upgr_persistency_get_password(ani_path, password, &hex)) {
243  SAH_TRACEZ_ERROR(ME, "ani='%s': failed to get password", ani_path);
244  goto exit;
245  }
246  when_true(strlen(password) == 0, exit); /* nothing to do */
247 
248  update_password_in_dm(ani_path, password, hex);
249 
250 exit:
251  return;
252 }
253 
void ani_append_tc_authentication(const char *const ani_path, amxc_string_t *const ani_auth_path)
Definition: ani.c:113
bool dm_is_hex_password(const char *const ani_path, bool *is_hex)
Definition: data_model.c:1236
int dm_change_object(const amxc_var_t *const args)
Definition: data_model.c:821
bool dm_get_ani_pon_mode(const char *const ani_path, pon_mode_t *pon_mode)
Definition: data_model.c:1280
void passwd_restore_password(const char *const ani_path)
Definition: password.c:237
bool passwd_check_password(const char *const ani_path, const char *const password)
Definition: password.c:99
void passwd_set_password(const char *const ani_path, const char *const password)
Definition: password.c:168
static void update_password_in_dm(const char *const ani_path, const char *const password, bool hex)
Definition: password.c:189
#define MAX_PASSWORD_LEN_PLUS_ONE
#define MAX_XPON_PASSWORD_LEN
#define MAX_GPON_PASSWORD_LEN
void pon_ctrl_set_password(const char *const ani_path, const char *const password, bool hex)
Definition: pon_ctrl.c:295
enum _pon_mode pon_mode_t
const char * pon_mode_to_string(pon_mode_t mode)
Definition: pon_mode.c:70
@ pon_mode_unknown
Definition: pon_mode.h:67
@ pon_mode_gpon
Definition: pon_mode.h:68
bool upgr_persistency_get_password(const char *const ani_path, char *const password, bool *hex)
void upgr_persistency_set_password(const char *const ani_path, const char *const password, bool hex)
#define SAH_TRACEZ_DEBUG(zone, format,...)
Definition: xpon_trace.h:115
#define ME
Definition: xpon_trace.h:78