TR181-XPON  1.4.0
TR-181 PON manager.
upgrade_persistency.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 
67 #ifndef _GNU_SOURCE
68 #define _GNU_SOURCE
69 #endif
70 
71 /* Related header */
72 #include "upgrade_persistency.h"
73 
74 /* System headers */
75 #include <errno.h>
76 #include <stdio.h> /* snprintf() */
77 #include <stdlib.h> /* free() */
78 #include <string.h>
79 #include <sys/stat.h> /* mkdir() */
80 #include <sys/types.h> /* mkdir() */
81 #include <unistd.h> /* access() */
82 
83 /* Other libraries' headers */
84 #include <amxc/amxc_macros.h> /* when_null() */
85 #include <amxc/amxc_string.h> /* amxc_string */
86 
87 /* Own headers */
88 #include "file_utils.h" /* write_file() */
89 #include "password_constants.h" /* MAX_PASSWORD_LEN_PLUS_ONE */
90 #include "persistency.h" /* persistency_get_folder() */
91 #include "xpon_trace.h"
92 
98 #define UPGR_STORAGE_DIR_OVERALL "/cfg/system/"
99 #define UPGR_STORAGE_DIR_COMPONENT UPGR_STORAGE_DIR_OVERALL "tr181-xpon/"
100 
101 /* Path to storage dir for upgrade persistent files of this plugin */
102 static char* s_upgr_storage_dir = NULL;
103 
113 
114  if(access(UPGR_STORAGE_DIR_OVERALL, R_OK | W_OK | X_OK) == 0) {
115  if(access(UPGR_STORAGE_DIR_COMPONENT, R_OK | W_OK | X_OK) == 0) {
117  when_null_trace(s_upgr_storage_dir, exit, ERROR,
118  "Failed to allocate mem for s_upgr_storage_dir");
119  goto exit;
120  }
121  if(mkdir(UPGR_STORAGE_DIR_COMPONENT, 0777) == -1) {
122  SAH_TRACEZ_ERROR(ME, "Failed to create %s: %s", UPGR_STORAGE_DIR_COMPONENT,
123  strerror(errno));
124  goto exit;
125  }
127  when_null_trace(s_upgr_storage_dir, exit, ERROR,
128  "Failed to allocate mem for s_upgr_storage_dir");
129  } else {
130  SAH_TRACEZ_WARNING(ME, "No folder for upgrade persistent settings");
131  SAH_TRACEZ_WARNING(ME, "Use folder for reboot persistent settings");
132  const char* const reboot_persistent = persistency_get_folder();
133  if(access(reboot_persistent, R_OK | W_OK | X_OK) != 0) {
134  SAH_TRACEZ_ERROR(ME, "No folder for any persistent settings");
135  goto exit;
136  }
137  s_upgr_storage_dir = strdup(reboot_persistent);
138  when_null_trace(s_upgr_storage_dir, exit, ERROR,
139  "Failed to allocate mem for s_upgr_storage_dir");
140  }
141 
142 exit:
143  SAH_TRACEZ_DEBUG(ME, "dir='%s'", s_upgr_storage_dir ? : "NULL");
144  return;
145 }
146 
153  if(s_upgr_storage_dir) {
154  free(s_upgr_storage_dir);
155  s_upgr_storage_dir = NULL;
156  }
157 }
158 
159 static void create_password_file_path(const char* const ani_object,
160  bool hex,
161  amxc_string_t* const file_path) {
162 
163  amxc_string_set(file_path, s_upgr_storage_dir);
164  amxc_string_appendf(file_path, "%s", ani_object);
165  if(hex) {
166  amxc_string_append(file_path, "_password_hex.txt", 17);
167  } else {
168  amxc_string_append(file_path, "_password_ascii.txt", 19);
169  }
170 }
171 
185 void upgr_persistency_set_password(const char* const ani_path,
186  const char* const password,
187  bool hex) {
188 
189  when_null_trace(s_upgr_storage_dir, exit, DEBUG, "No persistency");
190  when_null(ani_path, exit);
191  when_null(password, exit);
192 
193  SAH_TRACEZ_DEBUG(ME, "ani_path='%s' password='%s' hex=%d",
194  ani_path, password, hex);
195 
196  amxc_string_t file_path_ascii;
197  amxc_string_t file_path_hex;
198  amxc_string_init(&file_path_ascii, 0);
199  amxc_string_init(&file_path_hex, 0);
200 
201  create_password_file_path(ani_path, /*hex=*/ false, &file_path_ascii);
202  create_password_file_path(ani_path, /*hex=*/ true, &file_path_hex);
203 
204  const char* const ascii_cstr = amxc_string_get(&file_path_ascii, 0);
205  const char* const hex_cstr = amxc_string_get(&file_path_hex, 0);
206 
207  if(strlen(password) == 0) {
208  unlink(ascii_cstr);
209  unlink(hex_cstr);
210  } else {
211  write_file(hex ? hex_cstr : ascii_cstr, password);
212  unlink(hex ? ascii_cstr : hex_cstr);
213  }
214 
215  amxc_string_clean(&file_path_ascii);
216  amxc_string_clean(&file_path_hex);
217 
218 exit:
219  return;
220 }
221 
222 #define ASCII_PASSWORD_FILE_INDEX 0
223 #define HEX_PASSWORD_FILE_INDEX 1
224 
242 bool upgr_persistency_get_password(const char* const ani_path,
243  char* const password,
244  bool* hex) {
245 
246  bool rv = false;
247  int i;
248  char password_saved[MAX_PASSWORD_LEN_PLUS_ONE] = { 0 };
249  const char* password_file_used = NULL;
250 
251  amxc_string_t file_path_ascii;
252  amxc_string_t file_path_hex;
253  amxc_string_init(&file_path_ascii, 0);
254  amxc_string_init(&file_path_hex, 0);
255 
256  create_password_file_path(ani_path, /*hex=*/ false, &file_path_ascii);
257  create_password_file_path(ani_path, /*hex=*/ true, &file_path_hex);
258  const char* files[2] = { 0 };
259  files[ASCII_PASSWORD_FILE_INDEX] = amxc_string_get(&file_path_ascii, 0);
260  files[HEX_PASSWORD_FILE_INDEX] = amxc_string_get(&file_path_hex, 0);
261 
262  for(i = 0; i < 2; ++i) {
263  if(access(files[i], F_OK) == 0) {
264  if(!read_first_line_from_file(files[i], password_saved,
266  SAH_TRACEZ_ERROR(ME, "Failed get password from %s", files[i]);
267  goto exit;
268  }
269  password_file_used = files[i];
270  if(HEX_PASSWORD_FILE_INDEX == i) {
271  *hex = true;
272  }
273  break;
274  }
275  }
276  if(password_file_used == NULL) {
277  SAH_TRACEZ_DEBUG(ME, "%s: no password", ani_path);
278  rv = true;
279  goto exit;
280  }
281 
282 
283  if(strlen(password_saved) == 0) {
284  SAH_TRACEZ_ERROR(ME, "Password read from %s is empty",
285  password_file_used);
286  goto exit;
287  }
288 
289  if(snprintf(password, MAX_PASSWORD_LEN_PLUS_ONE, "%s",
290  password_saved) < 0) {
291  SAH_TRACEZ_ERROR(ME, "snprintf() to copy password failed");
292  goto exit;
293  }
294  SAH_TRACEZ_DEBUG(ME, "ani='%s': password='%s' hex=%d", ani_path,
295  password, *hex);
296  rv = true;
297 
298 exit:
299  amxc_string_clean(&file_path_ascii);
300  amxc_string_clean(&file_path_hex);
301  return rv;
302 }
303 
bool write_file(const char *const path, const char *const value)
Definition: file_utils.c:154
bool read_first_line_from_file(const char *const path, char *line, size_t len)
Definition: file_utils.c:212
#define MAX_PASSWORD_LEN_PLUS_ONE
const char * persistency_get_folder(void)
Definition: persistency.c:210
bool upgr_persistency_get_password(const char *const ani_path, char *const password, bool *hex)
#define HEX_PASSWORD_FILE_INDEX
static void create_password_file_path(const char *const ani_object, bool hex, amxc_string_t *const file_path)
#define UPGR_STORAGE_DIR_OVERALL
static char * s_upgr_storage_dir
#define UPGR_STORAGE_DIR_COMPONENT
#define ASCII_PASSWORD_FILE_INDEX
void upgr_persistency_cleanup(void)
void upgr_persistency_init(void)
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