Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / servers / exchange / lib / cptest.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Copyright (C) 2003, 2004 Novell, Inc.
4  *
5  * This  program is free  software; you  can redistribute  it and/or
6  * modify it under the terms of version 2  of the GNU General Public
7  * License as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 /* Change password test program */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30
31 #include "e2k-kerberos.h"
32 #include "test-utils.h"
33
34 const char *test_program_name = "cptest";
35
36 static void
37 krb_error (E2kKerberosResult result, const char *failed)
38 {
39         switch (result) {
40         case E2K_KERBEROS_USER_UNKNOWN:
41                 fprintf (stderr, "Unknown user\n");
42                 exit (1);
43
44         case E2K_KERBEROS_PASSWORD_INCORRECT:
45                 fprintf (stderr, "Password incorrect\n");
46                 exit (1);
47
48         case E2K_KERBEROS_PASSWORD_EXPIRED:
49                 printf ("Note: password has expired\n");
50                 break;
51
52         case E2K_KERBEROS_KDC_UNREACHABLE:
53                 fprintf (stderr, "KDC unreachable (network problem or no such domain)\n");
54                 exit (1);
55
56         case E2K_KERBEROS_TIME_SKEW:
57                 fprintf (stderr, "Client/server time skew is too large.\n");
58                 exit (1);
59
60         case E2K_KERBEROS_PASSWORD_TOO_WEAK:
61                 fprintf (stderr, "Server rejected new password\n");
62                 exit (1);
63
64         case E2K_KERBEROS_FAILED:
65                 if (failed) {
66                         fprintf (stderr, "%s\n", failed);
67                         exit (1);
68                 }
69                 /* else fall through */                 
70
71         default:
72                 fprintf (stderr, "Unknown error.\n");
73                 exit (1);
74         }
75 }
76
77 void
78 test_main (int argc, char **argv)
79 {
80         char *domain, *at, *prompt, *password;
81         char *newpass1, *newpass2;
82         const char *user;
83         int res;
84
85         if (argc != 2) {
86                 fprintf (stderr, "Usage: %s [user@]domain\n", argv[0]);
87                 exit (1);
88         }
89
90         domain = argv[1];
91         at = strchr (domain, '@');
92         if (at) {
93                 user = g_strndup (domain, at - domain);
94                 domain = at + 1;
95         } else
96                 user = g_get_user_name ();
97
98         prompt = g_strdup_printf ("Password for %s@%s", user, domain);
99         password = test_ask_password (prompt);
100         g_free (prompt);
101
102         res = e2k_kerberos_check_password (user, domain, password);
103         if (res != E2K_KERBEROS_OK)
104                 krb_error (res, NULL);
105
106         newpass1 = test_ask_password ("New password");
107         newpass2 = test_ask_password ("Confirm");
108
109         if (!newpass1 || !newpass2 || strcmp (newpass1, newpass2) != 0) {
110                 fprintf (stderr, "Passwords do not match.\n");
111                 exit (1);
112         }
113
114         res = e2k_kerberos_change_password (user, domain, password, newpass1);
115         if (res != E2K_KERBEROS_OK)
116                 krb_error (res, "Could not change password");
117
118         printf ("Password changed\n");
119         test_quit ();
120 }