gwenhywfar  4.99.8beta
changepin.c
Go to the documentation of this file.
1 /***************************************************************************
2  $RCSfile$
3  -------------------
4  cvs : $Id: create.c 892 2005-11-03 00:20:45Z aquamaniac $
5  begin : Tue May 03 2005
6  copyright : (C) 2005 by Martin Preuss
7  email : martin@libchipcard.de
8 
9  ***************************************************************************
10  * Please see toplevel file COPYING for license details *
11  ***************************************************************************/
12 
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
16 
17 #include "globals.h"
18 
19 #include <gwenhywfar/debug.h>
20 #include <gwenhywfar/ct.h>
21 #include <gwenhywfar/ctplugin.h>
22 #include <gwenhywfar/text.h>
23 
24 
25 
26 
27 int changePin(GWEN_DB_NODE *dbArgs, int argc, char **argv) {
28  GWEN_DB_NODE *db;
29  const char *ttype;
30  const char *tname;
31  GWEN_CRYPT_TOKEN *ct;
32  int rv;
33  int admin;
34  const GWEN_ARGS args[]={
35  {
36  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
37  GWEN_ArgsType_Char, /* type */
38  "tokenType", /* name */
39  1, /* minnum */
40  1, /* maxnum */
41  "t", /* short option */
42  "ttype", /* long option */
43  "Specify the crypt token type", /* short description */
44  "Specify the crypt token type" /* long description */
45  },
46  {
47  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
48  GWEN_ArgsType_Char, /* type */
49  "tokenName", /* name */
50  0, /* minnum */
51  1, /* maxnum */
52  "n", /* short option */
53  "tname", /* long option */
54  "Specify the crypt token name", /* short description */
55  "Specify the crypt token name" /* long description */
56  },
57  {
58  0, /* flags */
60  "admin",
61  0,
62  1,
63  "a",
64  "admin",
65  "Change the admin pin instead of the access pin",
66  "Change the admin pin instead of the access pin"
67  },
68  {
70  GWEN_ArgsType_Int, /* type */
71  "help", /* name */
72  0, /* minnum */
73  0, /* maxnum */
74  "h", /* short option */
75  "help", /* long option */
76  "Show this help screen", /* short description */
77  "Show this help screen" /* long description */
78  }
79  };
80 
81  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
82  rv=GWEN_Args_Check(argc, argv, 1,
84  args,
85  db);
86  if (rv==GWEN_ARGS_RESULT_ERROR) {
87  fprintf(stderr, "ERROR: Could not parse arguments\n");
88  return 1;
89  }
90  else if (rv==GWEN_ARGS_RESULT_HELP) {
91  GWEN_BUFFER *ubuf;
92 
93  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
94  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
95  fprintf(stderr, "ERROR: Could not create help string\n");
96  return 1;
97  }
98  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
99  GWEN_Buffer_free(ubuf);
100  return 0;
101  }
102 
103  ttype=GWEN_DB_GetCharValue(db, "tokenType", 0, 0);
104  assert(ttype);
105 
106  tname=GWEN_DB_GetCharValue(db, "tokenName", 0, 0);
107 
108  admin=GWEN_DB_GetIntValue(db, "admin", 0, 0);
109 
110  /* get crypt token */
111  ct=getCryptToken(ttype, tname);
112  if (ct==0)
113  return 3;
114 
115  if (GWEN_DB_GetIntValue(dbArgs, "forcePin", 0, 0))
117 
118  /* open crypt token for use */
119  rv=GWEN_Crypt_Token_Open(ct, 0, 0);
120  if (rv) {
121  DBG_ERROR(0, "Could not open token");
122  return 3;
123  }
124 
125  /* change pin */
126  rv=GWEN_Crypt_Token_ChangePin(ct, admin, 0);
127  if (rv) {
128  DBG_ERROR(0, "Could not change pin");
129  return 3;
130  }
131 
132  /* close crypt token */
133  rv=GWEN_Crypt_Token_Close(ct, 0, 0);
134  if (rv) {
135  DBG_ERROR(0, "Could not close token");
136  return 3;
137  }
138 
139  fprintf(stderr, "Crypt Token updated.\n");
140 
141  return 0;
142 }
143 
144 
145 
#define GWEN_CRYPT_TOKEN_MODE_FORCE_PIN_ENTRY
Definition: ct.h:59
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:223
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
int GWEN_Crypt_Token_ChangePin(GWEN_CRYPT_TOKEN *ct, int admin, uint32_t gid)
Definition: ct.c:503
GWEN_CRYPT_TOKEN * getCryptToken(const char *ttype, const char *tname)
Definition: gcttool/main.c:69
int GWEN_Crypt_Token_Close(GWEN_CRYPT_TOKEN *ct, int abandon, uint32_t gid)
Definition: ct.c:247
#define GWEN_ARGS_FLAGS_HELP
Definition: src/base/args.h:52
GWEN_BUFFER * GWEN_Buffer_new(char *buffer, uint32_t size, uint32_t used, int take)
Definition: buffer.c:38
#define GWEN_ARGS_RESULT_HELP
Definition: src/base/args.h:58
#define GWEN_ARGS_RESULT_ERROR
Definition: src/base/args.h:57
int GWEN_Args_Usage(const GWEN_ARGS *args, GWEN_BUFFER *ubuf, GWEN_ARGS_OUTTYPE ot)
#define GWEN_ARGS_MODE_ALLOW_FREEPARAM
Definition: src/base/args.h:54
const char * GWEN_DB_GetCharValue(GWEN_DB_NODE *n, const char *path, int idx, const char *defVal)
Definition: db.c:897
GWEN_DB_NODE * GWEN_DB_GetGroup(GWEN_DB_NODE *n, uint32_t flags, const char *path)
Definition: db.c:1260
void GWEN_Buffer_free(GWEN_BUFFER *bf)
Definition: buffer.c:83
struct GWEN_BUFFER GWEN_BUFFER
A dynamically resizeable text buffer.
Definition: buffer.h:41
struct GWEN_CRYPT_TOKEN GWEN_CRYPT_TOKEN
Definition: ct.h:19
#define GWEN_ARGS_FLAGS_LAST
Definition: src/base/args.h:51
int GWEN_Crypt_Token_Open(GWEN_CRYPT_TOKEN *ct, int admin, uint32_t gid)
Definition: ct.c:206
int changePin(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: changepin.c:27
#define DBG_ERROR(dbg_logger, format, args...)
Definition: debug.h:97
int GWEN_Args_Check(int argc, char **argv, int startAt, uint32_t mode, const GWEN_ARGS *args, GWEN_DB_NODE *db)
Definition: src/base/args.c:45
int GWEN_DB_GetIntValue(GWEN_DB_NODE *n, const char *path, int idx, int defVal)
Definition: db.c:1048
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
Definition: src/base/args.h:50
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168
void GWEN_Crypt_Token_AddModes(GWEN_CRYPT_TOKEN *ct, uint32_t f)
Definition: ct.c:188