-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.c
More file actions
81 lines (75 loc) · 2.23 KB
/
config.c
File metadata and controls
81 lines (75 loc) · 2.23 KB
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "frcloud.h"
#include <readline/readline.h>
int load_token(char * tokenfilename, char * auth)
{
char * key_token;
if (access(tokenfilename, R_OK) == -1)
{
int i, len, key_fd;
not_correct_input:
key_token = readline(
"Security token not found. You may enter token now and it will be stored to "
KEY_FILE
" file in current directory.\nPress enter without input to exit console now.\n\x1B[32m\x1B[32m\x1B[1mToken> \x1B[0m");
if (key_token == NULL) {
fprintf(stderr, "Programm aborted by what?\n");
return -1;
}
len = strlen(key_token);
if (len == 0) {
free(key_token);
fprintf(stderr, "Programm aborted by user request - no token provided\n");
return -1;
}
for (i = 0; i < 52; i++) {
char c = key_token[i];
if (isdigit(c) || (isascii(c) && islower(c)))
continue;
free(key_token);
goto not_correct_input;
}
key_token[i] = 0;
key_fd = open(tokenfilename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
if (key_fd < 0)
{
free(key_token);
fprintf(stderr, "Unable create key token file: %s", KEY_FILE);
return -2;
}
write(key_fd, key_token, strlen(key_token));
if (fchmod(key_fd, S_IRUSR) == -1) {
free(key_token);
fprintf(stderr, "Set S_IRUSR attrib failed ob: %s", KEY_FILE);
return -3;
}
close(key_fd);
strcat(auth, key_token);
free(key_token);
}
else
{
FILE * key_file;
char buff[128];
key_file = fopen(tokenfilename, "r");
if (fgets(buff, sizeof(buff), key_file) == NULL)
{
fprintf(stderr, "Unable read token\n");
fclose(key_file);
return -2;
}
fclose(key_file);
if (strlen(buff) != 52)
{
fprintf(stderr, "Access token size error\n");
return -3;
}
strcat(auth, buff);
}
return 0;
}