a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina/*
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina Authors:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina Pavel Březina <pbrezina@redhat.com>
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina Copyright (C) 2015 Red Hat
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina This program is free software; you can redistribute it and/or modify
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina it under the terms of the GNU General Public License as published by
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina the Free Software Foundation; either version 3 of the License, or
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina (at your option) any later version.
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina This program is distributed in the hope that it will be useful,
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina but WITHOUT ANY WARRANTY; without even the implied warranty of
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina GNU General Public License for more details.
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina You should have received a copy of the GNU General Public License
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina along with this program. If not, see <http://www.gnu.org/licenses/>.
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina*/
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina#include <stdlib.h>
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina#include "util/util.h"
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina#include "util/strtonum.h"
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina#include "tools/common/sss_colondb.h"
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina#define IS_STD_FILE(db) ((db)->file == stdin || (db)->file == stdout)
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březinastatic char *read_field_as_string(char *line,
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina const char **_value)
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina{
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina char *rest;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina char *value;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (line == NULL || *line == '\n' || *line == '\0') {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina /* There is nothing else to read. */
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina rest = NULL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina value = NULL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina goto done;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (*line == ':') {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina /* Special case for empty value. */
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina *line = '\0';
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina rest = line + 1;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina value = NULL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina goto done;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina /* Value starts at current position. */
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina value = line;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina /* Find next field delimiter. */
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina rest = strchr(line, ':');
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (rest == NULL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina /* There is no more field. Remove \n from the end. */
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina rest = strchr(line, '\n');
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (rest != NULL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina *rest = '\0';
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina rest = NULL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina goto done;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina /* Remove it and step one character further. */
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina *rest = '\0';
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina rest++;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březinadone:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina *_value = value;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return rest;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina}
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březinastatic char *read_field_as_uint32(char *line,
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina uint32_t *_value)
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina{
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina const char *str;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina char *rest;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina errno_t ret;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina rest = read_field_as_string(line, &str);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (str == NULL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina *_value = 0;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return rest;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina *_value = strtouint32(str, NULL, 10);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (errno != 0) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret = errno;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to parse number [%d]: %s\n",
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret, sss_strerror(ret));
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina *_value = 0;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return rest;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina}
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březinastruct sss_colondb {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina FILE *file;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina enum sss_colondb_mode mode;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina};
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březinaerrno_t sss_colondb_readline(TALLOC_CTX *mem_ctx,
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina struct sss_colondb *db,
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina struct sss_colondb_read_field *table)
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina{
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina int readchars;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina size_t linelen = 0;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina char *line = NULL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina char *tcline;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina char *rest;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina errno_t ret;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina int i;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (db->mode != SSS_COLONDB_READ) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return ERR_INTERNAL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina readchars = getline(&line, &linelen, db->file);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (readchars == -1) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina /* Nothing was read. */
2dd75ea79a57615808754c0ce550786edbc17d69Petr Cech
2dd75ea79a57615808754c0ce550786edbc17d69Petr Cech free(line);
2dd75ea79a57615808754c0ce550786edbc17d69Petr Cech line = NULL;
2dd75ea79a57615808754c0ce550786edbc17d69Petr Cech
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (errno != 0) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret = errno;
de1131abe5ba7aaeb59f81fc3a9cd2a71c0b52ddLukas Slebodnik DEBUG(SSSDBG_CRIT_FAILURE, "Unable to read line [%d]: %s\n",
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret, sss_strerror(ret));
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return ret;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return EOF;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina /* Copy line to mem_ctx. */
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina tcline = talloc_strdup(mem_ctx, line);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina free(line);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina line = NULL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (tcline == NULL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup() failed\n");
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return ENOMEM;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina rest = tcline;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina for (i = 0; table[i].type != SSS_COLONDB_SENTINEL; i++) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina switch (table[i].type) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina case SSS_COLONDB_UINT32:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina rest = read_field_as_uint32(rest, table[i].data.uint32);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina break;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina case SSS_COLONDB_STRING:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina rest = read_field_as_string(rest, table[i].data.str);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina break;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina case SSS_COLONDB_SENTINEL:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Trying to process sentinel?!\n");
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret = ERR_INTERNAL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina goto done;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (rest == NULL && table[i + 1].type != SSS_COLONDB_SENTINEL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE,
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina "Line contains less values than expected!\n");
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret = EINVAL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina goto done;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina } else if (rest != NULL && table[i + 1].type == SSS_COLONDB_SENTINEL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE,
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina "Line contains more values than expected!\n");
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret = EINVAL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina goto done;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret = EOK;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březinadone:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (ret != EOK) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina talloc_free(tcline);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return ret;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina}
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březinaerrno_t sss_colondb_writeline(struct sss_colondb *db,
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina struct sss_colondb_write_field *table)
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina{
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina TALLOC_CTX *tmp_ctx;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina char *line = NULL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina errno_t ret;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina int i;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (db->mode != SSS_COLONDB_WRITE) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return ERR_INTERNAL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina tmp_ctx = talloc_new(NULL);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (tmp_ctx == NULL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed.\n");
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return ENOMEM;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
6977d7c84145ac69195be58b3330861b9b8a3b72Lukas Slebodnik line = talloc_strdup(tmp_ctx, "");
6977d7c84145ac69195be58b3330861b9b8a3b72Lukas Slebodnik if (line == NULL) {
6977d7c84145ac69195be58b3330861b9b8a3b72Lukas Slebodnik DEBUG(SSSDBG_CRIT_FAILURE, "talloc_new() failed.\n");
6977d7c84145ac69195be58b3330861b9b8a3b72Lukas Slebodnik ret = ENOMEM;
6977d7c84145ac69195be58b3330861b9b8a3b72Lukas Slebodnik goto done;
6977d7c84145ac69195be58b3330861b9b8a3b72Lukas Slebodnik }
6977d7c84145ac69195be58b3330861b9b8a3b72Lukas Slebodnik
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina for (i = 0; table[i].type != SSS_COLONDB_SENTINEL; i++) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina switch (table[i].type) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina case SSS_COLONDB_UINT32:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (table[i].data.uint32 == 0) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina line = talloc_asprintf_append(line, ":");
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina } else {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina line = talloc_asprintf_append(line, ":%u", table[i].data.uint32);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina break;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina case SSS_COLONDB_STRING:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (table[i].data.str == NULL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina line = talloc_asprintf_append(line, ":");
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina } else {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina line = talloc_asprintf_append(line, ":%s", table[i].data.str);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina break;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina case SSS_COLONDB_SENTINEL:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Trying to process sentinel?!\n");
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret = ERR_INTERNAL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina goto done;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (line == NULL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret = ENOMEM;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina goto done;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina /* Remove starting : */
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina line++;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina fprintf(db->file, "%s\n", line);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina fflush(db->file);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret = EOK;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březinadone:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina talloc_free(tmp_ctx);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return ret;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina}
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březinastatic int sss_colondb_close(void *pvt)
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina{
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina struct sss_colondb *db = talloc_get_type(pvt, struct sss_colondb);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (db->file == NULL || IS_STD_FILE(db)) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return 0;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina fclose(db->file);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina db->file = NULL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return 0;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina}
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březinastatic FILE *open_db(const char *filename, enum sss_colondb_mode mode)
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina{
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina FILE *fp = NULL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina errno_t ret;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina errno = 0;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina switch (mode) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina case SSS_COLONDB_READ:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina fp = filename == NULL ? stdin : fopen(filename, "r");
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina break;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina case SSS_COLONDB_WRITE:
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina fp = filename == NULL ? stdout : fopen(filename, "w");
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina break;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (fp == NULL && filename != NULL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina ret = errno;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "Unable to open file %s [%d]: %s\n",
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina filename, ret, sss_strerror(ret));
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return fp;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina}
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březinastruct sss_colondb *sss_colondb_open(TALLOC_CTX *mem_ctx,
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina enum sss_colondb_mode mode,
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina const char *filename)
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina{
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina struct sss_colondb *db;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina db = talloc_zero(mem_ctx, struct sss_colondb);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (db == NULL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina DEBUG(SSSDBG_CRIT_FAILURE, "talloc_zero() failed\n");
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return NULL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina db->file = open_db(filename, mode);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina db->mode = mode;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina if (db->file == NULL) {
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina talloc_free(db);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return NULL;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina }
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina talloc_set_destructor((TALLOC_CTX *)db, sss_colondb_close);
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina return db;
a76f63544533f0404f7711a10c1a621c6045df17Pavel Březina}