tests: Fix headers inclusion
[libisds.git] / test / simline / server_cli.c
blob130e43978a932304ce51fc2a8abc0e28af7c9ad9
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 */
11 #include <string.h> /* memset() */
12 #include <unistd.h> /* for getopt() */
14 static const char *username = NULL;
15 static const char *password = NULL;
16 static const char *otp_code = NULL;
17 static _Bool terminate = 0;
18 static int otp_type = 'n';
21 /* Signal handler */
22 static void terminator(int signal) {
23 (void)signal;
24 terminate = 1;
27 static void usage(const char *name) {
28 printf("Usage: %s OPTIONS\n", name);
29 printf(
30 "\t-h HOTP_CODE Define HMAC-based OTP code\n"
31 "\t-p PASSWORD Define password\n"
32 "\t-t TOTP_CODE Define time-based OTP code\n"
33 "\t-u USERNAME Define user name\n"
34 "\t-a CERTIFICATE PEM-formated authority certiticate\n"
35 "\t-s CERTIFICATE PEM-formated server certificate\n"
36 "\t-S KEY PEM-formated server privat key\n"
37 "\t-c NAME Client distinguished name\n"
41 int main(int argc, char **argv) {
42 int error;
43 pid_t server_process;
44 char *server_address = NULL;
45 int option;
47 struct arguments_asws_changePassword_ChangePasswordOTP
48 service_passwdotp_arguments;
49 const struct arguments_asws_changePassword_SendSMSCode
50 service_sendsms_arguments = {
51 .status_code = "0000",
52 .status_message = "OTP code sent",
53 .reference_number = "43"
55 struct arguments_DS_DsManage_ChangeISDSPassword service_passwdbase_arguments;
56 const struct arguments_DS_Dx_EraseMessage
57 service_erasemessage_arguments = {
58 .message_id = "1234567",
59 .incoming = 1
61 struct tm date;
62 const struct arguments_DS_Dz_ResignISDSDocument
63 service_resigndocument_arguments = {
64 .status_code = "0000",
65 .status_message = "Document re-signed successfully",
66 .valid_to = &date
68 struct service_configuration services[] = {
69 { SERVICE_DS_Dx_EraseMessage, &service_erasemessage_arguments },
70 { SERVICE_DS_Dz_DummyOperation, NULL },
71 { SERVICE_DS_Dz_ResignISDSDocument, &service_resigndocument_arguments },
72 { SERVICE_END, NULL }, /* This entry could be replaced later */
73 { SERVICE_END, NULL }, /* This entry could be replaced later */
74 { SERVICE_END, NULL }
76 int last_service = sizeof(services)/sizeof(services[0]) - 1;
77 struct tls_authentication tls_arguments = {
78 .authority_certificate = NULL,
79 .server_certificate = NULL,
80 .server_key = NULL,
81 .client_name = NULL
83 struct arguments_basic_authentication server_basic_arguments;
84 struct arguments_otp_authentication server_otp_arguments;
86 memset(&date, 0, sizeof(date));
87 date.tm_year = 42;
88 date.tm_mon = 1;
89 date.tm_mday = 1;
91 /* Parse arguments */
92 while (-1 != (option = getopt(argc, argv, "h:p:t:u:a:s:S:c:"))) {
93 switch (option) {
94 case 'h':
95 otp_type = 'h';
96 otp_code = optarg;
97 break;
98 case 'p':
99 password = optarg;
100 break;
101 case 't':
102 otp_type = 't';
103 otp_code = optarg;
104 break;
105 case 'u':
106 username = optarg;
107 break;
108 case 'a':
109 tls_arguments.authority_certificate = optarg;
110 break;
111 case 's':
112 tls_arguments.server_certificate = optarg;
113 break;
114 case 'S':
115 tls_arguments.server_key = optarg;
116 break;
117 case 'c':
118 tls_arguments.client_name = optarg;
119 break;
120 default:
121 usage((argv != NULL) ? argv[0] : NULL);
122 exit(EXIT_FAILURE);
126 if (optind != argc) {
127 fprintf(stderr, "Superfluous argument\n");
128 usage((argv != NULL) ? argv[0] : NULL);
129 exit(EXIT_FAILURE);
132 /* Configure server */
133 if (otp_type == 'n') {
134 service_passwdbase_arguments.username = username;
135 service_passwdbase_arguments.current_password = password;
136 services[last_service-2].name = SERVICE_DS_DsManage_ChangeISDSPassword;
137 services[last_service-2].arguments = &service_passwdbase_arguments;
138 server_basic_arguments.username = username;
139 server_basic_arguments.password = password;
140 server_basic_arguments.isds_deviations = 1;
141 server_basic_arguments.services = services;
142 } else {
143 service_passwdotp_arguments.username = username;
144 service_passwdotp_arguments.current_password = password;
145 service_passwdotp_arguments.reference_number = "42";
146 services[last_service-2].name =
147 SERVICE_asws_changePassword_ChangePasswordOTP;
148 services[last_service-2].arguments = &service_passwdotp_arguments;
149 services[last_service-1].name =
150 SERVICE_asws_changePassword_SendSMSCode;
151 services[last_service-1].arguments = &service_sendsms_arguments;
152 server_otp_arguments.otp = otp_code;
153 if (otp_type == 't') {
154 server_otp_arguments.method = AUTH_OTP_TIME;
155 } else if (otp_type == 'h') {
156 server_otp_arguments.method = AUTH_OTP_HMAC;
157 } else {
158 fprintf(stderr, "Internal error: Uknown OTP type: %c\n", otp_type);
159 exit(EXIT_FAILURE);
161 service_passwdotp_arguments.method = server_otp_arguments.method;
162 server_otp_arguments.username = username;
163 server_otp_arguments.password = password;
164 server_otp_arguments.isds_deviations = 1;
165 server_otp_arguments.services = services;
168 /* Spawn the server */
169 if ((SIG_ERR == signal(SIGTERM, terminator))) {
170 fprintf(stderr, "Could not register SIGTERM handler\n");
171 exit(EXIT_FAILURE);
173 if ((SIG_ERR == signal(SIGCHLD, terminator))) {
174 fprintf(stderr, "Could not register SIGCHLD handler\n");
175 exit(EXIT_FAILURE);
178 printf("Starting server on:\n");
179 if (otp_type == 'n') {
180 error = start_server(&server_process, &server_address,
181 server_basic_authentication, &server_basic_arguments,
182 &tls_arguments);
183 } else {
184 error = start_server(&server_process, &server_address,
185 server_otp_authentication, &server_otp_arguments,
186 &tls_arguments);
188 if (error == -1) {
189 fprintf(stderr, "Could not start server: %s\n", server_error);
190 free(server_error);
191 free(server_address);
192 exit(EXIT_FAILURE);
194 printf("%s\n", server_address);
195 free(server_address);
197 printf("Waiting on SIGTERM...\n");
198 while (!terminate) {
199 select(0, NULL, NULL, NULL, NULL);
202 printf("Terminating...\n");
203 error = stop_server(server_process);
204 if (-1 == error) {
205 fprintf(stderr, "Could not stop server: %s\n", server_error);
206 free(server_error);
207 exit(EXIT_FAILURE);
208 } else if (error) {
209 fprintf(stderr, "Server crashed: %s\n", server_error);
210 free(server_error);
211 exit(EXIT_FAILURE);
214 printf("Terminated.\n");
215 exit(EXIT_SUCCESS);