Increase version to 0.10.2
[libisds.git] / client / login.c
blob4bd5ceb42001ce1ccbfec7092cfbdac8c0d64bd7
1 #define _XOPEN_SOURCE 500
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <locale.h>
5 #include <time.h>
6 #include <string.h>
7 #include <isds.h>
8 #include "common.h"
11 int main(int argc, char **argv) {
12 struct isds_ctx *ctx = NULL;
13 isds_error err;
14 const char *request_url = url;
15 const char *request_username = username();
16 const char *request_password = password();
17 _Bool use_otp = 0;
18 struct isds_otp otp;
20 setlocale(LC_ALL, "");
22 if (argc > 1) {
23 if (argc < 4 || argc > 6) {
24 printf("Usage: %s [URL LOGIN PASSWORD [{hotp OTP_CODE} | {totp [OTP_CODE]}]\n",
25 (argv[0])?argv[0]: "");
26 exit(EXIT_FAILURE);
29 request_url = argv[1];
30 request_username = argv[2];
31 request_password = argv[3];
33 if (argc > 4) {
34 use_otp = 1;
35 if (!strcmp(argv[4], "hotp")) otp.method = OTP_HMAC;
36 else if (!strcmp(argv[4], "totp")) otp.method = OTP_TIME;
37 else {
38 printf("Bad invocation: %s: Unknown OTP method\n", argv[4]);
39 exit(EXIT_FAILURE);
41 if (argc > 5) otp.otp_code = argv[5];
42 else otp.otp_code = NULL;
47 err = isds_init();
48 if (err) {
49 printf("isds_init() failed: %s\n", isds_strerror(err));
50 exit(EXIT_FAILURE);
53 isds_set_logging(ILF_ALL, ILL_ALL);
55 ctx = isds_ctx_create();
56 if (!ctx) {
57 printf("isds_ctx_create() failed");
60 err = isds_set_timeout(ctx, 10000);
61 if (err) {
62 printf("isds_set_timeout() failed: %s\n", isds_strerror(err));
65 err = isds_login(ctx, request_url, request_username, request_password,
66 NULL, (use_otp) ? &otp : NULL);
67 if (err == IE_PARTIAL_SUCCESS) {
68 printf("isds_login() partially succeeded: %s: %s\n", isds_strerror(err),
69 isds_long_message(ctx));
70 if (use_otp && otp.resolution == OTP_RESOLUTION_TOTP_SENT)
71 printf("Redo log-in with OTP code to finish log-in procedure.\n");
73 } else if (err) {
74 printf("isds_login() failed: %s: %s\n", isds_strerror(err),
75 isds_long_message(ctx));
76 } else {
77 printf("Logged in :)\n");
81 err = isds_logout(ctx);
82 if (err) {
83 printf("isds_logout() failed: %s\n", isds_strerror(err));
87 err = isds_ctx_free(&ctx);
88 if (err) {
89 printf("isds_ctx_free() failed: %s\n", isds_strerror(err));
93 err = isds_cleanup();
94 if (err) {
95 printf("isds_cleanup() failed: %s\n", isds_strerror(err));
98 exit (EXIT_SUCCESS);