gwenhywfar  4.99.8beta
gcttool/create.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/base64.h>
21 #include <gwenhywfar/ctplugin.h>
22 
23 
24 
25 
26 int createToken(GWEN_DB_NODE *dbArgs, int argc, char **argv) {
27  GWEN_DB_NODE *db;
28  const char *ttype;
29  const char *tname;
31  GWEN_PLUGIN *pl;
32  GWEN_CRYPT_TOKEN *ct;
33  int rv;
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  {
59  GWEN_ArgsType_Int, /* type */
60  "help", /* name */
61  0, /* minnum */
62  0, /* maxnum */
63  "h", /* short option */
64  "help", /* long option */
65  "Show this help screen", /* short description */
66  "Show this help screen" /* long description */
67  }
68  };
69 
70  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
71  rv=GWEN_Args_Check(argc, argv, 1,
73  args,
74  db);
75  if (rv==GWEN_ARGS_RESULT_ERROR) {
76  fprintf(stderr, "ERROR: Could not parse arguments\n");
77  return 1;
78  }
79  else if (rv==GWEN_ARGS_RESULT_HELP) {
80  GWEN_BUFFER *ubuf;
81 
82  ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
83  if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
84  fprintf(stderr, "ERROR: Could not create help string\n");
85  return 1;
86  }
87  fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
88  GWEN_Buffer_free(ubuf);
89  return 0;
90  }
91 
92  ttype=GWEN_DB_GetCharValue(db, "tokenType", 0, 0);
93  assert(ttype);
94 
95  tname=GWEN_DB_GetCharValue(db, "tokenName", 0, 0);
96 
97  /* get crypt token */
99  if (pm==0) {
100  DBG_ERROR(0, "Plugin manager not found");
101  return 3;
102  }
103 
104  pl=GWEN_PluginManager_GetPlugin(pm, ttype);
105  if (pl==0) {
106  DBG_ERROR(0, "Plugin not found");
107  return 3;
108  }
109  DBG_ERROR(0, "Plugin found");
110 
112  if (ct==0) {
113  DBG_ERROR(0, "Could not create crypt token");
114  return 3;
115  }
116 
117  /* create crypt token */
118  rv=GWEN_Crypt_Token_Create(ct, 0);
119  if (rv) {
120  DBG_ERROR(0, "Could not create token");
121  return 3;
122  }
123 
124  /* close crypt token */
125  rv=GWEN_Crypt_Token_Close(ct, 0, 0);
126  if (rv) {
127  DBG_ERROR(0, "Could not close token");
128  return 3;
129  }
130 
131  return 0;
132 }
133 
134 
135 
struct GWEN_PLUGIN_MANAGER GWEN_PLUGIN_MANAGER
Definition: plugin.h:40
char * GWEN_Buffer_GetStart(const GWEN_BUFFER *bf)
Definition: buffer.c:223
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
struct GWEN_PLUGIN GWEN_PLUGIN
Definition: plugin.h:39
#define GWEN_CRYPT_TOKEN_PLUGIN_TYPENAME
Definition: ctplugin.h:24
int GWEN_Crypt_Token_Close(GWEN_CRYPT_TOKEN *ct, int abandon, uint32_t gid)
Definition: ct.c:247
GWEN_PLUGIN * GWEN_PluginManager_GetPlugin(GWEN_PLUGIN_MANAGER *pm, const char *s)
Definition: plugin.c:501
GWEN_CRYPT_TOKEN * GWEN_Crypt_Token_Plugin_CreateToken(GWEN_PLUGIN *pl, const char *name)
Definition: ctplugin.c:138
#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
GWEN_PLUGIN_MANAGER * GWEN_PluginManager_FindPluginManager(const char *s)
Definition: plugin.c:519
#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
#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_Crypt_Token_Create(GWEN_CRYPT_TOKEN *ct, uint32_t gid)
Definition: ct.c:229
int createToken(GWEN_DB_NODE *dbArgs, int argc, char **argv)
#define GWEN_ARGS_FLAGS_HAS_ARGUMENT
Definition: src/base/args.h:50
#define GWEN_DB_FLAGS_DEFAULT
Definition: db.h:168