MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
mbsasltest.c
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbutil/mbsasltest.c,v 1.23 2017/01/12 14:44:05 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 /*
33  * The protocol is very simple. It is client-initiated (of course),
34  * by sending
35  *
36  * C: M
37  * S: M<length><mechanism list>
38  * C: S<length><mechanism chosen><length><additional data>
39  *
40  * S: C<length><additional data>
41  * C: C<length><additional data>
42  *
43  * S: O | F
44  *
45  * where M means "Methods", C means "Continuation" (with data),
46  * O means "OK" and F means "Fail"
47  */
48 
49 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
50 
51 #include <stdio.h>
52 #include <stdlib.h>
53 
54 #if defined(HAVE_SASL2) && defined(HAVE_THREADS) && (HAVE_SEMAPHORE_H)
55 
56 #include <string.h>
57 #include <unistd.h>
58 #include <errno.h>
59 #include <sys/mman.h>
60 #include <fcntl.h>
61 #include <signal.h>
62 #include <netdb.h>
63 #include <netinet/in.h>
64 #include <sys/types.h>
65 #include <sys/poll.h>
66 #include <sys/socket.h>
67 #include "ac/getopt.h"
68 
69 #include <sasl/sasl.h>
70 #include "mbsasl.h"
71 
72 #include "ac/pthread.h"
73 
74 static sem_t sem;
75 
76 static void *
77 server_f(void *arg)
78 {
79  int sock = ((int *)((void **)arg)[0])[0];
80  struct mbdyn_sasl_t *mbdyn_sasl = ((void **)arg)[1];
81 
82  /* server operations */
83  mbdyn_sasl_init(mbdyn_sasl);
84 
85  if (mbdyn_sasl_auth(sock, NULL, mbdyn_sasl) == SASL_OK) {
86  printf("[server] OK\n");
87  } else {
88  printf("[server] FAIL\n");
89  }
90 
91  sem_post(&sem);
92 
93  return NULL;
94 }
95 
96 int
97 main(int argc, char *argv[])
98 {
99  int sock, sockp[2];
100  int rc;
101  void *arg[2];
102  pthread_t th;
103  struct mbdyn_sasl_t mbdyn_sasl = MBDYN_SASL_INIT,
104  mbdyn_sasl_client = MBDYN_SASL_INIT,
105  mbdyn_sasl_server = MBDYN_SASL_INIT;
106 
107  while (1) {
108  int opt = getopt(argc, argv, "s:" /* MBDYN_SASL_OPTIONS */ );
109 
110  if (opt == EOF) {
111  break;
112  }
113 
114  switch (opt) {
115  case 's':
116  if (optarg[1] != '=' || mbdyn_sasl_parse_args(optarg[0], &optarg[2], &mbdyn_sasl)) {
117  printf("UNKNOWN PARAMETER '%c'\n", opt);
118  return 1;
119  }
120  break;
121 
122  default:
123  printf("usage: %s [-s{ailmrsuw}=<value>]\n"
124  "\ta=<authz> client: authorization identity (optional)\n"
125  "\tf=<flag>[=<value>]\n"
126  "\ti=<remoteip> remote ip\n"
127  "\tl=<localip> local ip\n"
128  "\tm=<method> (list of) acceptable method(s)\n"
129  "\tr=<realm> client: user realm;\n"
130  "\t server: server realm\n"
131 #if 0
132  "\ts={server|client} use SASL to negotiate auth \n"
133  "\t as server or client\n"
134 #endif
135  "\tu=<user> client: user identity\n"
136  "\tw=<cred> client: user credential\n", argv[0]);
137  exit(EXIT_SUCCESS);
138  }
139  }
140 
141  /* validate server data */
142  mbdyn_sasl_server.use_sasl = MBDYN_SASL_SERVER;
143  mbdyn_sasl_server.sasl_mech = mbdyn_sasl.sasl_mech;
144  mbdyn_sasl_server.sasl_realm = mbdyn_sasl.sasl_realm;
145 
146  if (mbdyn_sasl_validate(&mbdyn_sasl_server) != SASL_OK) {
147  fprintf(stderr, "[server] SASL DATA DID NOT VALIDATE\n");
148  return 1;
149  }
150 
151  /* validate client data */
152  mbdyn_sasl_client = mbdyn_sasl;
153  mbdyn_sasl_client.use_sasl = MBDYN_SASL_CLIENT;
154 
155  if (mbdyn_sasl_validate(&mbdyn_sasl_client) != SASL_OK) {
156  fprintf(stderr, "[client] SASL DATA DID NOT VALIDATE\n");
157  return 1;
158  }
159 
160  /* socketpair */
161  rc = socketpair(PF_LOCAL, SOCK_STREAM, 0, sockp);
162  if (rc != 0) {
163  fprintf(stderr, "[client] socketpair() failed\n");
164  exit(EXIT_FAILURE);
165  }
166  sock = sockp[0];
167 
168  /* server thread */
169  sem_init(&sem, 0, 0);
170  arg[0] = &sockp[1];
171  arg[1] = &mbdyn_sasl_server;
172  rc = pthread_create(&th, NULL, server_f, (void *)arg);
173  if (rc != 0) {
174  fprintf(stderr, "[client] pthread_create() failed\n");
175  exit(EXIT_FAILURE);
176  }
177  pthread_detach(th);
178 
179  /* client operations */
180  mbdyn_sasl_init(&mbdyn_sasl_client);
181 
182  if (mbdyn_sasl_auth(sock, NULL, &mbdyn_sasl_client) == SASL_OK) {
183  printf("[client] OK\n");
184  } else {
185  printf("[client] FAIL\n");
186  }
187 
188  /* wait for server */
189  write(sock, "Q", 1);
190  sem_wait(&sem);
191 
192  /* close the sasl session */
193  mbdyn_sasl_fini();
194 
195  return 0;
196 }
197 
198 #else /* ! defined(HAVE_SASL2) && defined(HAVE_THREADS) && (HAVE_SEMAPHORE_H) */
199 
200 int
201 main(void)
202 {
203  printf("need sasl2, pthreads and semaphores\n");
204  exit(EXIT_FAILURE);
205 }
206 
207 #endif /* ! defined(HAVE_SASL2) && defined(HAVE_THREADS) && (HAVE_SEMAPHORE_H) */
208 
209 
int getopt(int argc, char *const argv[], const char *opts)
Definition: getopt.c:93
int main(void)
Definition: mbsasltest.c:201
char * optarg
Definition: getopt.c:74