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 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "keyfile.h"
33
34 KeyfileConfig *load_keyfile_config(
char *file) {
35 FILE *in = fopen(file,
"r");
36 if(in ==
NULL) {
37 return NULL;
38 }
39
40 KeyfileConfig *conf = malloc(
sizeof(KeyfileConfig));
41 conf->parser.parse = keyfile_parse;
42 conf->file = file;
43 conf->users =
NULL;
44
45 int r = cfg_parse_basic_file((ConfigParser*)conf, in);
46 if(r !=
0) {
47 fclose(in);
48 free(conf);
49
50 return NULL;
51 }
52
53 fclose(in);
54
55 return conf;
56 }
57
58 void free_keyfile_config(KeyfileConfig *conf) {
59 if(conf->users) {
60 ucx_list_free_a(conf->parser.mp, conf->users);
61 }
62 ucx_mempool_destroy(conf->parser.mp->pool);
63 free(conf);
64 }
65
66 int keyfile_parse(
void *p, ConfigLine *begin, ConfigLine *end,
sstr_t line) {
67 KeyfileConfig *conf = p;
68 UcxAllocator *mp = conf->parser.mp;
69
70 ssize_t tkn =
0;
71 sstr_t *tk = sstrsplit(line, sstrn(
";",
1), &tkn);
72
73 if(tkn <
2) {
74 return 1;
75 }
76
77 KeyfileEntry *entry =
OBJ_NEW(mp, KeyfileEntry);
78 entry->groups =
NULL;
79 entry->numgroups =
0;
80
81
82 entry->name = sstrdup_a(mp, tk[
0]);
83
84
85 sstr_t hash = sstrtrim(tk[
1]);
86 if(hash.length <
4) {
87
88 return 1;
89 }
90 if(hash.ptr[
0] !=
'{') {
91
92 return 1;
93 }
94
95
96 sstr_t hash_type;
97 sstr_t hash_data;
98 for(
int i=
1;i<hash.length;i++) {
99 if(hash.ptr[i] ==
'}') {
100 hash_type = sstrsubsl(hash,
1, i-
1);
101 hash_data = sstrsubs(hash, i+
1);
102 }
103 }
104
105 if(!sstrcmp(hash_type, sstr(
"SSHA"))) {
106 entry->hashtype =
KEYFILE_SSHA;
107 }
else if(!sstrcmp(hash_type, sstr(
"SSHA256"))) {
108 entry->hashtype =
KEYFILE_SSHA256;
109 }
else if(!sstrcmp(hash_type, sstr(
"SSHA512"))) {
110 entry->hashtype =
KEYFILE_SSHA512;
111 }
else {
112
113 log_ereport(
114 LOG_FAILURE,
115 "keyfile_parse: unknown hash type: %s",
116 sstrdup_a(mp, hash_type).ptr);
117 return 1;
118 }
119
120 entry->hashdata = sstrdup_a(mp, hash_data);
121
122
123 if(tkn ==
3) {
124 sstr_t groups_str = sstrtrim(tk[
2]);
125 ssize_t ngroups =
0;
126 sstr_t *groups = sstrsplit(groups_str, sstrn(
",",
1), &ngroups);
127 if(ngroups >
0) {
128 entry->groups = mp->calloc(mp->pool, ngroups,
sizeof(
sstr_t));
129 entry->numgroups = ngroups;
130 for(
int i=
0;i<ngroups;i++) {
131 entry->groups[i] = sstrdup_a(mp, sstrtrim(groups[i]));
132 free(groups[i].ptr);
133 }
134 free(groups);
135 }
136 }
137
138
139 conf->users = ucx_list_append_a(mp, conf->users, entry);
140
141
142 for(
int i=
0;i<tkn;i++) {
143 free(tk[i].ptr);
144 }
145 free(tk);
146
147 return 0;
148 }
149