2 #define _XOPEN_SOURCE /* For getopt(3) */
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';
22 static void terminator(int signal
) {
27 static void usage(const char *name
) {
28 printf("Usage: %s OPTIONS\n", name
);
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
) {
44 char *server_address
= NULL
;
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",
62 const struct arguments_DS_Dz_ResignISDSDocument
63 service_resigndocument_arguments
= {
64 .status_code
= "0000",
65 .status_message
= "Document re-signed successfully",
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 */
76 int last_service
= sizeof(services
)/sizeof(services
[0]) - 1;
77 struct tls_authentication tls_arguments
= {
78 .authority_certificate
= NULL
,
79 .server_certificate
= NULL
,
83 struct arguments_basic_authentication server_basic_arguments
;
84 struct arguments_otp_authentication server_otp_arguments
;
86 memset(&date
, 0, sizeof(date
));
92 while (-1 != (option
= getopt(argc
, argv
, "h:p:t:u:a:s:S:c:"))) {
109 tls_arguments
.authority_certificate
= optarg
;
112 tls_arguments
.server_certificate
= optarg
;
115 tls_arguments
.server_key
= optarg
;
118 tls_arguments
.client_name
= optarg
;
121 usage((argv
!= NULL
) ? argv
[0] : NULL
);
126 if (optind
!= argc
) {
127 fprintf(stderr
, "Superfluous argument\n");
128 usage((argv
!= NULL
) ? argv
[0] : NULL
);
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
;
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
;
158 fprintf(stderr
, "Internal error: Uknown OTP type: %c\n", otp_type
);
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");
173 if ((SIG_ERR
== signal(SIGCHLD
, terminator
))) {
174 fprintf(stderr
, "Could not register SIGCHLD handler\n");
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
,
184 error
= start_server(&server_process
, &server_address
,
185 server_otp_authentication
, &server_otp_arguments
,
189 fprintf(stderr
, "Could not start server: %s\n", server_error
);
191 free(server_address
);
194 printf("%s\n", server_address
);
195 free(server_address
);
197 printf("Waiting on SIGTERM...\n");
199 select(0, NULL
, NULL
, NULL
, NULL
);
202 printf("Terminating...\n");
203 error
= stop_server(server_process
);
205 fprintf(stderr
, "Could not stop server: %s\n", server_error
);
209 fprintf(stderr
, "Server crashed: %s\n", server_error
);
214 printf("Terminated.\n");