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