1*ebfedea0SLionel Sambuc /* $NetBSD: example_client.c,v 1.4 2011/02/12 23:21:33 christos Exp $ */
2*ebfedea0SLionel Sambuc
3*ebfedea0SLionel Sambuc /* Copyright (c) 2010 The NetBSD Foundation, Inc.
4*ebfedea0SLionel Sambuc * All rights reserved.
5*ebfedea0SLionel Sambuc *
6*ebfedea0SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
7*ebfedea0SLionel Sambuc * by Mateusz Kocielski.
8*ebfedea0SLionel Sambuc *
9*ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10*ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11*ebfedea0SLionel Sambuc * are met:
12*ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14*ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
15*ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
16*ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
17*ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
18*ebfedea0SLionel Sambuc * must display the following acknowledgement:
19*ebfedea0SLionel Sambuc * This product includes software developed by the NetBSD
20*ebfedea0SLionel Sambuc * Foundation, Inc. and its contributors.
21*ebfedea0SLionel Sambuc * 4. Neither the name of The NetBSD Foundation nor the names of its
22*ebfedea0SLionel Sambuc * contributors may be used to endorse or promote products derived
23*ebfedea0SLionel Sambuc * from this software without specific prior written permission.
24*ebfedea0SLionel Sambuc *
25*ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26*ebfedea0SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27*ebfedea0SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28*ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29*ebfedea0SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30*ebfedea0SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31*ebfedea0SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32*ebfedea0SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33*ebfedea0SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34*ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35*ebfedea0SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
36*ebfedea0SLionel Sambuc */
37*ebfedea0SLionel Sambuc #include <sys/cdefs.h>
38*ebfedea0SLionel Sambuc __RCSID("$NetBSD: example_client.c,v 1.4 2011/02/12 23:21:33 christos Exp $");
39*ebfedea0SLionel Sambuc
40*ebfedea0SLionel Sambuc #include <err.h>
41*ebfedea0SLionel Sambuc #include <limits.h>
42*ebfedea0SLionel Sambuc #include <saslc.h>
43*ebfedea0SLionel Sambuc #include <stdio.h>
44*ebfedea0SLionel Sambuc #include <stdlib.h>
45*ebfedea0SLionel Sambuc #include <string.h>
46*ebfedea0SLionel Sambuc #include <unistd.h>
47*ebfedea0SLionel Sambuc
48*ebfedea0SLionel Sambuc static void
print_help(void)49*ebfedea0SLionel Sambuc print_help(void)
50*ebfedea0SLionel Sambuc {
51*ebfedea0SLionel Sambuc
52*ebfedea0SLionel Sambuc printf("usage: [-hl] {-m mech_name}\n");
53*ebfedea0SLionel Sambuc printf("-h - help\n");
54*ebfedea0SLionel Sambuc printf("-l - mechanisms list\n");
55*ebfedea0SLionel Sambuc printf("-m - use mech_name mechanism\n");
56*ebfedea0SLionel Sambuc }
57*ebfedea0SLionel Sambuc
58*ebfedea0SLionel Sambuc static void
list_mechanisms(void)59*ebfedea0SLionel Sambuc list_mechanisms(void)
60*ebfedea0SLionel Sambuc {
61*ebfedea0SLionel Sambuc
62*ebfedea0SLionel Sambuc printf("available mechanisms:\n");
63*ebfedea0SLionel Sambuc printf("ANONYMOUS, CRAM-MD5, DIGEST-MD5, GSSAPI, EXTERNAL, LOGIN, "
64*ebfedea0SLionel Sambuc "PLAIN\n");
65*ebfedea0SLionel Sambuc }
66*ebfedea0SLionel Sambuc
67*ebfedea0SLionel Sambuc static char *
nextline(char * buf,size_t len,FILE * fp)68*ebfedea0SLionel Sambuc nextline(char *buf, size_t len, FILE *fp)
69*ebfedea0SLionel Sambuc {
70*ebfedea0SLionel Sambuc char *p;
71*ebfedea0SLionel Sambuc
72*ebfedea0SLionel Sambuc if (fgets(buf, len, fp) == NULL)
73*ebfedea0SLionel Sambuc return NULL;
74*ebfedea0SLionel Sambuc
75*ebfedea0SLionel Sambuc if ((p = strchr(buf, '\n')) != NULL)
76*ebfedea0SLionel Sambuc *p = '\0';
77*ebfedea0SLionel Sambuc
78*ebfedea0SLionel Sambuc return buf;
79*ebfedea0SLionel Sambuc }
80*ebfedea0SLionel Sambuc
81*ebfedea0SLionel Sambuc int
main(int argc,char ** argv)82*ebfedea0SLionel Sambuc main(int argc, char **argv)
83*ebfedea0SLionel Sambuc {
84*ebfedea0SLionel Sambuc int opt, n, cont;
85*ebfedea0SLionel Sambuc char *mechanism = NULL;
86*ebfedea0SLionel Sambuc saslc_t *ctx;
87*ebfedea0SLionel Sambuc saslc_sess_t *sess;
88*ebfedea0SLionel Sambuc char input[LINE_MAX];
89*ebfedea0SLionel Sambuc char *option, *var;
90*ebfedea0SLionel Sambuc char *output;
91*ebfedea0SLionel Sambuc static char empty[] = "";
92*ebfedea0SLionel Sambuc size_t input_len, output_len;
93*ebfedea0SLionel Sambuc
94*ebfedea0SLionel Sambuc while ((opt = getopt(argc, argv, "hm:l")) != -1) {
95*ebfedea0SLionel Sambuc switch (opt) {
96*ebfedea0SLionel Sambuc case 'm':
97*ebfedea0SLionel Sambuc /* mechanism */
98*ebfedea0SLionel Sambuc mechanism = optarg;
99*ebfedea0SLionel Sambuc break;
100*ebfedea0SLionel Sambuc case 'l':
101*ebfedea0SLionel Sambuc /* list mechanisms */
102*ebfedea0SLionel Sambuc list_mechanisms();
103*ebfedea0SLionel Sambuc return EXIT_SUCCESS;
104*ebfedea0SLionel Sambuc case 'h':
105*ebfedea0SLionel Sambuc default:
106*ebfedea0SLionel Sambuc /* ??? */
107*ebfedea0SLionel Sambuc print_help();
108*ebfedea0SLionel Sambuc return EXIT_FAILURE;
109*ebfedea0SLionel Sambuc }
110*ebfedea0SLionel Sambuc }
111*ebfedea0SLionel Sambuc
112*ebfedea0SLionel Sambuc if (mechanism == NULL) {
113*ebfedea0SLionel Sambuc printf("mechanism: ");
114*ebfedea0SLionel Sambuc if (nextline(input, sizeof(input), stdin) == NULL)
115*ebfedea0SLionel Sambuc goto eof;
116*ebfedea0SLionel Sambuc mechanism = input;
117*ebfedea0SLionel Sambuc }
118*ebfedea0SLionel Sambuc
119*ebfedea0SLionel Sambuc ctx = saslc_alloc();
120*ebfedea0SLionel Sambuc
121*ebfedea0SLionel Sambuc if (saslc_init(ctx, NULL, NULL) < 0)
122*ebfedea0SLionel Sambuc goto error;
123*ebfedea0SLionel Sambuc
124*ebfedea0SLionel Sambuc if ((sess = saslc_sess_init(ctx, mechanism, NULL)) == NULL)
125*ebfedea0SLionel Sambuc goto error;
126*ebfedea0SLionel Sambuc
127*ebfedea0SLionel Sambuc /* reading properties */
128*ebfedea0SLionel Sambuc if (nextline(input, sizeof(input), stdin) == NULL)
129*ebfedea0SLionel Sambuc goto eof;
130*ebfedea0SLionel Sambuc n = atoi(input);
131*ebfedea0SLionel Sambuc
132*ebfedea0SLionel Sambuc while (n--) {
133*ebfedea0SLionel Sambuc if (nextline(input, sizeof(input), stdin) == NULL)
134*ebfedea0SLionel Sambuc goto eof;
135*ebfedea0SLionel Sambuc var = strchr(input, ' ');
136*ebfedea0SLionel Sambuc if (var != NULL)
137*ebfedea0SLionel Sambuc *var++ = '\0';
138*ebfedea0SLionel Sambuc else
139*ebfedea0SLionel Sambuc var = empty;
140*ebfedea0SLionel Sambuc option = input;
141*ebfedea0SLionel Sambuc if (saslc_sess_setprop(sess, option, var) < 0)
142*ebfedea0SLionel Sambuc goto error;
143*ebfedea0SLionel Sambuc }
144*ebfedea0SLionel Sambuc
145*ebfedea0SLionel Sambuc printf("session:\n");
146*ebfedea0SLionel Sambuc
147*ebfedea0SLionel Sambuc for (;;) {
148*ebfedea0SLionel Sambuc if (nextline(input, sizeof(input), stdin) == NULL)
149*ebfedea0SLionel Sambuc break;
150*ebfedea0SLionel Sambuc input_len = strlen(input);
151*ebfedea0SLionel Sambuc cont = saslc_sess_cont(sess, input, input_len, (void **)&output,
152*ebfedea0SLionel Sambuc &output_len);
153*ebfedea0SLionel Sambuc if (cont < 0)
154*ebfedea0SLionel Sambuc goto error_sess;
155*ebfedea0SLionel Sambuc printf("%s\n", output == NULL ? "empty line" : output);
156*ebfedea0SLionel Sambuc if (cont == 0)
157*ebfedea0SLionel Sambuc break;
158*ebfedea0SLionel Sambuc }
159*ebfedea0SLionel Sambuc
160*ebfedea0SLionel Sambuc saslc_sess_end(sess);
161*ebfedea0SLionel Sambuc if (saslc_end(ctx) < 0)
162*ebfedea0SLionel Sambuc goto error;
163*ebfedea0SLionel Sambuc
164*ebfedea0SLionel Sambuc return 0;
165*ebfedea0SLionel Sambuc eof:
166*ebfedea0SLionel Sambuc err(EXIT_FAILURE, "Unexpected EOF");
167*ebfedea0SLionel Sambuc error:
168*ebfedea0SLionel Sambuc errx(EXIT_FAILURE, "%s", saslc_strerror(ctx));
169*ebfedea0SLionel Sambuc error_sess:
170*ebfedea0SLionel Sambuc errx(EXIT_FAILURE, "%s", saslc_sess_strerror(sess));
171*ebfedea0SLionel Sambuc }
172