1 /*
2    Bacula(R) - The Network Backup Solution
3 
4    Copyright (C) 2000-2020 Kern Sibbald
5 
6    The original author of Bacula is Kern Sibbald, with contributions
7    from many others, a complete list can be found in the file AUTHORS.
8 
9    You may use this file and others of this release according to the
10    license defined in the LICENSE file, which includes the Affero General
11    Public License, v3.0 ("AGPLv3") and some additional permissions and
12    terms pursuant to its AGPLv3 Section 7.
13 
14    This notice must be preserved when any source code is
15    conveyed and/or propagated.
16 
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 
20 #ifndef CONFIG_STORAGE_H
21 #define CONFIG_STORAGE_H
22 
23 #include "common.h"
24 #include "tray_conf.h"
25 #include "android-fd-service.h"
26 
27 /*
28    ConfigStorage - Responsible for managing all operations (reads, updates, writes) in
29    the bacula-tray-monitor.conf file.
30 */
31 class ConfigStorage
32 {
33 
34 public:
getInstance()35     static ConfigStorage& getInstance() {
36         static ConfigStorage instance;
37         return instance;
38     }
39 
40     // Creates the singleton instance
init(const char * config_path)41     static bool init(const char *config_path) {
42        struct stat statp;
43        ConfigStorage *storage = &ConfigStorage::getInstance();
44        storage->config_path = config_path;
45 
46        /**
47         * If tray-monitor.conf does not exist yet,
48         * create it with default resources.
49        */
50        if (stat(config_path, &statp) != 0) {
51            const char *err_msg;
52            char *deviceId = bstrdup(AndroidFD::deviceId().toLatin1().constData());
53            err_msg = storage->addResource(deviceId, R_CLIENT, true);
54 
55            if (err_msg != NULL) {
56                Dmsg1(0, "Error - could not save default client resource - %s\n", err_msg);
57                return false;
58            }
59 
60            err_msg = storage->saveMonitor(deviceId);
61 
62            if (err_msg != NULL) {
63                Dmsg1(0, "Error - could not save default monitor resource - %s\n", err_msg);
64                return false;
65            }
66 
67            free(deviceId);
68        }
69 
70        return storage->reloadResources();
71     }
72 
73 private:
74     CONFIG *config = NULL;
75     RES_HEAD **rhead;
76 
ConfigStorage()77     explicit ConfigStorage() {}
78     void writeMonitor(FILE *fp, MONITOR *mon);
79     void writeResource(FILE *fp, RESMON *res, rescode code);
80     const char *validateResource(RESMON *res);
81 
82 public:
83     QString config_path;
84     bool reloadResources();
85     bool reloadResources(bool encodePassword);
86 
87     const char *saveMonitor(const char *deviceId);
88     const char *saveMonitor(MONITOR *mon);
89     MONITOR *getMonitor();
90 
91     const char *addResource(const char *resName, rescode code);
92     const char *addResource(const char *resName, rescode code, bool managed);
93     const char *addResource(RESMON *res, rescode code);
94     const char *editResource(RESMON *oldRes, RESMON *newRes, rescode code);
95     const char *saveResources(
96             QList<QObject *> *clients,
97             QList<QObject *> *directors,
98             QList<QObject *> *storages
99             );
100 
101     QList<RESMON *> *getAllResources();
102     QList<RESMON *> *getResources(rescode resCode);
103     RESMON *getResourceByName(rescode resCode, const char* resName);
104 
105     //Debug Only
106     void dump_storage();
107 };
108 
109 #endif // CONFIG_STORAGE_H
110