Fix FSF address (Tobias Mueller, #470445)
[platform/upstream/evolution-data-server.git] / servers / exchange / lib / gctest.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Copyright (C) 2002-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 /* Global Catalog test program */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <pthread.h>
27 #include <signal.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "e2k-global-catalog.h"
33 #include "e2k-sid.h"
34 #include "test-utils.h"
35
36 E2kGlobalCatalog *gc;
37 E2kOperation op;
38
39 static void
40 do_lookup (E2kGlobalCatalog *gc, const char *user)
41 {
42         E2kGlobalCatalogStatus status;
43         E2kGlobalCatalogEntry *entry;
44         E2kGlobalCatalogLookupType type;
45         guint32 flags;
46         int i, pwd_exp_days; 
47         double maxAge;
48
49         if (*user == '/')
50                 type = E2K_GLOBAL_CATALOG_LOOKUP_BY_LEGACY_EXCHANGE_DN;
51         else if (strchr (user, '@'))
52                 type = E2K_GLOBAL_CATALOG_LOOKUP_BY_EMAIL;
53         else
54                 type = E2K_GLOBAL_CATALOG_LOOKUP_BY_DN;
55
56         flags = E2K_GLOBAL_CATALOG_LOOKUP_SID |
57                 E2K_GLOBAL_CATALOG_LOOKUP_EMAIL |
58                 E2K_GLOBAL_CATALOG_LOOKUP_MAILBOX |
59                 E2K_GLOBAL_CATALOG_LOOKUP_LEGACY_EXCHANGE_DN |
60                 E2K_GLOBAL_CATALOG_LOOKUP_DELEGATES |
61                 E2K_GLOBAL_CATALOG_LOOKUP_DELEGATORS |
62                 E2K_GLOBAL_CATALOG_LOOKUP_QUOTA |
63                 E2K_GLOBAL_CATALOG_LOOKUP_ACCOUNT_CONTROL;
64
65         e2k_operation_init (&op);
66         status = e2k_global_catalog_lookup (gc, &op, type, user, flags, &entry);
67         // e2k_operation_free (&op);
68
69         switch (status) {
70         case E2K_GLOBAL_CATALOG_OK:
71                 break;
72         case E2K_GLOBAL_CATALOG_NO_SUCH_USER:
73                 printf ("No entry for %s\n", user);
74                 test_quit ();
75                 return;
76         case E2K_GLOBAL_CATALOG_NO_DATA:
77                 printf ("Entry for %s contains no data\n", user);
78                 test_quit ();
79                 return;
80         case E2K_GLOBAL_CATALOG_AUTH_FAILED:
81                 printf ("Authentication failed (try DOMAIN\\username)\n");
82                 test_quit ();
83                 return;
84         default:
85                 printf ("Error looking up user\n");
86                 test_quit ();
87                 return;
88         }
89
90         printf ("%s (%s)\n", entry->display_name, entry->dn);
91         if (entry->email)
92                 printf ("  email: %s\n", entry->email);
93         if (entry->mailbox)
94                 printf ("  mailbox: %s on %s\n", entry->mailbox, entry->exchange_server);
95         if (entry->legacy_exchange_dn)
96                 printf ("  Exchange 5.5 DN: %s\n", entry->legacy_exchange_dn);
97         if (entry->sid)
98                 printf ("  sid: %s\n", e2k_sid_get_string_sid (entry->sid));
99         if (entry->delegates) {
100                 printf ("  delegates:\n");
101                 for (i = 0; i < entry->delegates->len; i++)
102                         printf ("    %s\n", (char *)entry->delegates->pdata[i]);
103         }
104         if (entry->delegators) {
105                 printf ("  delegators:\n");
106                 for (i = 0; i < entry->delegators->len; i++)
107                         printf ("    %s\n", (char *)entry->delegators->pdata[i]);
108         }
109
110         if (entry->quota_warn || entry->quota_nosend || entry->quota_norecv )
111                 printf ("  Mail Quota Info:\n");
112         if (entry->quota_warn)  
113                 printf ("    Issue Quota warning at : %d\n", entry->quota_warn);
114         if (entry->quota_nosend)
115                 printf ("    Stop sending mails at  : %d\n", entry->quota_nosend);
116         if (entry->quota_norecv)
117                 printf ("    Stop sending and recieving mails at : %d\n", entry->quota_norecv);
118         if (entry->user_account_control)
119                 printf ("    user_account_control : %d\n", entry->user_account_control);
120         
121
122         maxAge = lookup_passwd_max_age (gc, &op);
123         printf("Password max age is %f \n", maxAge);
124         pwd_exp_days = (maxAge * 0.000000100)/86400 ;
125         printf("Password expiery period is %d \n", pwd_exp_days);
126
127         e2k_operation_free (&op);
128
129         e2k_global_catalog_entry_free (gc, entry);
130         test_quit ();
131 }
132
133 static char *
134 lookup_dn (E2kGlobalCatalog *gc, const char *id)
135 {
136         E2kGlobalCatalogEntry *entry;
137         E2kGlobalCatalogLookupType type;
138         E2kGlobalCatalogStatus status;
139         char *dn;
140
141         if (id[0] == '/')
142                 type = E2K_GLOBAL_CATALOG_LOOKUP_BY_LEGACY_EXCHANGE_DN;
143         else if (strchr (id, '@'))
144                 type = E2K_GLOBAL_CATALOG_LOOKUP_BY_EMAIL;
145         else
146                 return g_strdup (id);
147
148         e2k_operation_init (&op);
149         status = e2k_global_catalog_lookup (gc, &op, type, id, 0, &entry);
150         e2k_operation_free (&op);
151
152         switch (status) {
153         case E2K_GLOBAL_CATALOG_OK:
154                 break;
155         case E2K_GLOBAL_CATALOG_NO_SUCH_USER:
156                 printf ("No entry for %s\n", id);
157                 exit (1);
158                 break;
159         default:
160                 printf ("Error looking up user %s\n", id);
161                 exit (1);
162                 break;
163         }
164
165         dn = g_strdup (entry->dn);
166         e2k_global_catalog_entry_free (gc, entry);
167
168         return dn;
169 }
170
171 static void
172 do_modify (E2kGlobalCatalog *gc, const char *user,
173            int deleg_op, const char *delegate)
174 {
175         char *self_dn, *deleg_dn;
176         E2kGlobalCatalogStatus status;
177
178         self_dn = lookup_dn (gc, user);
179         deleg_dn = lookup_dn (gc, delegate);
180
181         e2k_operation_init (&op);
182         if (deleg_op == '+')
183                 status = e2k_global_catalog_add_delegate (gc, &op, self_dn, deleg_dn);
184         else
185                 status = e2k_global_catalog_remove_delegate (gc, &op, self_dn, deleg_dn);
186         e2k_operation_free (&op);
187
188         switch (status) {
189         case E2K_GLOBAL_CATALOG_OK:
190                 printf ("Done\n");
191                 break;
192         case E2K_GLOBAL_CATALOG_BAD_DATA:
193                 printf ("Invalid delegate DN\n");
194                 break;
195         case E2K_GLOBAL_CATALOG_NO_DATA:
196                 printf ("No such delegate to remove\n");
197                 break;
198         case E2K_GLOBAL_CATALOG_EXISTS:
199                 printf ("That user is already a delegate\n");
200                 break;
201         default:
202                 printf ("Failed\n");
203                 break;
204         }
205
206         test_quit ();
207 }
208
209 static void *
210 cancel (void *data)
211 {
212         e2k_operation_cancel (&op);
213         return NULL;
214 }
215
216 static void
217 quit (int sig)
218 {
219         static pthread_t cancel_thread;
220
221         if (!cancel_thread) {
222                 pthread_create (&cancel_thread, NULL, cancel, NULL);
223         } else
224                 exit (0);
225 }
226
227 const char *test_program_name = "gctest";
228
229 void
230 test_main (int argc, char **argv)
231 {
232         const char *server;
233
234         if (argc < 3 || argc > 4 ||
235             (argc == 4 && argv[3][0] != '+' && argv[3][0] != '-')) {
236                 fprintf (stderr, "Usage: %s server email-or-dn [[+|-]delegate]\n", argv[0]);
237                 exit (1);
238         }
239
240         signal (SIGINT, quit);
241
242         server = argv[1];
243         gc = test_get_gc (server);
244
245         if (argc == 3)
246                 do_lookup (gc, argv[2]);
247         else
248                 do_modify (gc, argv[2], argv[3][0], argv[3] + 1);
249
250         g_object_unref (gc);
251 }