test: Initialize GnuTLS in server
[libisds.git] / test / simline / server_cli.c
blob1149194107f0a43d7ce8271bf36a2a3f47d99d92
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 HOTP_CODE Define HMAC-based OTP code\n"
27 "\t-p PASSWORD Define password\n"
28 "\t-t TOTP_CODE Define time-based OTP code\n"
29 "\t-u USERNAME Define user name\n"
30 "\t-a CERTIFICATE PEM-formated authority certiticate\n"
31 "\t-s CERTIFICATE PEM-formated server certificate\n"
32 "\t-S KEY PEM-formated server privat key\n"
33 "\t-c NAME Client distinguished name\n"
37 int main(int argc, char **argv) {
38 int error;
39 pid_t server_process;
40 char *server_address = NULL;
41 int option;
43 struct arguments_asws_changePassword_ChangePasswordOTP
44 service_passwdotp_arguments;
45 const struct arguments_asws_changePassword_SendSMSCode
46 service_sendsms_arguments = {
47 .status_code = "0000",
48 .status_message = "OTP code sent",
49 .reference_number = "43"
51 struct arguments_DS_DsManage_ChangeISDSPassword service_passwdbase_arguments;
52 const struct arguments_DS_Dx_EraseMessage
53 service_erasemessage_arguments = {
54 .message_id = "1234567",
55 .incoming = 1
57 struct service_configuration services[] = {
58 { SERVICE_DS_Dx_EraseMessage, &service_erasemessage_arguments },
59 { SERVICE_DS_Dz_DummyOperation, NULL },
60 { SERVICE_END, NULL }, /* This entry could be replaced later */
61 { SERVICE_END, NULL }, /* This entry could be replaced later */
62 { SERVICE_END, NULL }
64 int last_service = sizeof(services)/sizeof(services[0]) - 1;
65 struct tls_authentication tls_arguments = {
66 .authority_certificate = NULL,
67 .server_certificate = NULL,
68 .server_key = NULL,
69 .client_name = NULL
71 struct arguments_basic_authentication server_basic_arguments;
72 struct arguments_otp_authentication server_otp_arguments;
74 /* Parse arguments */
75 while (-1 != (option = getopt(argc, argv, "h:p:t:u:a:s:S:c:"))) {
76 switch (option) {
77 case 'h':
78 otp_type = 'h';
79 otp_code = optarg;
80 break;
81 case 'p':
82 password = optarg;
83 break;
84 case 't':
85 otp_type = 't';
86 otp_code = optarg;
87 break;
88 case 'u':
89 username = optarg;
90 break;
91 case 'a':
92 tls_arguments.authority_certificate = optarg;
93 break;
94 case 's':
95 tls_arguments.server_certificate = optarg;
96 break;
97 case 'S':
98 tls_arguments.server_key = optarg;
99 break;
100 case 'c':
101 tls_arguments.client_name = optarg;
102 break;
103 default:
104 usage((argv != NULL) ? argv[0] : NULL);
105 exit(EXIT_FAILURE);
109 if (optind != argc) {
110 fprintf(stderr, "Superfluous argument\n");
111 usage((argv != NULL) ? argv[0] : NULL);
112 exit(EXIT_FAILURE);
115 /* Configure server */
116 if (otp_type == 'n') {
117 service_passwdbase_arguments.username = username;
118 service_passwdbase_arguments.current_password = password;
119 services[last_service-2].name = SERVICE_DS_DsManage_ChangeISDSPassword;
120 services[last_service-2].arguments = &service_passwdbase_arguments;
121 server_basic_arguments.username = username;
122 server_basic_arguments.password = password;
123 server_basic_arguments.isds_deviations = 1;
124 server_basic_arguments.services = services;
125 } else {
126 service_passwdotp_arguments.username = username;
127 service_passwdotp_arguments.current_password = password;
128 service_passwdotp_arguments.reference_number = "42";
129 services[last_service-2].name =
130 SERVICE_asws_changePassword_ChangePasswordOTP;
131 services[last_service-2].arguments = &service_passwdotp_arguments;
132 services[last_service-1].name =
133 SERVICE_asws_changePassword_SendSMSCode;
134 services[last_service-1].arguments = &service_sendsms_arguments;
135 server_otp_arguments.otp = otp_code;
136 if (otp_type == 't') {
137 server_otp_arguments.method = AUTH_OTP_TIME;
138 } else if (otp_type == 'h') {
139 server_otp_arguments.method = AUTH_OTP_HMAC;
141 service_passwdotp_arguments.method = server_otp_arguments.method;
142 server_otp_arguments.username = username;
143 server_otp_arguments.password = password;
144 server_otp_arguments.isds_deviations = 1;
145 server_otp_arguments.services = services;
148 /* Spawn the server */
149 if ((SIG_ERR == signal(SIGTERM, terminator))) {
150 fprintf(stderr, "Could not register SIGTERM handler\n");
151 exit(EXIT_FAILURE);
153 if ((SIG_ERR == signal(SIGCHLD, terminator))) {
154 fprintf(stderr, "Could not register SIGCHLD handler\n");
155 exit(EXIT_FAILURE);
158 printf("Starting server on:\n");
159 if (otp_type == 'n') {
160 error = start_server(&server_process, &server_address,
161 server_basic_authentication, &server_basic_arguments,
162 &tls_arguments);
163 } else {
164 error = start_server(&server_process, &server_address,
165 server_otp_authentication, &server_otp_arguments,
166 &tls_arguments);
168 if (error == -1) {
169 fprintf(stderr, "Could not start server\n");
170 free(server_address);
171 exit(EXIT_FAILURE);
173 printf("http://%s/\n", server_address);
174 free(server_address);
176 printf("Waiting on SIGTERM...\n");
177 while (!terminate) {
178 select(0, NULL, NULL, NULL, NULL);
181 printf("Terminating...\n");
182 if (-1 == stop_server(server_process)) {
183 fprintf(stderr, "Could not stop server: %s\n", server_error);
184 exit(EXIT_FAILURE);
187 printf("Terminated.\n");
188 exit(EXIT_SUCCESS);