summaryrefslogtreecommitdiff
path: root/nslcd/hackers_parse.c
blob: 7d63995bf635abf390649a9280afb7c17290754b (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
/* hackers_parse.c - load user data from hackers.git
 *
 * Copyright (C) 2014 Luke Shumaker
 *
 * 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/>.
 */
#define _GNU_SOURCE

#include <stdbool.h>
#include <stdio.h>
#include <yaml.h>

#include <error.h>
#include <errno.h>

/* These three are just for name2gid, which is surprisingly
 * complicated. */
#include <errno.h>
#include <grp.h>
#include <unistd.h> /* for sysconf(3) */

#include "hackers_parse.h"

/* None of the malloc-ish calls are error checked.  Bite me. */

#define DEFAULT_PASSWORD "!"

#define ASSERT(expr)                                                 \
	do {                                                         \
		errno = 0;                                           \
		if (!(expr)) {                                       \
			error(0, errno, "ASSERT(%s) failed", #expr); \
			goto error;                                  \
		}                                                    \
	} while(0)

/* Get a string value from a YAML scalar node */
#define STR_VALUE(node)                                   \
	({                                                \
		ASSERT((node)->type == YAML_SCALAR_NODE); \
		((char*)(node)->data.scalar.value);       \
	})

/* Bitmask flags for the completion of the fields in
 * 'struct passwd' (which is defined in <pwd.h>) */
#define	PW_NAME  	 (1 << 0)
#define	PW_PASSWD	 (1 << 1)
#define	PW_UID   	 (1 << 2)
#define	PW_GID   	 (1 << 3)
#define	PW_GECOS 	 (1 << 4)
#define	PW_DIR   	 (1 << 5)
#define	PW_SHELL 	 (1 << 6)
#define PW_ALL		((1 << 7) - 1)

static 
gid_t
name2gid(const char *name) {
	gid_t gid;
	ssize_t buflen = sysconf(_SC_GETGR_R_SIZE_MAX);
	if (buflen < 1)
		buflen = 256;
	char *buf = malloc(buflen);
	struct group grp;
	struct group *ret;
	while (getgrnam_r(name, &grp, buf, (size_t)buflen, &ret) < 0) {
		if (errno == ERANGE) {
			buflen += 256;
			buf = realloc(buf, buflen);
		} else {
			gid = 0;
			goto end;
		}
	}
	if (ret == NULL) {
		gid = 0;
		goto end;
	}
	gid = ret->gr_gid;
 end:
	free(buf);
	return gid;
}

static
bool
is_numeric(const char *str) {
	size_t i;
	for (i = 0; str[i] != '\0'; i++) {
		if ('0' > str[i]  || str[i] > '9')
			return false;
	}
	return (i > 0);
}

uid_t
filename2uid(const char *filename) {
	if (filename == NULL)
		return 0;

	char *tmp = strdupa(filename);
	char *bname = basename(tmp);
	char *ext = strchr(bname, '.');
	if ((strcmp(ext, ".yml") != 0) || (ext != &bname[4]))
		return 0;
	ext[0] = '\0';
	uid_t uid = is_numeric(bname) ? atoi(bname) : 0;
	return uid;
}

int
load_user_password(struct passwd *user) {
	char *filename = NULL;
	FILE *file;
	char *line = NULL;
	ssize_t line_len;
	size_t line_cap = 0;

	asprintf(&filename, "%s/.password", user->pw_dir);
	if ((file = fopen(filename, "r")) == NULL)
		goto nopassword;
	// TODO: check permissions on 'file'
	if ((line_len = getline(&line, &line_cap, file)) < 1)
		goto nopassword;
	if (line[line_len-1] == '\n')
		line[--line_len] = '\0';
	free(filename);
	fclose(file);
	user->pw_passwd = line;
	return 0;
 nopassword:
	free(filename);
	free(line);
	user->pw_passwd = strdup("!");
	return 0;
}

#define NODE(id) yaml_document_get_node(&yaml_document, id)
int
load_user_yaml(const char *filename, struct passwd *user) {
	int ret = -1;
	unsigned char flags = 0;

	PASSWD_FREE(*user);

	FILE *yaml_file = NULL;
	yaml_parser_t	yaml_parser;	ZERO(yaml_parser);
	yaml_document_t	yaml_document;	ZERO(yaml_document);

	uid_t uid;
	ASSERT((uid = user->pw_uid = filename2uid(filename)) > 0);
	flags |= PW_UID;

	ASSERT((yaml_file = fopen(filename, "r")) != NULL);
	ASSERT(yaml_parser_initialize(&yaml_parser));
	yaml_parser_set_input_file(&yaml_parser, yaml_file);
	ASSERT(yaml_parser_load(&yaml_parser, &yaml_document));
	yaml_node_t *root = yaml_document_get_root_node(&yaml_document);

	ASSERT(root->type == YAML_MAPPING_NODE);
	for (yaml_node_pair_t *pair = root->data.mapping.pairs.start;
	     pair < root->data.mapping.pairs.top;
	     pair++) {
		yaml_node_t *key = NODE(pair->key);
		yaml_node_t *val = NODE(pair->value);
		if (strcmp("username", STR_VALUE(key))==0) {
			user->pw_name = strdup(STR_VALUE(val));
			asprintf(&(user->pw_dir), "/home/%s", user->pw_name);
			flags |= PW_NAME | PW_DIR;
		}
		if (strcmp("fullname", STR_VALUE(key))==0) {
			user->pw_gecos = strdup(STR_VALUE(val));
			flags |= PW_GECOS;
		}
		if (strcmp("shell", STR_VALUE(key))==0) {
			user->pw_shell = strdup(STR_VALUE(val));
			flags |= PW_SHELL;
		}
		if (strcmp("groups", STR_VALUE(key))==0) {
			if (val->type != YAML_SEQUENCE_NODE)
				goto error;
			for (yaml_node_item_t *itemp = val->data.sequence.items.start;
			     itemp < val->data.sequence.items.top;
			     itemp++) {
				yaml_node_t *item = NODE(*itemp);
				if (itemp == val->data.sequence.items.start) {
					/* primary group */
					char *grp_name = STR_VALUE(item);
					ASSERT((user->pw_gid = name2gid(grp_name)) > 0);
					flags |= PW_GID;
				} else {
					/* secondary group */
					// TODO: do something here
				}
			}
		}
	}
	if (flags & PW_DIR) {
		if (load_user_password(user) < 0)
			goto error;
		flags |= PW_PASSWD;
	}
	ASSERT(flags == PW_ALL);
	ret = 0;
	goto end;
 error:
	PASSWD_FREE(*user);
	user->pw_uid = uid;
 end:
	yaml_document_delete(&yaml_document);
	yaml_parser_delete(&yaml_parser);
	if (yaml_file != NULL)
		fclose(yaml_file);
	return ret;
}