aboutsummaryrefslogtreecommitdiff
path: root/lib/udb.c
blob: c2ba9cc6a84975f85c67f43e687c4d18c4192acb (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
/* This file is part of GNU Dico.
   Copyright (C) 1998-2018 Sergey Poznyakoff

   GNU Dico 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, or (at your option)
   any later version.

   GNU Dico 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 GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <dico.h>
#include <dico/udb.h>
#include <sys/types.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

struct dico_udb {
    void *handle;
    dico_url_t url;
    const char *qpw;
    const char *qgrp;
    const char *options;
    int (*_db_open) (void **, dico_url_t, const char *);
    int (*_db_close) (void *);
    int (*_db_check_password) (void *, const char *, const char *,
			       const char *);
    int (*_db_get_password) (void *, const char *, const char *, char **);
    int (*_db_get_groups) (void *, const char *, const char *, dico_list_t *);
};

int
dico_udb_open(dico_udb_t db)
{
    if (!db)
	return 1;
    if (!db->_db_open)
	return 0;
    return db->_db_open(&db->handle, db->url, db->options);
}

int
dico_udb_close(dico_udb_t db)
{
    int rc;

    if (!db->_db_close)
	rc = 0;
    else 
	rc = db->_db_close(db->handle);
    db->handle = NULL;
    return rc;
}

int
dico_udb_check_password(dico_udb_t db, const char *key, const char *pass)
{
    if (!db->_db_check_password)
	return ENOSYS;
    return db->_db_check_password(db->handle, db->qpw, key, pass);
}

int
dico_udb_get_password(dico_udb_t db, const char *key, char **pass)
{
    return db->_db_get_password(db->handle, db->qpw, key, pass);
}

int
dico_udb_get_groups(dico_udb_t db, const char *key, dico_list_t *groups)
{
    return db->_db_get_groups(db->handle, db->qgrp, key, groups);
}

dico_list_t /* of struct dico_udb_def */ dico_udb_def_list;

static int
udb_def_cmp(const void *item, const void *data, void *ignore)
{
    const struct dico_udb_def *def = item;
    const char *proto = data;
    return strcmp(def->proto, proto);
}

int
dico_udb_define(struct dico_udb_def *dptr)
{
    if (!dico_udb_def_list) {
	dico_udb_def_list = dico_list_create();
	if (!dico_udb_def_list) {
	    errno = ENOMEM;
	    return 1;
	}
	dico_list_set_comparator(dico_udb_def_list, udb_def_cmp, NULL);
    }
    return dico_list_append(dico_udb_def_list, dptr);
}

int
dico_udb_create(dico_udb_t *pdb,
		const char *urlstr, const char *qpw, const char *qgrp,
		const char *options)
{
    dico_url_t url;
    int rc;
    struct dico_udb_def *def;
    struct dico_udb *uptr;
    
    rc = dico_url_parse(&url, urlstr);
    if (rc) {
	errno = EINVAL;
	return 1;
    }

    def = dico_list_locate(dico_udb_def_list, url->proto);
    if (!def) {
	errno = EINVAL;
	dico_url_destroy(&url);
	return 1;
    }

    uptr = calloc(1, sizeof(*uptr));
    if (!uptr)
	return 1;
    
    uptr->url = url;
    uptr->qpw = qpw;
    uptr->qgrp = qgrp;
    uptr->options = options;
    uptr->_db_open = def->_db_open;
    uptr->_db_close = def->_db_close;
    uptr->_db_check_password = def->_db_check_password;
    uptr->_db_get_password = def->_db_get_password;
    uptr->_db_get_groups = def->_db_get_groups;

    *pdb = uptr;
    return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.