aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
blob: b498b72581f65e8853855e96735815f5a225a6d9 (plain)
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
#include <stdio.h>
#include <termios.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <getopt.h>
#include <errno.h>
#include <dirent.h>
#include <time.h>

#include "easydir.h"
#include "handerror.h"

//#define DEBUG
#define MAXLEN_PASSWORD 128
#define STANDARD_AMOUNT_GENERATE_SYMBOLS 10
#define FULLNAMEFILE "/.lock-password/Password-Bank"
#define TREENAMEFILE "/.lock-password"
#define ADDNAMETOFULL "/Password-Bank"
#define NAMEFILE "Password-Bank"


#define LOCKPASS_DIR "/.lock-password/"
#define GPGKEY_FILE "/.gpg-key"

#define HASH_INIT 6385337657
#define HASH_HELP 6385292014
#define HASH_VERSION 229486327000139
#define HASH_COPY 6385123360
#define HASH_EDIT 6385183019
#define HASH_MOVE 249844339311324255
#define HASH_GENERATE 7572409341523952
#define HASH_INSERT 6953633055386
#define HASH_REMOVE 6953974396019
#define HASH_DELETE 6953426453624
#define HASH_CHANGE 6953390490059

// == global var == 
char *gPath_rootdir; // init ONLY in main

static void clearStdinBuff()
{
	char garbage;

	while( (garbage = fgetc(stdin)) != '\n' && garbage != EOF )
		;
}

static char* getPassword(char *path, char *password)
{
	FILE *filePass;
	filePass = fopen(path, "r");
	if(filePass == NULL) {
		if(errno == ENOENT) { // file doesn't exist
			printError("lpass: No such file exists\n");
		}
		callError(110);
	}

	char sign[MAXLEN_PASSWORD];
	if(!fgets(sign, sizeof(sign), filePass)) {
		callError(111);
	}

	strcpy(password, sign);
	fclose(filePass);
	return password;
}

static void showTree(char *path)
{
	if(opendir(path) != NULL) { // if it's directory
		int pid;
		pid = fork();
		if(pid == -1) callError(101);
		if(pid == 0) { /* new process */
			execlp("tree", "tree", "-C", "--noreport", path, NULL);
			perror("tree");
			exit(4);
		}
		wait(&pid);
	}
	else {
		char password[MAXLEN_PASSWORD];
		getPassword(path, password);
		printf("%s\n", password);
	}
}

static void insertPass(char *root_path, char *add_path, char *password)
{
	/* root_path = /home/[username]/
	add_path = banks/france/[number]
	main_path = banks/france
	file_path = [number] */

	char *main_path = malloc(sizeof(char) * strlen(add_path) + sizeof(char));
	char *file_path = malloc(sizeof(char) * strlen(add_path) + sizeof(char));

	if(splitPath(add_path, main_path, file_path) == NULL) {
		printError("lpass: The path you specified is incorrect\n");
	}

	int pass_buf = strlen(root_path) + strlen(add_path);
	char *final_path = (char*) malloc(sizeof(char) * pass_buf + sizeof(char));

	strcpy(final_path, root_path);
	strcat(final_path, "/");
	strcat(final_path, main_path);

	if(strcmp(main_path, "") != 0) {
		int pid = fork();
		if(pid == -1) callError(103);
		if(pid == 0) { /* new process */
			execlp("mkdir", "mkdir", "-p", main_path, NULL);
			perror("mkdir");
			exit(4);
		}
		wait(&pid);
	}
	
	if(chdir(final_path) != 0) {
		callError(107);
	}

	// create file, copy password there
	FILE *filePass;
	filePass = fopen(file_path, "w");	
	if(filePass == NULL) {
		callError(108);
	}

	fputs(password, filePass);

	free(main_path);
	free(file_path);
	fclose(filePass);
}

static char *generatePassword(char *dest, int amount)
{
	char allowed_symbols[] = {
		'A','E','I','J','O','U','B','C','D','F','G','H',
		'K','L','M','N','P','Q','R','S','T','V','W','X',
		'Y','Z','a','e','i','j','o','u','b','c','d','f',
		'g','h','k','l','m','n','p','q','r','s','t','v',
		'w','x','y','z','1','2','3','4','5','6','7','8',
		'9','0','!','#','$',';','%','^',':','&','?','*',
		'(',')','-','_','+','=','<', '>'
	};
	int max = sizeof(allowed_symbols);
	srand(time(NULL));

	char password[MAXLEN_PASSWORD];
	for(int i=0; i < amount; i++)
	{
		char c = allowed_symbols[rand() % max];

		password[i] = c;
		password[i+1] = '\0';
	}

	strcpy(dest, password);
	return dest;
}

static void nonvisibleEnter(int status)
{
	struct termios term_settings;
	tcgetattr(0, &term_settings); // get current settings
	if(status == 1) {
		term_settings.c_lflag &= ~ECHO; // flag reset
	}
	else {
		term_settings.c_lflag |= ECHO;
	}
	tcsetattr(0, TCSANOW, &term_settings);
}

static int getAnswer(char *text)
{
	char answer;
	printf("%s\n", text);
	while( (answer = fgetc(stdin)) )
	{
		clearStdinBuff();
		switch(answer)
		{
			case 'Y':
			case 'y':
			{
				return 1;
			}
			case 'N':
			case 'n':
			{
				return 0;
			}
			case EOF:
			{
				printError("lpass: Unexpected end of file\n");
			}
			default:
			{
				printf("%s\n", text);
				break;
			}
		}
	}

	return -1;
}

static char *typePass(char *text, char *dest)
{
	printf("%s", text);
	if(fgets(dest, (sizeof(char)*MAXLEN_PASSWORD + sizeof(char)), stdin) == NULL) {
		nonvisibleEnter(0);
		printError("lpass: Unexpected end of file\n");
	}

	int len = strlen(dest);
	if(len < 2) {
		nonvisibleEnter(0);
		printError("lpass: incorrect password\n");
	}
	
	if(dest[len-1] == '\n') {
		dest[len-1] = '\0';
	}

	#if defined(DEBUG)
		printf("%s", dest);
	#endif

	printf("\n"); // for new line

	return dest;
}

static void cmd_init(int argc, char *argv[])
{
	// argv[2] = name of storage
	// argv[3] = gpg key

	if(argv[2] == NULL)
		printError("Use: lpass init [storage name] [gpg key]\n");
	else if(strcmp(argv[2], argv[3]) == 0 || argv[3] == NULL)
		printError("Use: lpass init [storage name] [gpg key]\n");

	char *main_path, *file_path;
	main_path = (char *) malloc( sizeof(char) * (strlen(argv[2])+1) );
	file_path = (char *) malloc( sizeof(char) * (strlen(argv[2])+1) );

	if(splitPath(argv[2], main_path, file_path) == NULL) { // check correct input
		printError("lpass: The path you specified is incorrect\n");
	}


	// create main directory:
	int len_passwordstore = strlen(gPath_rootdir) + strlen(file_path) + strlen(GPGKEY_FILE) + 1; // +1 for '\0'
	char *path_passwordstore = (char *) malloc(sizeof(char) * len_passwordstore);

	strcpy(path_passwordstore, gPath_rootdir);
	strcat(path_passwordstore, file_path);

	int pid = fork();
	if(pid == -1) callError(100);
	if(pid == 0) { /* new process */
		execlp("mkdir", "mkdir", "-vp", path_passwordstore, NULL);
		perror("mkdir");
		exit(4);
	}
	wait(&pid);

	// check .main-store
	FILE *filestore;
	filestore = fopen(path_passwordstore, "r");
	if(filestore == NULL) {
		// create .main-store
		filestore = fopen(path_passwordstore, "w");
		if(filestore == NULL) {
			callError(121);
		}
		fputs(file_path, filestore);
	}
	fclose(filestore);

	// create .gpg-key in store
	strcat(path_passwordstore, GPGKEY_FILE);
	FILE *filekey;
	filekey = fopen(path_passwordstore, "w");	
	if(filekey == NULL) {
		callError(122);
	}
	fputs(argv[3], filekey);
	fclose(filekey);

	free(path_passwordstore);
	free(main_path);
	free(file_path);
	printf("LockPassword initialized successfully\n");
}

static void cmd_change(int argc, char *argv[])
{
	// argv[2] = desired password storage
	if(argv[2] == NULL)
		printError("Use: lpass change [storage name]\n");
}

static unsigned long hash(char *str)
{
    unsigned long hash = 5381;
    char c;

    while( (c = *str++) )
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash;
}

int main(int argc, char *argv[])
{
	if(!isatty(0)) { // stdin
		printError("lpass: Please, use a terminal to run this program\n");
	}

	int len_rootdir = strlen(getenv("HOME")) + strlen(LOCKPASS_DIR) + 1; // +1 for '\0'
	gPath_rootdir = (char *) malloc(sizeof(char) * len_rootdir);

	strcpy(gPath_rootdir, getenv("HOME"));
	strcat(gPath_rootdir, LOCKPASS_DIR);

	//int path_init = 0;
	//if(chdir(gPath_rootdir) == 0) path_init = 1;

	if(argv[1] == NULL) {
		printf("show tree\n");
	}
	else
	{
		switch( hash(argv[1]) )
		{
		case HASH_INIT:
			cmd_init(argc, argv);
			break;
		case HASH_HELP:
			break;
		case HASH_VERSION:
			break;
		case HASH_COPY:
			break;
		case HASH_CHANGE:
			cmd_change(argc, argv);
			break;
		case HASH_EDIT:
			break;
		case HASH_MOVE:
			break;
		case HASH_GENERATE:
			break;
		case HASH_INSERT:
			break;
		case HASH_REMOVE:
		case HASH_DELETE:
			printf("borrr\n");
			break;
		default:
			printf("please, use help\n");
			break;
		}
	}

	free(gPath_rootdir);
	return 0;



	#if defined(DEBUG)
		char* short_options = "c:e:g:i:R:t";
	#else
		char* short_options = "c:e:g:i:R:";
	#endif

	struct option long_options[] = {
		{"init", required_argument, NULL, 'I'},
		{NULL, 0, NULL, 0}
	};

	int rootBuf = strlen(getenv("HOME")) + strlen(FULLNAMEFILE);
	char *rootPath = (char *) malloc(sizeof(char) * rootBuf + sizeof(char)); // +1 for '\0'

	int path_init = 0;
	strcpy(rootPath, getenv("HOME"));
	strcat(rootPath, TREENAMEFILE);

	if(chdir(rootPath) == 0) path_init = 1;

	int option_index = -1, result = -1;
	result = getopt_long(argc, argv, short_options, long_options, &option_index);
	if(!path_init) {
		if(result != 'I') // if doesn't init
		{ 
			if(errno == ENOENT) { // file doesn't exist
				printError("lpass: Before starting work, you must initialize LockPassword\n\
					Use: lpass --init\n");
			}
			callError(102);
		}
	}
	else {
		if(result == 'I') {
			printError("lpass: You already initialized LockPassword\n");
		}
		else if(result != -1)
		{
			strcat(rootPath, ADDNAMETOFULL); // complements to full path
			if(chdir(rootPath) != 0) {
				callError(112);
			}
		}
	}

	if(result != -1)
	{
		switch(result) {
		case 'c': {
			printf("Coming soon...\n");
			break;
		}
		case 'e': {
			char *passPath = optarg;

			if(checkFileExist(passPath) == 0) {
				printError("lpass: No such file exists\n");
			}

			/* ask user about change pass */
			int buffSize = (strlen("Do you want edit password in '' (Y/N)?") + strlen(passPath)) * sizeof(char) + sizeof(char);
			char *str = malloc(buffSize);
			snprintf(str, buffSize, "Do you want edit password in '%s' (Y/N)?", passPath);

			if(getAnswer(str) != 1) {
				free(str);
				return 0;
			}
			free(str);

			/* enter password */
			nonvisibleEnter(1); // change terminal work
			char pass_one[MAXLEN_PASSWORD], pass_two[MAXLEN_PASSWORD];
			typePass("Please type your new password: ", pass_one);
			typePass("Please type your new password again: ", pass_two);
			nonvisibleEnter(0);

			if(strcmp(pass_one, pass_two) == 0) {
				printf("Password correct\n");

				insertPass(rootPath, passPath, pass_one);
				printf("Password updated successfully for %s\n", passPath);
			}
			else printf("Passwords do not match\n");

			break;
		}
		case 'g': {
			char *passPath = argv[argc-1];
			char *lenPass = optarg;

			if(checkFileExist(passPath) == 1) {
				/* ask user about change pass */
				int buffSize = (strlen("Do you want generate new password and paste it in '' (Y/N)?") + strlen(passPath)) * sizeof(char) + sizeof(char);
				char *str = malloc(buffSize);

				snprintf(str, buffSize, "Do you want generate new password and paste it in '%s' (Y/N)?", passPath);
				if(getAnswer(str) != 1) {
					// free(str);
					return 0;
				}
				free(str);
			}

			int n_symbols = 0;
			if(strcmp(lenPass, passPath) == 0)
				n_symbols = STANDARD_AMOUNT_GENERATE_SYMBOLS;
			else n_symbols = atoi(lenPass);


			if(n_symbols < 1 || n_symbols > 127) {
				printError("lpass: you typed an incorrect number");
			}

			/* generate password */
			char gpass[MAXLEN_PASSWORD];
			generatePassword(gpass, n_symbols);

			insertPass(rootPath, passPath, gpass);
			printf("Generated password: %s\n", gpass);
			printf("Password added successfully for %s\n", passPath);

			break;
		}
		case 'i': {
			if(checkFileExist(optarg) == 1) {
				printf("lpass: To change pass use argument '-e'\n");
				return 0;
			}
			
			/* enter password */
			nonvisibleEnter(1); // change terminal work
			char pass_one[MAXLEN_PASSWORD], pass_two[MAXLEN_PASSWORD];
			typePass("Please type your password: ", pass_one);
			typePass("Please type your password again: ", pass_two);
			nonvisibleEnter(0);

			if(strcmp(pass_one, pass_two) == 0) {
				printf("Password correct\n");

				insertPass(rootPath, optarg, pass_one);
				printf("Password added successfully for %s\n", optarg);
			}
			else printf("lpass: Passwords do not match\n");

			break;
		}
		case 'R': {
			if(checkFileExist(optarg) == 0) {
				printError("lpass: No such file exists\n");
			}

			// check working
			char *main_path, *file_path;
			main_path = (char *) malloc( sizeof(char) * (strlen(optarg)+1) );
			file_path = (char *) malloc( sizeof(char) * (strlen(optarg)+1) );

			if(splitPath(optarg, main_path, file_path) == NULL) { // check correct input
				printError("lpass: The path you specified is incorrect\n");
			}

			if(deleteFile(optarg)) {
				deleteEmptyDir(main_path);
			}

			free(main_path);
			free(file_path);
			break;
		}
		case 'I': {
			// create direction:
			int pid = fork();
			if(pid == -1) callError(100);
			if(pid == 0) { /* new process */
				execlp("mkdir", "mkdir", "-vp", rootPath, NULL);
				perror("mkdir");
				exit(4);
			}
			wait(&pid);
			printf("LockPassword initialized successfully\n");
			break;
		}

		#if defined(DEBUG)
		case 't':
		{
			// for check new features:
			break;
		}
		#endif

		default: break;

		}
	}
	else
	{
		char *ptr;
		if(strcmp(argv[argc-1], argv[0]) == 0) {
			ptr = malloc(sizeof(char) * strlen(NAMEFILE) + sizeof(char));
			strcpy(ptr, NAMEFILE);
		}
		else {
			int buff_tree = strlen(NAMEFILE) + strlen(argv[argc-1]);
			ptr = malloc(sizeof(char) * buff_tree + sizeof(char) + sizeof(char));
			strcpy(ptr, NAMEFILE);
			strcat(ptr, "/");
			strcat(ptr, argv[argc-1]);
		}
		showTree(ptr);

		free(ptr);
	}

	return 0;
}