File: yubiserver-admin.c

package info (click to toggle)
yubiserver 0.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 552 kB
  • sloc: ansic: 1,801; sh: 84; makefile: 54
file content (727 lines) | stat: -rw-r--r-- 20,834 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/* Copyright (c) 2011 Nanakos Chrysostomos <nanakos@wired-net.gr>
   Simple and lightweight Yubikey OTP-OATH/HOTP Validation Server

   yubiserver-admin is placed under the GNU General Public License, version 2 or later.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <gcrypt.h>
#include <sqlite3.h>
#include <time.h>
#include <sys/timeb.h>
#include <getopt.h>
#include <math.h>

#include <mhash.h>

#include "yubiserver.h"

int yubikey_table = 0;
int oath_table = 0;
int api_table = 0;
int ext_db = 0;
int Argc;
char **Argv;
#define YUBIKEY	0
#define OATH	1
#define API	2

static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
        'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
        'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
        'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
        'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
        'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
        'w', 'x', 'y', 'z', '0', '1', '2', '3',
        '4', '5', '6', '7', '8', '9', '+', '/'};

static int mod_table[] = {0, 2, 1};

char *base64_encode(const unsigned char *data,size_t input_length,size_t output_length) {
        int i,j;
        output_length = (size_t) (4.0 * ceil((double) input_length / 3.0));
        char *encoded_data = malloc(output_length);
        if (encoded_data == NULL) return NULL;
        for (i = 0, j = 0; i < input_length;) {
                uint32_t octet_a = i < input_length ? data[i++] : 0;
                uint32_t octet_b = i < input_length ? data[i++] : 0;
                uint32_t octet_c = i < input_length ? data[i++] : 0;
                uint32_t triple = (octet_a << 0x10) + (octet_b << 0x08) + octet_c;

                encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
                encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
                encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
                encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
        }

        for (i = 0; i < mod_table[input_length % 3]; i++)
                encoded_data[output_length - 1 - i] = '=';
        return encoded_data;
}

void print_table(char *table,int id)
{
	sqlite3 *handle;
	sqlite3_stmt *stmt;
	int retval,rows=0;
	char *query = NULL;

	if(id==YUBIKEY || id==OATH)
	{
		query = (char *)malloc((size_t)100);
		sprintf(query,"SELECT nickname,publicname,active FROM %s",table);
	}
	else if(id==API)
	{
		query = (char *)malloc((size_t)100);
		sprintf(query,"SELECT nickname,id FROM %s",table);
	}
	
	retval = sqlite3_open(sqlite3_dbpath,&handle);

	if(retval)
	{
		fprintf(stderr,"Database connection failed");
		exit(2);
	}

	if(query != NULL) retval = sqlite3_prepare_v2(handle,query,-1,&stmt,0);

	if(retval)
	{
		fprintf(stderr,"Selecting data from DB failed");
		exit(2);
	}

	while(1)
	{
		retval = sqlite3_step(stmt);

		if(retval==SQLITE_ROW)
		{
			if(id==YUBIKEY || id==OATH)
			{
				if(!rows) printf("%-20s %-20s %-20s\n","[Username]","[Public Token ID]","Active");
				printf("%-20s %-20s %-20s\n",sqlite3_column_text(stmt,0),sqlite3_column_text(stmt,1),sqlite3_column_text(stmt,2));
				rows++;
			}else if(id==API)
			{
				if(!rows) printf("%-20s %-20s\n","[Username]","[API ID]");
				printf("%-20s %-20s\n",sqlite3_column_text(stmt,0),sqlite3_column_text(stmt,1));
				rows++;
			}
		}
		else if(retval==SQLITE_DONE)
		{
			break;
		}
		else
		{
			fprintf(stderr,"Database error encountered");
			break;
		}
	}
	sqlite3_close(handle);
	free(query);

	if(!rows) printf("No keys in database.\n");
	printf("Total keys in database: %d\n",rows);
	exit(0);
}

void db_ops(char *table,char *user,int operation,int id)
{
	sqlite3 *handle;
        sqlite3_stmt *stmt;
        int retval;
        char *query = NULL;
	time_t t;
	struct tm *tmp;
	struct timeb tp;
	char *cdatetime = NULL;
	char *cdatetime_tmp = NULL;

	if(id == YUBIKEY || id == OATH)
	{
		if(operation == ENABLE_USER || operation == DISABLE_USER || operation == DELETE_USER)
		{
			query = (char *)malloc((size_t)100);
	                sprintf(query,"SELECT * FROM %s WHERE nickname='%s'",table,user);
			retval = sqlite3_open(sqlite3_dbpath,&handle);

			if(retval)
			{
				fprintf(stderr,"Database connection failed");
				exit(2);
			}
			
			if(query != NULL) retval = sqlite3_prepare_v2(handle,query,-1,&stmt,0);
			
			if(retval)
			{
				fprintf(stderr,"Selecting data from DB failed");
				goto out;
			}
			retval = sqlite3_step(stmt);
				
			if(retval==SQLITE_ROW)
			{
				fprintf(stdout,"User '%s' found\n",user);
				if(operation == ENABLE_USER)
				{
					sprintf(query,"SELECT * FROM %s WHERE nickname = '%s'  AND active = '1'",table,user);
					retval = sqlite3_prepare_v2(handle,query,-1,&stmt,0);
				}
				else if(operation == DISABLE_USER)
				{
					sprintf(query,"SELECT * FROM %s WHERE nickname = '%s'  AND active = '0'",table,user);
					retval = sqlite3_prepare_v2(handle,query,-1,&stmt,0);
				}
				else if(operation == DELETE_USER)
				{
					sprintf(query,"DELETE FROM %s WHERE nickname = '%s'",table,user);
					retval = sqlite3_exec(handle,query,0,0,0);
					if(retval)
				       	{
						fprintf(stderr,"Deleting user '%s' failed",user);
						goto out;
					}
					fprintf(stdout,"User '%s' deleted\n",user);
					goto out;
				}

				if(retval)
				{
					fprintf(stderr,"Selecting data from DB failed");
					goto out;
				}
				retval = sqlite3_step(stmt);
				if(retval==SQLITE_ROW)
				{
					if(operation == ENABLE_USER)
						fprintf(stdout,"User '%s' is already enabled\n",user);
					else if(operation == DISABLE_USER)
						fprintf(stdout,"User '%s' is already disabled\n",user);
				}
				else if(retval == SQLITE_DONE)
				{
					if(operation == ENABLE_USER)
					{
						fprintf(stdout,"Enabling user '%s'\n",user);
						sprintf(query,"UPDATE %s SET active = '1' WHERE nickname = '%s'",table,user);
						retval = sqlite3_exec(handle,query,0,0,0);
						if(retval)
						{
							fprintf(stderr,"Selecting data from DB failed");
							goto out;
						}
						fprintf(stdout,"User '%s' enabled\n",user);

					}
					else if(operation == DISABLE_USER)
					{
						fprintf(stdout,"Disabling user '%s'\n",user);
						sprintf(query,"UPDATE %s SET active = '0' WHERE nickname = '%s'",table,user);
						retval = sqlite3_exec(handle,query,0,0,0);
						if(retval)
						{
							fprintf(stderr,"Selecting data from DB failed");
							exit(2);
						}
						fprintf(stdout,"User '%s' disabled\n",user);
					}
				}
				else
				{
					fprintf(stderr,"Database error encountered");
					goto out;
				}
			}
			else if(retval==SQLITE_DONE)
			{
				fprintf(stdout,"User '%s' not found\n",user);
			}
			else
			{
				fprintf(stderr,"Database error encountered");
				exit(0);
			}
		}else if(operation == ADD_USER)
		{
			if(id==YUBIKEY)
			{
				if(!ext_db)
				{
					if((strlen(user) > 16 || strlen(Argv[4])!=12 || strlen(Argv[5]) != 12 || strlen(Argv[6]) != 32))
					{
						fprintf(stderr,"Parameters are not correct. Please try again.\n");
						fprintf(stderr,"Public token ID must be 12 characters long.\n");
						fprintf(stderr,"Secret token ID must be 12 characters long.\n");
						fprintf(stderr,"AES key must be 32 characters long.\n");
						exit(2);
					}
				}else if(ext_db)
				{
					if((strlen(user) > 16 || strlen(Argv[6])!=12 || strlen(Argv[7]) != 12 || strlen(Argv[8]) != 32))
					{
						fprintf(stderr,"Parameters are not correct. Please try again.\n");
						fprintf(stderr,"Public token ID must be 12 characters long.\n");
						fprintf(stderr,"Secret token ID must be 12 characters long.\n");
						fprintf(stderr,"AES key must be 32 characters long.\n");
						exit(2);
					}
				}
				query = (char *)malloc((size_t)200);
				if(!ext_db)
				{
					sprintf(query,"SELECT * FROM %s WHERE nickname='%s' OR publicname='%s'",table,user,Argv[4]);
				}else if(ext_db)
				{
					sprintf(query,"SELECT * FROM %s WHERE nickname='%s' OR publicname='%s'",table,user,Argv[6]);
				}
				retval = sqlite3_open(sqlite3_dbpath,&handle);
				if(retval)
				{
					fprintf(stderr,"Database connection failed");
					exit(2);
				}
			
				if(query != NULL) retval = sqlite3_prepare_v2(handle,query,-1,&stmt,0);
			
				if(retval)
				{
					fprintf(stderr,"Selecting data from DB failed");
					goto out;
				}
				retval = sqlite3_step(stmt);
				
				if(retval==SQLITE_ROW)
				{
					fprintf(stdout,"Username or key already exist into database. Delete it before adding the same key.\n");
					goto out;
				}else if(retval == SQLITE_DONE)
				{
					fprintf(stdout,"Username/key not found.Adding it into database.\n");
					t = time(NULL);
					tmp = localtime(&t);
					ftime(&tp);
					cdatetime = (char *)malloc((char)25);
					cdatetime_tmp = (char *)malloc((char)20);
					strftime(cdatetime_tmp,20,"%Y-%m-%dT%H:%M:%S",tmp);
					sprintf(cdatetime,"%s.%.3dZ",cdatetime_tmp,tp.millitm);
					if(!ext_db)
					{
						sprintf(query,"INSERT INTO %s VALUES('%s','%s','%s','%s','%s',1,1,1)",table,user,Argv[4],cdatetime,Argv[5],Argv[6]);
					}else if(ext_db)
					{
						sprintf(query,"INSERT INTO %s VALUES('%s','%s','%s','%s','%s',1,1,1)",table,user,Argv[6],cdatetime,Argv[7],Argv[8]);
					}
					retval = sqlite3_exec(handle,query,0,0,0);
					free(cdatetime); free(cdatetime_tmp);
					if(retval)
					{
						fprintf(stderr,"Adding user '%s' failed",user);
						goto out;
					}
					fprintf(stdout,"User '%s' added to database\n",user);
					goto out;
				}

			}else if(id==OATH)
			{
				if(!ext_db)
				{
					if((strlen(user) > 16 || strlen(Argv[4])!=12 || strlen(Argv[5]) != 40))
					{
						fprintf(stderr,"Parameters are not correct. Please try again.\n");
						fprintf(stderr,"Public token ID must be 12 characters long.\n");
						fprintf(stderr,"Secret key must be 40 characters long.\n");
						exit(2);
					}
				}
				else  if(ext_db)
				{
					if((strlen(user) > 16 || strlen(Argv[6])!=12 || strlen(Argv[7]) != 40))
					{
						fprintf(stderr,"Parameters are not correct. Please try again.\n");
						fprintf(stderr,"Public token ID must be 12 characters long.\n");
						fprintf(stderr,"Secret key must be 40 characters long.\n");
						exit(2);
					}
				}
				query = (char *)calloc(1,(size_t)150);
				if(!ext_db)
				{
					sprintf(query,"SELECT * FROM %s WHERE nickname='%s' OR publicname='%s'",table,user,Argv[4]);
				}else if(ext_db)
				{
					sprintf(query,"SELECT * FROM %s WHERE nickname='%s' OR publicname='%s'",table,user,Argv[6]);
				}
				retval = sqlite3_open(sqlite3_dbpath,&handle);
				if(retval)
				{
					fprintf(stderr,"Database connection failed");
					exit(2);
				}
			
				if(query != NULL) retval = sqlite3_prepare_v2(handle,query,-1,&stmt,0);
			
				if(retval)
				{
					fprintf(stderr,"Selecting data from DB failed");
					goto out;
				}
				retval = sqlite3_step(stmt);
				
				if(retval==SQLITE_ROW)
				{
					fprintf(stdout,"Username or key already exist into database. Delete it before adding the same key.\n");
					goto out;
				}else if(retval == SQLITE_DONE)
				{
					fprintf(stdout,"Username/key not found.Adding it into database.\n");
					t = time(NULL);
					tmp = localtime(&t);
					cdatetime = (char *)malloc((char)21);
					strftime(cdatetime,21,"%Y-%m-%dT%H:%M:%SZ",tmp);
					if(!ext_db)
					{
						sprintf(query,"INSERT INTO %s VALUES('%s','%s','%s','%s',1,1)",table,user,Argv[4],cdatetime,Argv[5]);
					}else if(ext_db)
					{
						sprintf(query,"INSERT INTO %s VALUES('%s','%s','%s','%s',1,1)",table,user,Argv[6],cdatetime,Argv[7]);
					}	
					retval = sqlite3_exec(handle,query,0,0,0);
					free(cdatetime);
					if(retval)
					{
						fprintf(stderr,"Adding user '%s' failed\n",user);
						goto out;
					}
					fprintf(stdout,"User '%s' added to database\n",user);
					goto out;
				}
			}
		}
	}else if(id == API)
	{
		if(operation == ENABLE_USER || operation == DISABLE_USER)
		{
			fprintf(stderr,"You cannot enable/disable '%s' user in API's table\n",user);
			exit(0);
		}
		else if(operation == DELETE_USER)
		{
			query = (char *)malloc((size_t)100);
			sprintf(query,"SELECT * FROM %s WHERE nickname='%s'",table,user);
			retval = sqlite3_open(sqlite3_dbpath,&handle);
			if(retval)
			{
				fprintf(stderr,"Database connection failed");
				exit(2);
			}
			
			if(query != NULL) retval = sqlite3_prepare_v2(handle,query,-1,&stmt,0);
			
			if(retval)
			{
				fprintf(stderr,"Selecting data from DB failed");
				goto out;
			}
			retval = sqlite3_step(stmt);
			
			if(retval==SQLITE_ROW)
			{
				fprintf(stdout,"User '%s' found\n",user);
			       	sprintf(query,"DELETE FROM %s WHERE nickname = '%s'",table,user);
				
			       	retval = sqlite3_exec(handle,query,0,0,0);
		       		if(retval)
				{
					 fprintf(stderr,"Deleting user '%s' failed",user);
					 goto out;
				}
				fprintf(stdout,"User '%s' deleted\n",user);
				goto out;
			}else if(retval==SQLITE_DONE)
			{
				fprintf(stdout,"User '%s' not found\n",user);
			}
			else
			{
				fprintf(stderr,"Database error encountered");
				exit(0);
			}
		}else if(operation == ADD_USER)
		{
			query = (char *)malloc((size_t)100);
			sprintf(query,"SELECT * FROM %s WHERE nickname='%s'",table,user);
			int api_id = 0;

			retval = sqlite3_open(sqlite3_dbpath,&handle);

			if(retval)
			{
				fprintf(stderr,"Database connection failed");
				exit(2);
			}

			if(query != NULL) retval = sqlite3_prepare_v2(handle,query,-1,&stmt,0);
			
			if(retval)
			{
				fprintf(stderr,"Selecting data from DB failed");
				goto out;
			}
			retval = sqlite3_step(stmt);
			
			if(retval==SQLITE_ROW)
			{
				fprintf(stdout,"API Key for username '%s' is already present. Remove it or choose another one.\n",user);
				goto out;
			}else if(retval==SQLITE_DONE)
			{
				fprintf(stdout,"User '%s' not found.Adding API Key\n",user);
				sprintf(query,"SELECT id FROM apikeys ORDER BY id DESC LIMIT 1");
				if(query != NULL) retval = sqlite3_prepare_v2(handle,query,-1,&stmt,0);
				if(retval)
				{
					fprintf(stderr,"Selecting data from DB failed");
					goto out;
				}
				retval = sqlite3_step(stmt);

				if(retval == SQLITE_ROW)
				{
					api_id = atoi((char *)sqlite3_column_text(stmt,0))+1;

				}else if(retval==SQLITE_DONE)
				{
					api_id = 1;
				}
				else{
					fprintf(stderr,"Database error encountered");
	                                exit(0);
				}
				if(!ext_db)
				{
					if(Argc!=5)
					{
						fprintf(stderr,"No API Key given.\n");
						goto out;
					}
				}else if(ext_db)
				{
					if(Argc!=7)
					{
						fprintf(stderr,"No API Key given.\n");
						goto out;
					}
				}
				if(!ext_db)
				{
					if(strlen(Argv[4])>20 || strlen(Argv[4])<20)
					{
						fprintf(stderr,"Invalid API Key. It must be 20 characters long.\n");
						goto out;
					}
					sprintf(query,"INSERT INTO apikeys VALUES ('%s','%s','%d')",user,Argv[4],api_id);
				}
				else if(ext_db)
				{
					if(strlen(Argv[6])>20 || strlen(Argv[6])<20)
					{
						fprintf(stderr,"Invalid API Key. It must be 20 characters long.\n");
						goto out;
					}
					sprintf(query,"INSERT INTO apikeys VALUES ('%s','%s','%d')",user,Argv[6],api_id);
				}
				retval = sqlite3_exec(handle,query,0,0,0);
				if(retval)
				{
					fprintf(stderr,"Adding new API KEY for '%s' failed",user);
					goto out;
				}
				if(!ext_db)
				{
					fprintf(stdout,"New API Key for '%s': %s\n",user,base64_encode((unsigned char *)Argv[4],20,20));
				}else if(ext_db)
				{
					fprintf(stdout,"New API Key for '%s': %s\n",user,base64_encode((unsigned char *)Argv[6],20,20));
				}
					fprintf(stdout,"Your API Key ID is: %d\n",api_id);
				goto out;
			}
			else
			{
				fprintf(stderr,"Database error encountered");
				exit(0);
			}
		}
	}

out:
	sqlite3_close(handle);
	free(query);
	exit(0);
}	

void show_table()
{
	if(yubikey_table && !oath_table && !api_table)
		print_table("yubikeys",YUBIKEY);
	else if(!yubikey_table && oath_table && !api_table)
		print_table("oathtokens",OATH);
	else if(!yubikey_table && !oath_table && api_table)
		print_table("apikeys",API);
	else 
		printf("Please choose a table to list.\n");
}

void user_ops(char *user,int operation)
{
	if(yubikey_table && !oath_table && !api_table)
		db_ops("yubikeys",user,operation,YUBIKEY);
	else if(!yubikey_table && oath_table && !api_table)
		db_ops("oathtokens",user,operation,OATH);
	else if(!yubikey_table && !oath_table && api_table)
		db_ops("apikeys",user,operation,API);
	else
		printf("Please choose a table first.\n");
		exit(2);
}

void usage()
{
	fprintf(stderr,"yubiserve-admin Yubikey Database Management Tool\n"
		       "Version " VERSION ". Written and copyrights by Nanakos Chrysostomos.\n"
		       "THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY! USE AT YOUR OWN RISK!\n"
		       "\nUsage: yubiserver-admin [[-b FILE]] [table] [options] [[attributes]] \n\n"
		       "Options supported:\n"
		       "   --version or -V			Print version information\n"
		       "   --help or -h				Print this help screen\n"
		       "   --database or -b			Use this SQLite3 database file (optional)\n"
		       "   --yubikey or -y			Choose Yubikey Token table\n"
		       "   --oath or -o				Choose OATH Token table\n"
		       "   --api or -p				Choose API Key table\n"
		       "   --add N [P S [A]] or -a N [P S [A]]	Add Yubikey OTP & HOTP/OATH token or API Key 'N' user\n"
		       "					where N is the username, P the Public Token ID,\n"
		       "					S the Secret ID and A the AES Key\n"
		       "					N must be 16 characters max,P must be 12 characters\n"
		       "					for Yubikey OTP and 12 characters max for HOTP/OATH\n"
		       "					S must be 12 characters for Yubikey OTP and 40 for HOTP/OATH\n"
		       "					and AES key must be 32 characters\n"
		       "					Adding a user to API keys requires a username\n"
		       "					and a API Key 20 characters long\n"
		       "   --delete N or -x N			Delete Yubikey OTP, HOTP/OATH token or API Key 'N' user\n"
		       "   --enable N or -e N			Enable Yubikey OTP, HOTP/OATH token 'N' user\n"
		       "   --disable N or -d N			Disable Yubikey OTP, HOTP/OATH token 'N' user\n"
		       "   --list or -l				List Yubikey OTP, HOTP/OATH token or API Key\n"
		       "This version of yubiserver-admin has been configured with '" SQLITE3_DB_PATH "' as its default\n"
                       "SQLite3 database file.\n");
}
int main(int argc, char **argv)
{

	struct option long_options[] = 
	{
		/*Takes no parameters */
		{"version",0,0,'V'},
		{"help",0,0,'h'},
		{"list",0,0,'l'},
		{"yubikey",0,0,'y'},
		{"oath",0,0,'o'},
		{"api",0,0,'p'},

		/* Takes parameters */
		{"add",1,0,'a'},
		{"delete",1,0,'x'},
		{"enable",1,0,'e'},
		{"disable",1,0,'d'},
		{"database",1,0,'b'},
		{0,0,0}
	};
	int option_index = 0,c;
	if(argc<2)
	{
		usage();
		exit(0);
	}

	while( (c=getopt_long(argc,argv,"Vhlyopa:x:e:d:b:",long_options, &option_index)) != -1)
	{
		switch(c)
		{
			case 'V':
				printf("yubiserver-admin version " VERSION ". Copyright (C) 2011 Nanakos Chrysostomos.\n"
					"This program is free software; you can redistribute it and/or modify\n"
					"it under the terms of the GNU General Public License as published by\n"
					"the Free Software Foundation; either version 2 of the License, or\n"
					"(at your option) any later version.\n" );
				exit(0);
			case 'h':
				usage();
				exit(0);
			case 'b':
				sqlite3_dbpath = strdup(optarg);
				ext_db = 1;
				break;
			case 'a':
				Argc = argc;
				Argv = argv;
				user_ops(strdup(optarg),ADD_USER);
				fprintf(stderr,"Never here\n");
				exit(2);
			case 'y':
				yubikey_table = 1;
				break;
			case 'o':
				oath_table = 1;
				break;
			case 'p':
				api_table = 1;
				break;
			case 'l':
				show_table();
				exit(0);
			case 'e':
				user_ops(strdup(optarg),ENABLE_USER);
				fprintf(stderr,"Never here\n");
				exit(2);
			case 'd':
				user_ops(strdup(optarg),DISABLE_USER);
				fprintf(stderr,"Never here\n");
				exit(2);
			case 'x':
				user_ops(strdup(optarg),DELETE_USER);
				fprintf(stderr,"Never here\n");
				exit(2);
		}
	}

	//printf("Exiting...\n");
	exit(0);

}