test: server_cli tool added
[libisds.git] / test / simline / server_cli.c
blobc57bfe6005645731b94139774db34f87124ba839
1 #ifndef _XOPEN_SOURCE
2 #define _XOPEN_SOURCE /* For getopt(3) */
3 #endif
5 #include "server.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <signal.h>
9 #include <sys/select.h>
10 #include <sys/types.h> /* For pid_t */
12 static const char *username = NULL;
13 static const char *password = NULL;
14 static const char *otp_code = NULL;
15 static _Bool terminate = 0;
16 static int otp_type = 'n';
19 static void terminator(int signal) {
20 terminate = 1;
23 static void usage(const char *name) {
24 printf("Usage: %s OPTIONS\n", name);
25 printf(
26 "\t-h TOTP_CODE\n"
27 "\t-p PASSWORD\n"
28 "\t-t TOTP_CODE\n"
29 "\t-u USERNAME\n"
33 int main(int argc, char **argv) {
34 int error;
35 pid_t server_process;
36 char *server_address = NULL;
37 int option;
39 struct arguments_asws_changePassword_ChangePasswordOTP service_passwdotp_arguments;
40 struct arguments_DS_DsManage_ChangeISDSPassword service_passwdbase_arguments;
41 struct service_configuration services[] = {
42 { SERVICE_END, NULL }, /* This entry will be replaced later */
43 { SERVICE_DS_Dz_DummyOperation, NULL },
44 { SERVICE_END, NULL }
46 struct arguments_basic_authentication server_basic_arguments;
47 struct arguments_otp_authentication server_otp_arguments;
49 /* Parse arguments */
50 while (-1 != (option = getopt(argc, argv, "h:p:t:u:"))) {
51 switch (option) {
52 case 'h':
53 otp_type = 'h';
54 otp_code = optarg;
55 break;
56 case 'p':
57 password = optarg;
58 break;
59 case 't':
60 otp_type = 't';
61 otp_code = optarg;
62 break;
63 case 'u':
64 username = optarg;
65 break;
66 default:
67 usage((argv != NULL) ? argv[0] : NULL);
68 exit(EXIT_FAILURE);
72 if (optind != argc) {
73 fprintf(stderr, "Superfluous argument\n");
74 usage((argv != NULL) ? argv[0] : NULL);
75 exit(EXIT_FAILURE);
78 /* Configure server */
79 if (otp_type == 'n') {
80 service_passwdbase_arguments.username = username;
81 service_passwdbase_arguments.current_password = password;
82 services[0].name = SERVICE_DS_DsManage_ChangeISDSPassword;
83 services[0].arguments = &service_passwdbase_arguments;
84 server_basic_arguments.username = username;
85 server_basic_arguments.password = password;
86 server_basic_arguments.isds_deviations = 1;
87 server_basic_arguments.services = services;
88 } else {
89 service_passwdotp_arguments.username = username;
90 service_passwdotp_arguments.current_password = password;
91 services[0].name = SERVICE_asws_changePassword_ChangePasswordOTP;
92 services[0].arguments = &service_passwdotp_arguments;
93 server_otp_arguments.otp = otp_code;
94 if (otp_type == 't') {
95 server_otp_arguments.method = AUTH_OTP_TIME;
96 } else if (otp_type == 'h') {
97 server_otp_arguments.method = AUTH_OTP_HMAC;
99 server_otp_arguments.username = username;
100 server_otp_arguments.password = password;
101 server_otp_arguments.isds_deviations = 1;
102 server_otp_arguments.services = services;
105 /* Spawn the server */
106 if ((SIG_ERR == signal(SIGTERM, terminator))) {
107 fprintf(stderr, "Could not register SIGTERM handler\n");
108 exit(EXIT_FAILURE);
110 if ((SIG_ERR == signal(SIGCHLD, terminator))) {
111 fprintf(stderr, "Could not register SIGCHLD handler\n");
112 exit(EXIT_FAILURE);
115 printf("Starting server on:\n");
116 if (otp_type == 'n') {
117 error = start_server(&server_process, &server_address,
118 server_basic_authentication, &server_basic_arguments);
119 } else {
120 error = start_server(&server_process, &server_address,
121 server_otp_authentication, &server_otp_arguments);
123 if (error == -1) {
124 fprintf(stderr, "Could not start server\n");
125 free(server_address);
126 exit(EXIT_FAILURE);
128 printf("http://%s/\n", server_address);
129 free(server_address);
131 printf("Waiting on SIGTERM...\n");
132 while (!terminate) {
133 select(0, NULL, NULL, NULL, NULL);
136 printf("Terminating...\n");
137 if (-1 == stop_server(server_process)) {
138 fprintf(stderr, "Could not stop server: %s\n", server_error);
139 exit(EXIT_FAILURE);
142 printf("Terminated.\n");
143 exit(EXIT_SUCCESS);