MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
sockdrive.c
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/utils/sockdrive.c,v 1.31 2017/01/12 15:10:28 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  *
10  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
11  * via La Masa, 34 - 20156 Milano, Italy
12  * http://www.aero.polimi.it
13  *
14  * Changing this copyright notice is forbidden.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation (version 2 of the License).
19  *
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */
30 
31 #include "mbconfig.h" /* This goes first in every *.c,*.cc file */
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include "ac/getopt.h"
36 
37 #include <stdio.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <sys/un.h>
45 #include <netdb.h>
46 
47 #include <string.h>
48 #include <signal.h>
49 
50 #ifdef HAVE_SASL2
51 #if defined(HAVE_SASL_SASL_H)
52 #include <sasl/sasl.h>
53 #elif defined (HAVE_SASL_H)
54 #include <sasl.h>
55 #endif /* HAVE_SASL_SASL_H || HAVE_SASL_H */
56 #include "mbsasl.h"
57 #endif /* HAVE_SASL2 */
58 
59 #include "sock.h"
60 
61 const unsigned short int PORT = 5555;
62 const char *SERVERHOST = "localhost";
63 const char *SERVERPATH = "/var/mbdyn/mbdyn.sock";
64 
65 static void
66 usage(void)
67 {
68  fprintf(stderr,
69  "\n\tusage: sockdrive [h:p:D:w:Wi:I:] <label> [<value>]\n\n"
70  "\t\t-D <user>\tuser name\n"
71  "\t\t-h <host>\thost name\n"
72  "\t\t-i {yes|no}\tincremental (i.e. value[<label>] += <value>)\n"
73  "\t\t-I {yes|no}\timpulsive (reset after one step)\n"
74  "\t\t-m <mech>\tSASL mechanism (needs -S)\n"
75  "\t\t-p <port>\tport number\n"
76  "\t\t-P <path>\tpath of named pipe\n"
77  "\t\t-S\t\tenable SASL auth"
78 #ifndef HAVE_SASL2
79  " (not supported)"
80 #endif /* ! HAVE_SASL2 */
81  "\n"
82  "\t\t-w <cred>\tuser credentials\n"
83  "\t\t-W\t\tprompt for user credentials\n"
84  "\t\t<label>:\tfile drive (base 1) index to modify\n"
85  "\n"
86  "\t\t<value>:\tnew value (or increment if -i)\n\n");
87 }
88 
89  static int sasl = 0;
90 #ifdef HAVE_SASL2
91 static struct mbdyn_sasl_t mbdyn_sasl = MBDYN_SASL_INIT;
92 #endif /* HAVE_SASL2 */
93 
94 int
95 main(int argc, char *argv[])
96 {
97  int sock;
98 
99  char *path = NULL;
100  char *host = (char *)SERVERHOST;
101  unsigned short int port = PORT;
102 
103  char *user = NULL;
104  char *cred = NULL;
105  char *mech = NULL;
106 
107  int inc = 0;
108  int imp = 0;
109  char *label;
110  char *value = NULL;
111 
112  FILE *fd;
113 
114 
115  while (1) {
116  int opt;
117 
118  opt = getopt (argc, argv, "D:h:i:I:m:p:P:S:w:W");
119 
120  if (opt == EOF) {
121  break;
122  }
123 
124  switch (opt) {
125  case 'D':
126  user = optarg;
127  break;
128 
129  case 'h':
130  host = optarg;
131  break;
132 
133  case 'i':
134  if (strcasecmp(optarg, "yes") == 0) {
135  inc = 1;
136  } else if (strcasecmp(optarg, "no") == 0) {
137  inc = -1;
138  }
139  break;
140 
141  case 'I':
142  if (strcasecmp(optarg, "yes") == 0) {
143  imp = 1;
144  } else if (strcasecmp(optarg, "no") == 0) {
145  imp = -1;
146  }
147  break;
148 
149  case 'm':
150  mech = optarg;
151 #ifndef HAVE_SASL2
152  fprintf(stderr, "SASL not supported\n");
153 #endif /* ! HAVE_SASL2 */
154  break;
155 
156  case 'p':
157  port = atoi(optarg);
158  break;
159 
160  case 'P':
161  path = optarg;
162  break;
163 
164  case 'S':
165  sasl++;
166 #ifndef HAVE_SASL2
167  fprintf(stderr, "SASL not supported\n");
168  exit(EXIT_FAILURE);
169 #endif /* ! HAVE_SASL2 */
170  break;
171 
172  case 'w':
173  cred = strdup(optarg);
174  break;
175 
176  case 'W':
177  if (cred) {
178  free(cred);
179  }
180 
181  if (user) {
182  char buf[1024];
183 
184  snprintf(buf, sizeof(buf), "Password for user \"%s\": ", user);
185  cred = getpass(buf);
186 
187  } else {
188  cred = getpass("Password: ");
189  }
190 
191  if (cred) {
192  cred = strdup(cred);
193  }
194  break;
195  }
196  }
197 
198  if ((argc - optind) < 1) {
199  usage();
200  exit(EXIT_SUCCESS);
201  }
202  label = argv[optind];
203 
204  if ((argc - optind) > 1) {
205  value = argv[optind + 1];
206  }
207 
208  if (path) {
209  sock = mbdyn_make_named_socket(0, path, 0, NULL);
210  } else {
211  sock = mbdyn_make_inet_socket(0, host, port, 0, NULL);
212  }
213  if (sock < 0) {
214  fprintf(stderr, "socket initialization error\n");
215  exit(EXIT_FAILURE);
216  }
217 
218  if (sasl) {
219 #ifdef HAVE_SASL2
220  printf("initializing SASL data...\n");
221 
222  mbdyn_sasl.use_sasl = MBDYN_SASL_CLIENT;
223  mbdyn_sasl.sasl_flags = MBDYN_SASL_FLAG_CRITICAL | MBDYN_SASL_FLAG_USERAUTHZ;
224  mbdyn_sasl.sasl_mech = mech;
225  mbdyn_sasl.sasl_user = user;
226  mbdyn_sasl.sasl_cred = cred;
227  mbdyn_sasl.sasl_hostname = host;
228 
229  if (mbdyn_sasl_client_init(&mbdyn_sasl) != SASL_OK) {
230  fprintf(stderr, "SASL init failed\n");
231  exit(EXIT_FAILURE);
232  }
233 
234  if (mbdyn_sasl_client_auth(sock, NULL, &mbdyn_sasl) != SASL_OK) {
235  fprintf(stderr, "SASL auth failed\n");
236  exit(EXIT_FAILURE);
237  }
238 #endif /* HAVE_SASL2 */
239  }
240 
241  fd = fdopen(sock, "w");
242 
243  if (!sasl) {
244  if (user) {
245  fprintf(fd, "user: %s\n", user);
246  if (cred) {
247  fprintf(fd, "password: %s\n", cred);
248  memset(cred, '\0', strlen(cred));
249  }
250  }
251  }
252 
253  fprintf(fd, "label: %s\n", label);
254 
255  if (inc == 1) {
256  fprintf(fd, "inc: yes\n");
257  } else if (inc == -1) {
258  fprintf(fd, "inc: no\n");
259  }
260 
261  if (imp == 1) {
262  fprintf(fd, "imp: yes\n");
263  } else if (imp == -1) {
264  fprintf(fd, "imp: no\n");
265  }
266 
267  if (value != NULL) {
268  fprintf(fd, "value: %s\n", value);
269  }
270 
271  fprintf(fd, ".\n");
272 
273  fclose(fd);
274 
275  exit(EXIT_SUCCESS);
276 }
277 
int optind
Definition: getopt.c:72
int main(int argc, char *argv[])
Definition: sockdrive.c:95
int mbdyn_make_inet_socket(struct sockaddr_in *name, const char *hostname, unsigned short int port, int dobind, int *perror)
const char * SERVERHOST
Definition: sockdrive.c:62
const unsigned short int PORT
Definition: sockdrive.c:61
const char * host
Definition: autopilot.c:142
int mbdyn_make_named_socket(struct sockaddr_un *name, const char *path, int dobind, int *perror)
int getopt(int argc, char *const argv[], const char *opts)
Definition: getopt.c:93
char * optarg
Definition: getopt.c:74
static int sasl
Definition: sockdrive.c:89
unsigned short int port
Definition: autopilot.c:143
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
static void usage(void)
Definition: sockdrive.c:66
const char * path
Definition: autopilot.c:141
const char * SERVERPATH
Definition: sockdrive.c:63