Merge branch 'master' of ssh://moonshot.suchdamage.org:822/srv/git/cyrus-sasl
[cyrus-sasl.git] / utils / sasldblistusers.c
1 /* sasldblistusers.c -- list users in sasldb
2  * $Id: sasldblistusers.c,v 1.22 2003/10/03 20:30:14 rjs3 Exp $
3  * Rob Siemborski
4  * Tim Martin
5  */
6 /* 
7  * Copyright (c) 1998-2003 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 #ifdef HAVE_UNISTD_H
52 #include <unistd.h>
53 #endif
54
55 #include <sasl.h>
56 #include "../sasldb/sasldb.h"
57
58 #ifdef WIN32
59 #include <saslutil.h>
60 __declspec(dllimport) char *optarg;
61 __declspec(dllimport) int optind;
62 #endif
63
64 /* Cheating to make the utils work out right */
65 LIBSASL_VAR const sasl_utils_t *sasl_global_utils;
66
67 char *sasldb_path = SASL_DB_PATH;
68 const char *progname = NULL;
69
70 int good_getopt(void *context __attribute__((unused)), 
71                 const char *plugin_name __attribute__((unused)), 
72                 const char *option,
73                 const char **result,
74                 unsigned *len)
75 {
76     if (sasldb_path && !strcmp(option, "sasldb_path")) {
77         *result = sasldb_path;
78         if (len)
79             *len = strlen(sasldb_path);
80         return SASL_OK;
81     }
82
83     return SASL_FAIL;
84 }
85
86 static struct sasl_callback goodsasl_cb[] = {
87     { SASL_CB_GETOPT, &good_getopt, NULL },
88     { SASL_CB_LIST_END, NULL, NULL }
89 };
90
91 int main(int argc, char **argv)
92 {
93     int c;
94     int result;
95     sasl_conn_t *conn;
96     int bad_option = 0;
97     int display_usage = 0;
98     const char *sasl_implementation;
99     int libsasl_version;
100     int libsasl_major;
101     int libsasl_minor;
102     int libsasl_step;
103
104     if (! argv[0])
105        progname = "sasldblistusers2";
106     else {
107        progname = strrchr(argv[0], HIER_DELIMITER);
108        if (progname)
109            progname++;
110        else
111            progname = argv[0];
112     }
113
114     /* A single parameter not starting with "-" denotes sasldb to use */
115     if ((argc == 2) && argv[1][0] != '-') {
116         sasldb_path = argv[1];
117         goto START_WORK;
118     }
119
120     while ((c = getopt(argc, argv, "vf:h?")) != EOF) {
121        switch (c) {
122          case 'f':
123            sasldb_path = optarg;
124            break;
125          case 'h':
126            bad_option = 0;
127             display_usage = 1;
128            break;
129          case 'v':
130            sasl_version (&sasl_implementation, &libsasl_version);
131            libsasl_major = libsasl_version >> 24;
132            libsasl_minor = (libsasl_version >> 16) & 0xFF;
133            libsasl_step = libsasl_version & 0xFFFF;
134
135            (void)fprintf(stderr, "\nThis product includes software developed by Computing Services\n"
136                 "at Carnegie Mellon University (http://www.cmu.edu/computing/).\n\n"
137                 "Built against SASL API version %u.%u.%u\n"
138                 "LibSasl version %u.%u.%u by \"%s\"\n",
139                 SASL_VERSION_MAJOR, SASL_VERSION_MINOR, SASL_VERSION_STEP,
140                 libsasl_major, libsasl_minor, libsasl_step, sasl_implementation);
141            exit(0);
142            break;
143
144          default:
145            bad_option = 1;
146             display_usage = 1;
147             break;
148        }
149     }
150
151     if (optind != argc)
152        display_usage = 1;
153
154     if (display_usage) {
155            fprintf(stderr,
156              "\nThis product includes software developed by Computing Services\n"
157              "at Carnegie Mellon University (http://www.cmu.edu/computing/).\n\n");
158
159            fprintf(stderr, "%s: usage: %s [-v] [[-f] sasldb]\n",
160                    progname, progname);
161            fprintf(stderr, "\t-f sasldb\tuse given file as sasldb\n"
162                            "\t-v\tprint version numbers and exit\n");
163        if (bad_option) {
164            fprintf(stderr, "Unrecognized command line option\n");
165        }
166        return 1;
167     }
168  
169 START_WORK:
170     result = sasl_server_init(goodsasl_cb, "sasldblistusers");
171     if(result != SASL_OK) {
172         fprintf(stderr, "Couldn't initialize server API\n");
173         return 1;
174     }
175     
176     result = sasl_server_new("sasldb",
177                              "localhost",
178                              NULL,
179                              NULL,
180                              NULL,
181                              NULL,
182                              0,
183                              &conn);
184
185     if(_sasl_check_db(sasl_global_utils, conn) != SASL_OK) {
186         fprintf(stderr, "check_db unsuccessful\n");
187         return 1;
188     }
189
190     if(_sasldb_listusers (sasl_global_utils, conn, NULL, NULL) != SASL_OK) {
191         fprintf(stderr, "listusers failed\n");
192     }
193
194     sasl_dispose(&conn);
195     sasl_done();
196
197     return 0;
198 }