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