113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle/* Copyright (C) 2005 Red Hat, Inc. */
213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindlestruct semanage_user_extra;
413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindlestruct semanage_user_key;
513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindletypedef struct semanage_user_extra record_t;
613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindletypedef struct semanage_user_key record_key_t;
713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#define DBASE_RECORD_DEFINED
813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindlestruct dbase_file;
1013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindletypedef struct dbase_file dbase_t;
1113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#define DBASE_DEFINED
1213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
1313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include <stdlib.h>
1413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include <stdio.h>
1513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include <strings.h>
1613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
1713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include "user_internal.h"
1813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include "database_file.h"
1913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include "parse_utils.h"
2013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include "debug.h"
2113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle#include "handle.h"
2213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
2313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindlestatic int user_extra_print(semanage_handle_t * handle,
2413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle			    semanage_user_extra_t * user_extra, FILE * str)
2513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle{
2613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
2713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	const char *name = semanage_user_extra_get_name(user_extra);
2813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	const char *prefix = semanage_user_extra_get_prefix(user_extra);
2913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
3013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (fprintf(str, "user %s prefix %s;\n", name, prefix) < 0)
3113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
3213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
3313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	return STATUS_SUCCESS;
3413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
3513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle      err:
3613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	ERR(handle, "could not print user extra data "
3713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	    "for %s to stream", name);
3813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	return STATUS_ERR;
3913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle}
4013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
4113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindlestatic int user_extra_parse(semanage_handle_t * handle,
4213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle			    parse_info_t * info,
4313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle			    semanage_user_extra_t * user_extra)
4413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle{
4513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
4613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	char *str = NULL;
4713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
4813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (parse_skip_space(handle, info) < 0)
4913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
5013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (!info->ptr)
5113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto last;
5213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
5313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	/* User string */
5413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (parse_assert_str(handle, info, "user") < 0)
5513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
5613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (parse_assert_space(handle, info) < 0)
5713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
5813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
5913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	/* Extract name */
6013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (parse_fetch_string(handle, info, &str, ' ') < 0)
6113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
6213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (semanage_user_extra_set_name(handle, user_extra, str) < 0)
6313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
6413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	free(str);
6513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	str = NULL;
6613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
6713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	/* Prefix string */
6813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (parse_assert_space(handle, info) < 0)
6913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
7013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (parse_assert_str(handle, info, "prefix") < 0)
7113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
7213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (parse_assert_space(handle, info) < 0)
7313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
7413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
7513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	/* Extract prefix */
7613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (parse_fetch_string(handle, info, &str, ';') < 0)
7713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
7813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (semanage_user_extra_set_prefix(handle, user_extra, str) < 0)
7913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
8013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	free(str);
8113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	str = NULL;
8213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
8313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	/* Semicolon */
8413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (parse_skip_space(handle, info) < 0)
8513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
8613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (parse_assert_ch(handle, info, ';') < 0)
8713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		goto err;
8813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
8913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	return STATUS_SUCCESS;
9013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
9113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle      last:
9213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	parse_dispose_line(info);
9313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	return STATUS_NODATA;
9413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
9513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle      err:
9613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	ERR(handle, "could not parse user extra data");
9713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	free(str);
9813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	parse_dispose_line(info);
9913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	return STATUS_ERR;
10013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle}
10113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
10213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle/* USER EXTRA RECORD: FILE extension: method table */
10313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindlerecord_file_table_t SEMANAGE_USER_EXTRA_FILE_RTABLE = {
10413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	.parse = user_extra_parse,
10513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	.print = user_extra_print,
10613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle};
10713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
10813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindleint user_extra_file_dbase_init(semanage_handle_t * handle,
109e37fa2f63be89afab9b5f5ddfedbd589d0676c4eCaleb Case			       const char *path_ro,
110e37fa2f63be89afab9b5f5ddfedbd589d0676c4eCaleb Case			       const char *path_rw,
111e37fa2f63be89afab9b5f5ddfedbd589d0676c4eCaleb Case			       dbase_config_t * dconfig)
11213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle{
11313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
11413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	if (dbase_file_init(handle,
115e37fa2f63be89afab9b5f5ddfedbd589d0676c4eCaleb Case			    path_ro,
116e37fa2f63be89afab9b5f5ddfedbd589d0676c4eCaleb Case			    path_rw,
11713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle			    &SEMANAGE_USER_EXTRA_RTABLE,
11813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle			    &SEMANAGE_USER_EXTRA_FILE_RTABLE,
11913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle			    &dconfig->dbase) < 0)
12013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle		return STATUS_ERR;
12113cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
12213cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	dconfig->dtable = &SEMANAGE_FILE_DTABLE;
12313cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	return STATUS_SUCCESS;
12413cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle}
12513cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
12613cd4c8960688af11ad23b4c946149015c80d54Joshua Brindlevoid user_extra_file_dbase_release(dbase_config_t * dconfig)
12713cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle{
12813cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle
12913cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle	dbase_file_release(dconfig->dbase);
13013cd4c8960688af11ad23b4c946149015c80d54Joshua Brindle}
131