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 
17 #include <gwenhywfar/text.h>
18 
19 
20 
AH_Control_UnblockPin(AB_PROVIDER * pro,GWEN_DB_NODE * dbArgs,int argc,char ** argv)21 int AH_Control_UnblockPin(AB_PROVIDER *pro,
22                           GWEN_DB_NODE *dbArgs,
23                           int argc,
24                           char **argv)
25 {
26   GWEN_DB_NODE *db;
27   uint32_t uid;
28   AB_USER *u=NULL;
29   int rv;
30   const GWEN_ARGS args[]= {
31     {
32       GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
33       GWEN_ArgsType_Int,            /* type */
34       "userId",                     /* name */
35       0,                            /* minnum */
36       1,                            /* maxnum */
37       "u",                          /* short option */
38       "user",                       /* long option */
39       "Specify the unique user id",    /* short description */
40       "Specify the unique user id"     /* long description */
41     },
42     {
43       GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
44       GWEN_ArgsType_Int,            /* type */
45       "help",                       /* name */
46       0,                            /* minnum */
47       0,                            /* maxnum */
48       "h",                          /* short option */
49       "help",                       /* long option */
50       "Show this help screen",      /* short description */
51       "Show this help screen"       /* long description */
52     }
53   };
54 
55   db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
56   rv=GWEN_Args_Check(argc, argv, 1,
57                      0 /*GWEN_ARGS_MODE_ALLOW_FREEPARAM*/,
58                      args,
59                      db);
60   if (rv==GWEN_ARGS_RESULT_ERROR) {
61     fprintf(stderr, "ERROR: Could not parse arguments\n");
62     return 1;
63   }
64   else if (rv==GWEN_ARGS_RESULT_HELP) {
65     GWEN_BUFFER *ubuf;
66 
67     ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
68     if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
69       fprintf(stderr, "ERROR: Could not create help string\n");
70       return 1;
71     }
72     fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(ubuf));
73     GWEN_Buffer_free(ubuf);
74     return 0;
75   }
76 
77 
78   /* doit */
79   uid=(uint32_t) GWEN_DB_GetIntValue(db, "userId", 0, 0);
80   if (uid==0) {
81     fprintf(stderr, "ERROR: Invalid or missing unique user id\n");
82     return 1;
83   }
84 
85   rv=AB_Provider_HasUser(pro, uid);
86   if (rv<0) {
87     fprintf(stderr, "ERROR: User with id %lu not found\n", (unsigned long int) uid);
88     return 2;
89   }
90 
91   rv=AB_Provider_GetUser(pro, uid, 1, 1, &u);
92   if (rv<0) {
93     fprintf(stderr, "ERROR: User with id %lu not found\n", (unsigned long int) uid);
94     return 2;
95   }
96   else {
97     AB_IMEXPORTER_CONTEXT *ctx;
98 
99     ctx=AB_ImExporterContext_new();
100     rv=AH_Provider_UnblockPin(pro, u, ctx, 1, 0, 1);
101     AB_ImExporterContext_free(ctx);
102     if (rv) {
103       DBG_ERROR(0, "Error unblocking pin (%d)", rv);
104       AB_User_free(u);
105       return 3;
106     }
107   }
108   AB_User_free(u);
109 
110   return 0;
111 }
112 
113 
114 
115 
116