source: trunk/third/cyrus-sasl/utils/sasldblistusers.c @ 17977

Revision 17977, 4.4 KB checked in by ghudson, 22 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r17976, which included commits to RCS files with non-trunk default branches.
Line 
1/* sasldblistusers.c -- list users in sasldb
2 * $Id: sasldblistusers.c,v 1.1.1.1 2002-10-13 18:00:24 ghudson Exp $
3 * Rob Siemborski
4 * Tim Martin
5 */
6/*
7 * Copyright (c) 2001 Carnegie Mellon University.  All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in
18 *    the documentation and/or other materials provided with the
19 *    distribution.
20 *
21 * 3. The name "Carnegie Mellon University" must not be used to
22 *    endorse or promote products derived from this software without
23 *    prior written permission. For permission or any other legal
24 *    details, please contact 
25 *      Office of Technology Transfer
26 *      Carnegie Mellon University
27 *      5000 Forbes Avenue
28 *      Pittsburgh, PA  15213-3890
29 *      (412) 268-4387, fax: (412) 268-7395
30 *      tech-transfer@andrew.cmu.edu
31 *
32 * 4. Redistributions of any form whatsoever must retain the following
33 *    acknowledgment:
34 *    "This product includes software developed by Computing Services
35 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
36 *
37 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
38 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
39 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
40 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
41 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
42 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
43 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
44 */
45
46#include <config.h>
47
48#include <stdio.h>
49#include <stdlib.h>
50
51#include <sasl.h>
52#include "../sasldb/sasldb.h"
53
54/* Cheating to make the utils work out right */
55extern const sasl_utils_t *sasl_global_utils;
56
57/*
58 * List all users in database
59 */
60int listusers(sasl_conn_t *conn)
61{
62    int result;
63    char key_buf[32768];
64    size_t key_len;
65    sasldb_handle dbh;
66   
67    dbh = _sasldb_getkeyhandle(sasl_global_utils, conn);
68
69    if(!dbh) {
70        printf("can't getkeyhandle\n");
71        return SASL_FAIL;
72    }
73
74    result = _sasldb_getnextkey(sasl_global_utils, dbh,
75                                key_buf, 32768, &key_len);
76
77    while (result == SASL_CONTINUE)
78    {
79        char authid_buf[16384];
80        char realm_buf[16384];
81        char property_buf[16384];
82        int ret;
83
84        ret = _sasldb_parse_key(key_buf, key_len,
85                                authid_buf, 16384,
86                                realm_buf, 16384,
87                                property_buf, 16384);
88
89        if(ret == SASL_BUFOVER) {
90            printf("Key too large\n");
91            continue;
92        } else if(ret != SASL_OK) {
93            printf("Bad Key!\n");
94            continue;
95        }
96       
97        printf("%s@%s: %s\n",authid_buf,realm_buf,property_buf);
98
99        result = _sasldb_getnextkey(sasl_global_utils, dbh,
100                                    key_buf, 32768, &key_len);
101    }
102
103    if (result == SASL_BUFOVER) {
104        fprintf(stderr, "Key too large!\n");
105    } else if (result != SASL_OK) {
106        fprintf(stderr,"db failure\n");
107    }
108
109    return _sasldb_releasekeyhandle(sasl_global_utils, dbh);
110}
111
112char *sasldb_path = SASL_DB_PATH;
113
114int good_getopt(void *context __attribute__((unused)),
115                const char *plugin_name __attribute__((unused)),
116                const char *option,
117                const char **result,
118                unsigned *len)
119{
120    if (sasldb_path && !strcmp(option, "sasldb_path")) {
121        *result = sasldb_path;
122        if (len)
123            *len = strlen(sasldb_path);
124        return SASL_OK;
125    }
126
127    return SASL_FAIL;
128}
129
130static struct sasl_callback goodsasl_cb[] = {
131    { SASL_CB_GETOPT, &good_getopt, NULL },
132    { SASL_CB_LIST_END, NULL, NULL }
133};
134
135int main(int argc, char **argv)
136{
137    int result;
138    sasl_conn_t *conn;
139    if (argc > 1)
140        sasldb_path = argv[1];
141
142    result = sasl_server_init(goodsasl_cb, "sasldblistusers");
143    if(result != SASL_OK) {
144        fprintf(stderr, "Couldn't init server\n");
145        return 1;
146    }
147   
148    result = sasl_server_new("sasldb",
149                             "localhost",
150                             NULL,
151                             NULL,
152                             NULL,
153                             NULL,
154                             0,
155                             &conn);
156
157    if(_sasl_check_db(sasl_global_utils, conn) != SASL_OK) {
158        fprintf(stderr, "check_db unsuccessful\n");
159        return 1;
160    }
161
162    if(listusers(conn) != SASL_OK) {
163        fprintf(stderr, "listusers failed\n");
164    }
165
166    sasl_dispose(&conn);
167    sasl_done();
168
169    return 0;
170}
Note: See TracBrowser for help on using the repository browser.