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