gwenhywfar  4.99.8beta
activatekey.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Thu Jun 24 2010
3  copyright : (C) 2010 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 #include "globals.h"
15 
16 #include <gwenhywfar/debug.h>
17 #include <gwenhywfar/ct.h>
18 #include <gwenhywfar/ctplugin.h>
19 #include <gwenhywfar/text.h>
20 
21 
22 
23 
24 
25 
26 int activateKey(GWEN_DB_NODE *dbArgs, int argc, char **argv) {
27  GWEN_DB_NODE *db;
28  const char *ttype;
29  const char *tname;
30  GWEN_CRYPT_TOKEN *ct;
31  unsigned int keyId;
32  int rv;
33  const char *s;
34  const GWEN_ARGS args[]={
35  {
36  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
37  GWEN_ArgsType_Int, /* type */
38  "keyId", /* name */
39  1, /* minnum */
40  1, /* maxnum */
41  "k", /* short option */
42  "key", /* long option */
43  "Key id", /* short description */
44  "Key id" /* long description */
45  },
46  {
47  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
48  GWEN_ArgsType_Char, /* type */
49  "tokenType", /* name */
50  1, /* minnum */
51  1, /* maxnum */
52  "t", /* short option */
53  "ttype", /* long option */
54  "Specify the crypt token type", /* short description */
55  "Specify the crypt token type" /* long description */
56  },
57  {
58  GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
59  GWEN_ArgsType_Char, /* type */
60  "tokenName", /* name */
61  0, /* minnum */
62  1, /* maxnum */
63  "n", /* short option */
64  "tname", /* long option */
65  "Specify the crypt token name", /* short description */
66  "Specify the crypt token name" /* long description */
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  keyId=GWEN_DB_GetIntValue(db, "keyId", 0, 0);
104  if (keyId==0) {
105  DBG_ERROR(0, "Key Id must not be zero");
106  return 1;
107  }
108 
109  s=GWEN_DB_GetCharValue(db, "algo", 0, "rsa");
110  if (!s) {
111  DBG_ERROR(0, "Algo id missing");
112  return 1;
113  }
114  ttype=GWEN_DB_GetCharValue(db, "tokenType", 0, 0);
115  assert(ttype);
116 
117  tname=GWEN_DB_GetCharValue(db, "tokenName", 0, 0);
118 
119  /* get crypt token */
120  ct=getCryptToken(ttype, tname);
121  if (ct==0)
122  return 3;
123 
124  if (GWEN_DB_GetIntValue(dbArgs, "forcePin", 0, 0))
126 
127  /* open crypt token for use */
128  rv=GWEN_Crypt_Token_Open(ct, 1, 0);
129  if (rv) {
130  DBG_ERROR(0, "Could not open token");
131  return 3;
132  }
133  else {
134  /* activate key */
135  rv=GWEN_Crypt_Token_ActivateKey(ct, keyId, 0);
136  if (rv) {
138  "Error activating key (%d)", rv);
139  return 3;
140  }
141  }
142 
143  /* close crypt token */
144  rv=GWEN_Crypt_Token_Close(ct, 0, 0);
145  if (rv) {
146  DBG_ERROR(0, "Could not close token");
147  return 3;
148  }
149 
150  fprintf(stderr, "Key %d successfully activated.\n", keyId);
151 
152  return 0;
153 }
154 
155 
156 
#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
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_LOGDOMAIN
Definition: logger.h:35
#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 activateKey(GWEN_DB_NODE *dbArgs, int argc, char **argv)
Definition: activatekey.c:26
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
#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
int GWEN_Crypt_Token_ActivateKey(GWEN_CRYPT_TOKEN *ct, uint32_t id, uint32_t gid)
Definition: ct.c:518
#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