Merge ssh://repo.or.cz/srv/git/libisds
[libisds.git] / client / resetpassword.c
blobab38e78852257edd15e9490d175035ca3b3169f2
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"
10 void reset_password(struct isds_ctx *ctx,
11 const struct isds_DbOwnerInfo *box, const struct isds_DbUserInfo *user,
12 const _Bool paid, char **token) {
13 char *refnumber = NULL;
15 printf("Resetting password\n");
16 isds_error err = isds_reset_password(ctx, box, user, paid, NULL, token,
17 &refnumber);
18 if (err) {
19 printf("isds_reset_password() failed: %s: %s\n",
20 isds_strerror(err), isds_long_message(ctx));
21 } else {
22 printf("isds_reset_password() succeeded as request #%s.\n"
23 "This should not happen\n", refnumber);
24 if (token) printf("token = %s\n", *token);
26 printf("\n");
27 free(refnumber);
30 int main(int argc, char **argv) {
31 struct isds_ctx *ctx = NULL;
32 isds_error err;
33 struct isds_DbOwnerInfo *db_owner_info = NULL;
34 struct isds_DbUserInfo *db_user_info = NULL;
36 setlocale(LC_ALL, "");
38 err = isds_init();
39 if (err) {
40 printf("isds_init() failed: %s\n", isds_strerror(err));
41 exit(EXIT_FAILURE);
44 isds_set_logging(ILF_ALL & ~ILF_HTTP, ILL_ALL);
46 ctx = isds_ctx_create();
47 if (!ctx) {
48 printf("isds_ctx_create() failed");
51 err = isds_set_timeout(ctx, 10000);
52 if (err) {
53 printf("isds_set_timeout() failed: %s\n", isds_strerror(err));
56 /* err = isds_set_tls(ctx, ITLS_VERIFY_SERVER, 0);
57 if (err) {
58 printf("isds_set_tls(ITLS_VERIFY_SERVER) failed: %s\n",
59 isds_strerror(err));
62 err = isds_set_tls(ctx, ITLS_CA_FILE, "/etc/ssl/certs/ca-certificates.crt");
63 if (err) {
64 printf("isds_set_tls(ITLS_CA_FILE) failed: %s\n",
65 isds_strerror(err));
66 }*/
68 err = isds_login(ctx, url, username, password, NULL);
69 if (err) {
70 printf("isds_login() failed: %s: %s\n", isds_strerror(err),
71 isds_long_message(ctx));
72 } else {
73 printf("Logged in :)\n");
78 /* Get info about my box */
79 printf("Getting info about my box:\n");
80 err = isds_GetOwnerInfoFromLogin(ctx, &db_owner_info);
81 if (err) {
82 printf("isds_GetOwnerInfoFromLogin() failed: %s: %s\n",
83 isds_strerror(err), isds_long_message(ctx));
84 } else {
85 printf("isds_GetOwnerInfoFromLogin() succeeded\n");
87 print_DbOwnerInfo(db_owner_info);
91 /* Get info about my account */
92 printf("Getting info about my account:\n");
93 err = isds_GetUserInfoFromLogin(ctx, &db_user_info);
94 if (err) {
95 printf("isds_GetUserInfoFromLogin() failed: %s: %s\n",
96 isds_strerror(err), isds_long_message(ctx));
97 } else {
98 printf("isds_GetUserInfoFromLogin() succeeded\n");
99 print_DbUserInfo(db_user_info);
103 if (db_owner_info && db_user_info) {
104 char *token = NULL;
105 /* Try some invalid invocation that should fail */
106 #if 0
107 reset_password(ctx, db_owner_info, db_user_info, 0, NULL);
108 reset_password(ctx, db_owner_info, db_user_info, 1, NULL);
109 reset_password(ctx, db_owner_info, db_user_info, 1, &token);
110 #endif
111 fprintf(stderr, "This function can lose current user credentials. "
112 "DO NOT TRY IT!\n");
113 free(token);
116 isds_DbOwnerInfo_free(&db_owner_info);
117 isds_DbUserInfo_free(&db_user_info);
120 err = isds_logout(ctx);
121 if (err) {
122 printf("isds_logout() failed: %s\n", isds_strerror(err));
126 err = isds_ctx_free(&ctx);
127 if (err) {
128 printf("isds_ctx_free() failed: %s\n", isds_strerror(err));
132 err = isds_cleanup();
133 if (err) {
134 printf("isds_cleanup() failed: %s\n", isds_strerror(err));
137 exit (EXIT_SUCCESS);