1 /***************************************************************************
2  begin       : Tue May 03 2005
3  copyright   : (C) 2018 by Martin Preuss
4  email       : martin@libchipcard.de
5 
6  ***************************************************************************
7  *          Please see toplevel file COPYING for license details           *
8  ***************************************************************************/
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 
15 #include "globals_l.h"
16 #include "aqhbci/banking/user.h"
17 
18 #include <gwenhywfar/text.h>
19 #include <gwenhywfar/syncio_file.h>
20 
21 #include <errno.h>
22 
23 
AH_Control_MkPinList(AB_PROVIDER * pro,GWEN_DB_NODE * dbArgs,int argc,char ** argv)24 int AH_Control_MkPinList(AB_PROVIDER *pro,
25                          GWEN_DB_NODE *dbArgs,
26                          int argc,
27                          char **argv)
28 {
29   GWEN_DB_NODE *db;
30   GWEN_SYNCIO *sio;
31   AB_USER_LIST *ul;
32   AB_USER *u;
33   int rv;
34   const char *outFile;
35   const GWEN_ARGS args[]= {
36     {
37       GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
38       GWEN_ArgsType_Char,           /* type */
39       "outFile",                    /* name */
40       0,                            /* minnum */
41       1,                            /* maxnum */
42       "o",                          /* short option */
43       "outfile",                    /* long option */
44       "Specify the name of the output file", /* short description */
45       "Specify the name of the output file"  /* long description */
46     },
47     {
48       GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
49       GWEN_ArgsType_Int,            /* type */
50       "help",                       /* name */
51       0,                            /* minnum */
52       0,                            /* maxnum */
53       "h",                          /* short option */
54       "help",                       /* long option */
55       "Show this help screen",      /* short description */
56       "Show this help screen"       /* long description */
57     }
58   };
59 
60   db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
61   rv=GWEN_Args_Check(argc, argv, 1,
62                      0 /*GWEN_ARGS_MODE_ALLOW_FREEPARAM*/,
63                      args,
64                      db);
65   if (rv==GWEN_ARGS_RESULT_ERROR) {
66     fprintf(stderr, "ERROR: Could not parse arguments\n");
67     return 1;
68   }
69   else if (rv==GWEN_ARGS_RESULT_HELP) {
70     GWEN_BUFFER *ubuf;
71 
72     ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
73     if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
74       fprintf(stderr, "ERROR: Could not create help string\n");
75       return 1;
76     }
77     fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(ubuf));
78     GWEN_Buffer_free(ubuf);
79     return 0;
80   }
81 
82   outFile=GWEN_DB_GetCharValue(db, "outfile", 0, 0);
83 
84   if (outFile==0) {
85     sio=GWEN_SyncIo_File_fromStdout();
86     GWEN_SyncIo_AddFlags(sio, GWEN_SYNCIO_FLAGS_DONTCLOSE);
87   }
88   else {
89     sio=GWEN_SyncIo_File_new(outFile, GWEN_SyncIo_File_CreationMode_CreateAlways);
90     GWEN_SyncIo_AddFlags(sio,
91                          GWEN_SYNCIO_FILE_FLAGS_READ |
92                          GWEN_SYNCIO_FILE_FLAGS_WRITE |
93                          GWEN_SYNCIO_FILE_FLAGS_UREAD |
94                          GWEN_SYNCIO_FILE_FLAGS_UWRITE |
95                          GWEN_SYNCIO_FILE_FLAGS_GREAD |
96                          GWEN_SYNCIO_FILE_FLAGS_GWRITE);
97     rv=GWEN_SyncIo_Connect(sio);
98     if (rv<0) {
99       DBG_ERROR(0, "Error opening output file: %s",
100                 strerror(errno));
101       return 4;
102     }
103   }
104 
105   GWEN_SyncIo_WriteLine(sio, "# This is a PIN file to be used with AqBanking");
106   GWEN_SyncIo_WriteLine(sio, "# Please insert the PINs/passwords for the users below");
107 
108   ul=AB_User_List_new();
109   rv=AB_Provider_ReadUsers(pro, ul);
110   if (rv<0) {
111     DBG_ERROR_ERR(0, rv);
112     AB_User_List_free(ul);
113     GWEN_SyncIo_Disconnect(sio);
114     GWEN_SyncIo_free(sio);
115     return 3;
116   }
117 
118   u=AB_User_List_First(ul);
119   while (u) {
120     const char *s;
121     GWEN_BUFFER *nbuf;
122     int rv;
123 
124     GWEN_SyncIo_WriteLine(sio, "");
125     GWEN_SyncIo_WriteString(sio, "# User \"");
126     s=AB_User_GetUserId(u);
127     assert(s);
128     GWEN_SyncIo_WriteString(sio, s);
129     GWEN_SyncIo_WriteString(sio, "\" at \"");
130     s=AB_User_GetBankCode(u);
131     GWEN_SyncIo_WriteString(sio, s);
132     GWEN_SyncIo_WriteLine(sio, "\"");
133 
134     nbuf=GWEN_Buffer_new(0, 256, 0, 1);
135     if (AH_User_GetCryptMode(u)==AH_CryptMode_Pintan)
136       rv=AH_User_MkPinName(u, nbuf);
137     else
138       rv=AH_User_MkPasswdName(u, nbuf);
139 
140     if (rv==0) {
141       GWEN_BUFFER *obuf;
142 
143       obuf=GWEN_Buffer_new(0, 256, 0, 1);
144       if (GWEN_Text_EscapeToBufferTolerant(GWEN_Buffer_GetStart(nbuf), obuf)) {
145         DBG_ERROR(0, "Error escaping name to buffer");
146         GWEN_SyncIo_Disconnect(sio);
147         GWEN_SyncIo_free(sio);
148         return 3;
149       }
150       GWEN_SyncIo_WriteString(sio, GWEN_Buffer_GetStart(obuf));
151       GWEN_SyncIo_WriteLine(sio, " = \"\"");
152 
153       GWEN_Buffer_free(obuf);
154     }
155     GWEN_Buffer_free(nbuf);
156 
157     u=AB_User_List_Next(u);
158   }
159   AB_User_List_free(ul);
160 
161 
162   rv=GWEN_SyncIo_Disconnect(sio);
163   if (rv<0) {
164     DBG_ERROR_ERR(0, rv);
165     GWEN_SyncIo_free(sio);
166     return 4;
167   }
168   GWEN_SyncIo_free(sio);
169 
170 
171   return 0;
172 }
173 
174 
175 
176 
177