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() */
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';
21 static void terminator(int signal
) {
26 static void usage(const char *name
) {
27 printf("Usage: %s OPTIONS\n", name
);
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
) {
43 char *server_address
= NULL
;
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",
61 const struct arguments_DS_Dz_ResignISDSDocument
62 service_resigndocument_arguments
= {
63 .status_code
= "0000",
64 .status_message
= "Document re-signed successfully",
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 */
75 int last_service
= sizeof(services
)/sizeof(services
[0]) - 1;
76 struct tls_authentication tls_arguments
= {
77 .authority_certificate
= NULL
,
78 .server_certificate
= NULL
,
82 struct arguments_basic_authentication server_basic_arguments
;
83 struct arguments_otp_authentication server_otp_arguments
;
85 memset(&date
, 0, sizeof(date
));
91 while (-1 != (option
= getopt(argc
, argv
, "h:p:t:u:a:s:S:c:"))) {
108 tls_arguments
.authority_certificate
= optarg
;
111 tls_arguments
.server_certificate
= optarg
;
114 tls_arguments
.server_key
= optarg
;
117 tls_arguments
.client_name
= optarg
;
120 usage((argv
!= NULL
) ? argv
[0] : NULL
);
125 if (optind
!= argc
) {
126 fprintf(stderr
, "Superfluous argument\n");
127 usage((argv
!= NULL
) ? argv
[0] : NULL
);
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
;
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
;
157 fprintf(stderr
, "Internal error: Uknown OTP type: %c\n", otp_type
);
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");
172 if ((SIG_ERR
== signal(SIGCHLD
, terminator
))) {
173 fprintf(stderr
, "Could not register SIGCHLD handler\n");
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
,
183 error
= start_server(&server_process
, &server_address
,
184 server_otp_authentication
, &server_otp_arguments
,
188 fprintf(stderr
, "Could not start server: %s\n", server_error
);
190 free(server_address
);
193 printf("%s\n", server_address
);
194 free(server_address
);
196 printf("Waiting on SIGTERM...\n");
198 select(0, NULL
, NULL
, NULL
, NULL
);
201 printf("Terminating...\n");
202 error
= stop_server(server_process
);
204 fprintf(stderr
, "Could not stop server: %s\n", server_error
);
208 fprintf(stderr
, "Server crashed: %s\n", server_error
);
213 printf("Terminated.\n");