1 /***************************************************************************
2  begin       : Mon Mar 01 2004
3  copyright   : (C) 2018 by Martin Preuss
4  email       : martin@libchipcard.de
5 
6  ***************************************************************************
7  * This file is part of the project "AqBanking".                           *
8  * Please see toplevel file COPYING of that project for license details.   *
9  ***************************************************************************/
10 
11 #ifdef HAVE_CONFIG_H
12 # include <config.h>
13 #endif
14 
15 /* don't warn about our own deprecated functions */
16 //#define AQBANKING_NOWARN_DEPRECATED
17 
18 
19 #include "banking_p.h"
20 #include "backendsupport/provider_l.h"
21 #include "backendsupport/imexporter_l.h"
22 #include "backendsupport/bankinfoplugin_l.h"
23 #include "i18n_l.h"
24 #include "banking_dialogs.h"
25 
26 
27 #include <gwenhywfar/version.h>
28 #include <gwenhywfar/gwenhywfar.h>
29 #include <gwenhywfar/debug.h>
30 #include <gwenhywfar/misc.h>
31 #include <gwenhywfar/directory.h>
32 #include <gwenhywfar/libloader.h>
33 #include <gwenhywfar/text.h>
34 #include <gwenhywfar/xml.h>
35 #include <gwenhywfar/pathmanager.h>
36 #include <gwenhywfar/gui.h>
37 #include <gwenhywfar/dbio.h>
38 #include <gwenhywfar/ctplugin.h>
39 #include <gwenhywfar/configmgr.h>
40 #include <gwenhywfar/syncio_file.h>
41 
42 #include <assert.h>
43 #include <unistd.h>
44 #include <sys/stat.h>
45 #include <errno.h>
46 #include <ctype.h>
47 
48 
49 #ifdef OS_WIN32
50 # define ftruncate chsize
51 # define DIRSEP "\\"
52 #else
53 # define DIRSEP "/"
54 #endif
55 
GWEN_INHERIT_FUNCTIONS(AB_BANKING)56 GWEN_INHERIT_FUNCTIONS(AB_BANKING)
57 
58 #include <aqbanking/error.h>
59 
60 
61 #include "banking_init.c"
62 #include "banking_cfg.c"
63 #include "banking_update.c"
64 
65 #include "banking_accspec.c"
66 #include "banking_account.c"
67 #include "banking_user.c"
68 #include "banking_transaction.c"
69 
70 #include "banking_online.c"
71 #include "banking_imex.c"
72 #include "banking_bankinfo.c"
73 #include "banking_dialogs.c"
74 #include "banking_compat.c"
75 
76 
77 
78 
79 AB_BANKING *AB_Banking_new(const char *appName,
80                            const char *dname,
81                            uint32_t extensions)
82 {
83   AB_BANKING *ab;
84   GWEN_BUFFER *nbuf;
85   char buffer[256];
86   int err;
87 
88   assert(appName);
89   err=GWEN_Init();
90   if (err) {
91     DBG_ERROR_ERR(AQBANKING_LOGDOMAIN, err);
92     abort();
93   }
94   DBG_INFO(AQBANKING_LOGDOMAIN,
95            "Application \"%s\" compiled with extensions %08x",
96            appName, extensions);
97 
98   nbuf=GWEN_Buffer_new(0, 256, 0, 1);
99   if (GWEN_Text_EscapeToBufferTolerant(appName, nbuf)) {
100     DBG_ERROR(AQBANKING_LOGDOMAIN,
101               "Bad application name, aborting.");
102     GWEN_Buffer_free(nbuf);
103     abort();
104   }
105   else {
106     char *s;
107 
108     s=GWEN_Buffer_GetStart(nbuf);
109     while (*s) {
110       *s=tolower(*s);
111       s++;
112     }
113   }
114 
115   GWEN_NEW_OBJECT(AB_BANKING, ab);
116   GWEN_INHERIT_INIT(AB_BANKING, ab);
117   ab->appEscName=strdup(GWEN_Buffer_GetStart(nbuf));
118   ab->appName=strdup(appName);
119   ab->cryptTokenList=GWEN_Crypt_Token_List2_new();
120   ab->dbRuntimeConfig=GWEN_DB_Group_new("runtimeConfig");
121 
122   GWEN_Buffer_free(nbuf);
123 
124   {
125     int rv;
126 
127     rv=AB_Banking_CopyOldSettingsFolderIfNeeded(ab);
128     if (rv<0) {
129       DBG_ERROR(AQBANKING_LOGDOMAIN, "Could not copy old settings folder (%d), ignoring", rv);
130     }
131   }
132 
133   AB_Banking__GetConfigManager(ab, dname);
134 
135   ab->appExtensions=extensions;
136 
137   if (getcwd(buffer, sizeof(buffer)-1)==0) {
138     DBG_ERROR(AQBANKING_LOGDOMAIN,
139               "getcwd: %s", strerror(errno));
140   }
141   else {
142     struct stat st;
143 
144     if (stat(buffer, &st)) {
145       DBG_ERROR(AQBANKING_LOGDOMAIN,
146                 "stat(%s): %s", buffer, strerror(errno));
147     }
148     else {
149       ab->startFolder=strdup(buffer);
150     }
151   }
152 
153   return ab;
154 }
155 
156 
157 
AB_Banking_free(AB_BANKING * ab)158 void AB_Banking_free(AB_BANKING *ab)
159 {
160   if (ab) {
161     DBG_INFO(AQBANKING_LOGDOMAIN, "Destroying AB_BANKING");
162 
163     GWEN_INHERIT_FINI(AB_BANKING, ab);
164 
165     GWEN_DB_Group_free(ab->dbRuntimeConfig);
166     AB_Banking_ClearCryptTokenList(ab);
167     GWEN_Crypt_Token_List2_free(ab->cryptTokenList);
168     GWEN_ConfigMgr_free(ab->configMgr);
169     free(ab->startFolder);
170     free(ab->appName);
171     free(ab->appEscName);
172     free(ab->dataDir);
173     GWEN_FREE_OBJECT(ab);
174     GWEN_Fini();
175   }
176 }
177 
178 
179 
AB_Banking_GetNamedUniqueId(AB_BANKING * ab,const char * idName,int startAtStdUniqueId)180 int AB_Banking_GetNamedUniqueId(AB_BANKING *ab, const char *idName, int startAtStdUniqueId)
181 {
182   int rv;
183   int uid=0;
184   GWEN_DB_NODE *dbConfig=NULL;
185 
186   rv=GWEN_ConfigMgr_LockGroup(ab->configMgr,
187                               AB_CFG_GROUP_MAIN,
188                               "uniqueId");
189   if (rv<0) {
190     DBG_ERROR(AQBANKING_LOGDOMAIN, "Unable to lock main config (%d)", rv);
191     return rv;
192   }
193 
194   rv=GWEN_ConfigMgr_GetGroup(ab->configMgr,
195                              AB_CFG_GROUP_MAIN,
196                              "uniqueId",
197                              &dbConfig);
198   if (rv<0) {
199     DBG_ERROR(AQBANKING_LOGDOMAIN, "Unable to read main config (%d)", rv);
200     return rv;
201   }
202 
203   if (idName && *idName) {
204     GWEN_BUFFER *tbuf;
205 
206     tbuf=GWEN_Buffer_new(0, 256, 0, 1);
207     GWEN_Buffer_AppendString(tbuf, "uniqueid-");
208     GWEN_Buffer_AppendString(tbuf, idName);
209 
210     uid=GWEN_DB_GetIntValue(dbConfig, GWEN_Buffer_GetStart(tbuf), 0, 0);
211     if (uid==0 && startAtStdUniqueId) {
212       /* not set yet, start with a unique id from standard source */
213       uid=GWEN_DB_GetIntValue(dbConfig, "uniqueId", 0, 0);
214       uid++;
215       GWEN_DB_SetIntValue(dbConfig, GWEN_DB_FLAGS_OVERWRITE_VARS, "uniqueId", uid);
216       GWEN_DB_SetIntValue(dbConfig, GWEN_DB_FLAGS_OVERWRITE_VARS, GWEN_Buffer_GetStart(tbuf), uid);
217     }
218     else {
219       uid++;
220       GWEN_DB_SetIntValue(dbConfig, GWEN_DB_FLAGS_OVERWRITE_VARS, GWEN_Buffer_GetStart(tbuf), uid);
221     }
222     GWEN_Buffer_free(tbuf);
223   }
224   else {
225     uid=GWEN_DB_GetIntValue(dbConfig, "uniqueId", 0, 0);
226     uid++;
227     GWEN_DB_SetIntValue(dbConfig, GWEN_DB_FLAGS_OVERWRITE_VARS, "uniqueId", uid);
228   }
229 
230   rv=GWEN_ConfigMgr_SetGroup(ab->configMgr,
231                              AB_CFG_GROUP_MAIN,
232                              "uniqueId",
233                              dbConfig);
234   if (rv<0) {
235     DBG_ERROR(AQBANKING_LOGDOMAIN, "Unable to write main config (%d)", rv);
236     GWEN_ConfigMgr_UnlockGroup(ab->configMgr,
237                                AB_CFG_GROUP_MAIN,
238                                "uniqueId");
239     GWEN_DB_Group_free(dbConfig);
240     return rv;
241   }
242   GWEN_DB_Group_free(dbConfig);
243 
244   /* unlock */
245   rv=GWEN_ConfigMgr_UnlockGroup(ab->configMgr,
246                                 AB_CFG_GROUP_MAIN,
247                                 "uniqueId");
248   if (rv<0) {
249     DBG_ERROR(AQBANKING_LOGDOMAIN, "Unable to unlock main config (%d)", rv);
250     return rv;
251   }
252 
253   return uid;
254 }
255 
256 
257 
AB_Banking_GetConfigMgr(AB_BANKING * ab)258 GWEN_CONFIGMGR *AB_Banking_GetConfigMgr(AB_BANKING *ab)
259 {
260   assert(ab);
261   return ab->configMgr;
262 }
263 
264 
265 
AB_Banking_GetAppName(const AB_BANKING * ab)266 const char *AB_Banking_GetAppName(const AB_BANKING *ab)
267 {
268   assert(ab);
269   return ab->appName;
270 }
271 
272 
273 
AB_Banking_GetEscapedAppName(const AB_BANKING * ab)274 const char *AB_Banking_GetEscapedAppName(const AB_BANKING *ab)
275 {
276   assert(ab);
277   return ab->appEscName;
278 }
279 
280 
281 
AB_Banking_GetVersion(int * major,int * minor,int * patchlevel,int * build)282 void AB_Banking_GetVersion(int *major,
283                            int *minor,
284                            int *patchlevel,
285                            int *build)
286 {
287   if (major)
288     *major=AQBANKING_VERSION_MAJOR;
289   if (minor)
290     *minor=AQBANKING_VERSION_MINOR;
291   if (patchlevel)
292     *patchlevel=AQBANKING_VERSION_PATCHLEVEL;
293   if (build)
294     *build=AQBANKING_VERSION_BUILD;
295 }
296 
297 
298 
AB_Banking_RuntimeConfig_SetCharValue(AB_BANKING * ab,const char * varName,const char * value)299 void AB_Banking_RuntimeConfig_SetCharValue(AB_BANKING *ab, const char *varName, const char *value)
300 {
301   assert(ab);
302   assert(ab->dbRuntimeConfig);
303   GWEN_DB_SetCharValue(ab->dbRuntimeConfig, GWEN_DB_FLAGS_OVERWRITE_VARS, varName, value);
304 }
305 
306 
307 
AB_Banking_RuntimeConfig_GetCharValue(const AB_BANKING * ab,const char * varName,const char * defaultValue)308 const char *AB_Banking_RuntimeConfig_GetCharValue(const AB_BANKING *ab, const char *varName, const char *defaultValue)
309 {
310   assert(ab);
311   assert(ab->dbRuntimeConfig);
312   return GWEN_DB_GetCharValue(ab->dbRuntimeConfig, varName, 0, defaultValue);
313 }
314 
315 
316 
AB_Banking_RuntimeConfig_SetIntValue(AB_BANKING * ab,const char * varName,int value)317 void AB_Banking_RuntimeConfig_SetIntValue(AB_BANKING *ab, const char *varName, int value)
318 {
319   assert(ab);
320   assert(ab->dbRuntimeConfig);
321   GWEN_DB_SetIntValue(ab->dbRuntimeConfig, GWEN_DB_FLAGS_OVERWRITE_VARS, varName, value);
322 }
323 
324 
325 
AB_Banking_RuntimeConfig_GetIntValue(const AB_BANKING * ab,const char * varName,int defaultValue)326 int AB_Banking_RuntimeConfig_GetIntValue(const AB_BANKING *ab, const char *varName, int defaultValue)
327 {
328   assert(ab);
329   assert(ab->dbRuntimeConfig);
330   return GWEN_DB_GetIntValue(ab->dbRuntimeConfig, varName, 0, defaultValue);
331 }
332 
333 
334 
335 
AB_Banking_GetNewUserDialog(AB_BANKING * ab,const char * backend,int mode)336 GWEN_DIALOG *AB_Banking_GetNewUserDialog(AB_BANKING *ab,
337                                          const char *backend,
338                                          int mode)
339 {
340   /*
341     AB_PROVIDER *pro;
342     GWEN_DIALOG *dlg;
343 
344     assert(ab);
345     pro=AB_Banking_GetProvider(ab, backend);
346     if (!pro) {
347       DBG_ERROR(AQBANKING_LOGDOMAIN, "Backend \"%s\" not found", backend);
348       return NULL;
349     }
350 
351     dlg=AB_Provider_GetNewUserDialog(pro, mode);
352     if (dlg==NULL) {
353       DBG_INFO(AQBANKING_LOGDOMAIN,
354          "Provider did not return a NewUser dialog (backend=%s, mode=%d)",
355          backend, mode);
356       return NULL;
357     }
358 
359     return dlg;
360   */
361   return NULL;
362 }
363 
364 
365 
366 
367 
368 
369