File: sasldblistusers.c

package info (click to toggle)
cyrus-sasl2 2.1.25.dfsg1-6%2Bdeb7u1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,908 kB
  • sloc: ansic: 57,016; sh: 12,558; java: 1,614; xml: 1,498; makefile: 562; perl: 199
file content (198 lines) | stat: -rw-r--r-- 5,739 bytes parent folder | download | duplicates (3)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* sasldblistusers.c -- list users in sasldb
 * $Id: sasldblistusers.c,v 1.24 2011/09/01 14:12:18 mel Exp $
 * Rob Siemborski
 * Tim Martin
 */
/* 
 * Copyright (c) 1998-2003 Carnegie Mellon University.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The name "Carnegie Mellon University" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For permission or any other legal
 *    details, please contact  
 *      Office of Technology Transfer
 *      Carnegie Mellon University
 *      5000 Forbes Avenue
 *      Pittsburgh, PA  15213-3890
 *      (412) 268-4387, fax: (412) 268-7395
 *      tech-transfer@andrew.cmu.edu
 *
 * 4. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by Computing Services
 *     at Carnegie Mellon University (http://www.cmu.edu/computing/)."
 *
 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <sasl.h>
#include "../sasldb/sasldb.h"

#ifdef WIN32
#include <saslutil.h>
__declspec(dllimport) char *optarg;
__declspec(dllimport) int optind;
#endif

/* Cheating to make the utils work out right */
LIBSASL_VAR const sasl_utils_t *sasl_global_utils;

char *sasldb_path = SASL_DB_PATH;
const char *progname = NULL;

int good_getopt(void *context __attribute__((unused)), 
		const char *plugin_name __attribute__((unused)), 
		const char *option,
		const char **result,
		unsigned *len)
{
    if (sasldb_path && !strcmp(option, "sasldb_path")) {
	*result = sasldb_path;
	if (len)
	    *len = (unsigned) strlen(sasldb_path);
	return SASL_OK;
    }

    return SASL_FAIL;
}

static struct sasl_callback goodsasl_cb[] = {
    { SASL_CB_GETOPT, (sasl_callback_ft)&good_getopt, NULL },
    { SASL_CB_LIST_END, NULL, NULL }
};

int main(int argc, char **argv)
{
    int c;
    int result;
    sasl_conn_t *conn;
    int bad_option = 0;
    int display_usage = 0;
    const char *sasl_implementation;
    int libsasl_version;
    int libsasl_major;
    int libsasl_minor;
    int libsasl_step;

    if (! argv[0])
       progname = "sasldblistusers2";
    else {
       progname = strrchr(argv[0], HIER_DELIMITER);
       if (progname)
           progname++;
       else
           progname = argv[0];
    }

    /* A single parameter not starting with "-" denotes sasldb to use */
    if ((argc == 2) && argv[1][0] != '-') {
	sasldb_path = argv[1];
	goto START_WORK;
    }

    while ((c = getopt(argc, argv, "vf:h?")) != EOF) {
       switch (c) {
         case 'f':
	   sasldb_path = optarg;
	   break;
         case 'h':
           bad_option = 0;
            display_usage = 1;
           break;
	 case 'v':
	   sasl_version (&sasl_implementation, &libsasl_version);
	   libsasl_major = libsasl_version >> 24;
	   libsasl_minor = (libsasl_version >> 16) & 0xFF;
	   libsasl_step = libsasl_version & 0xFFFF;

	   (void)fprintf(stderr, "\nThis product includes software developed by Computing Services\n"
		"at Carnegie Mellon University (http://www.cmu.edu/computing/).\n\n"
		"Built against SASL API version %u.%u.%u\n"
		"LibSasl version %u.%u.%u by \"%s\"\n",
		SASL_VERSION_MAJOR, SASL_VERSION_MINOR, SASL_VERSION_STEP,
		libsasl_major, libsasl_minor, libsasl_step, sasl_implementation);
	   exit(0);
	   break;

         default:
           bad_option = 1;
            display_usage = 1;
            break;
       }
    }

    if (optind != argc)
       display_usage = 1;

    if (display_usage) {
           fprintf(stderr,
             "\nThis product includes software developed by Computing Services\n"
             "at Carnegie Mellon University (http://www.cmu.edu/computing/).\n\n");

           fprintf(stderr, "%s: usage: %s [-v] [[-f] sasldb]\n",
		   progname, progname);
           fprintf(stderr, "\t-f sasldb\tuse given file as sasldb\n"
                           "\t-v\tprint version numbers and exit\n");
       if (bad_option) {
           fprintf(stderr, "Unrecognized command line option\n");
       }
       return 1;
    }
 
START_WORK:
    result = sasl_server_init(goodsasl_cb, "sasldblistusers");
    if(result != SASL_OK) {
	fprintf(stderr, "Couldn't initialize server API\n");
	return 1;
    }
    
    result = sasl_server_new("sasldb",
			     "localhost",
			     NULL,
			     NULL,
			     NULL,
			     NULL,
			     0,
			     &conn);

    if(_sasl_check_db(sasl_global_utils, conn) != SASL_OK) {
	fprintf(stderr, "check_db unsuccessful\n");
	return 1;
    }

    if(_sasldb_listusers (sasl_global_utils, conn, NULL, NULL) != SASL_OK) {
	fprintf(stderr, "listusers failed\n");
    }

    sasl_dispose(&conn);
    sasl_done();

    return 0;
}