2003-03-31 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-auth.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-auth.c Authentication
3  *
4  * Copyright (C) 2002, 2003 Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 #include "dbus-auth.h"
24 #include "dbus-string.h"
25 #include "dbus-list.h"
26 #include "dbus-internals.h"
27 #include "dbus-keyring.h"
28 #include "dbus-sha.h"
29
30 /* See doc/dbus-sasl-profile.txt */
31
32 /**
33  * @defgroup DBusAuth Authentication
34  * @ingroup  DBusInternals
35  * @brief DBusAuth object
36  *
37  * DBusAuth manages the authentication negotiation when a connection
38  * is first established, and also manage any encryption used over a
39  * connection.
40  *
41  * The file doc/dbus-sasl-profile.txt documents the network protocol
42  * used for authentication.
43  *
44  * @todo some SASL profiles require sending the empty string as a
45  * challenge/response, but we don't currently allow that in our
46  * protocol.
47  *
48  * @todo DBusAuth really needs to be rewritten as an explicit state
49  * machine. Right now it's too hard to prove to yourself by inspection
50  * that it works.
51  *
52  * @todo right now sometimes both ends will block waiting for input
53  * from the other end, e.g. if there's an error during
54  * DBUS_COOKIE_SHA1.
55  *
56  * @todo the cookie keyring needs to be cached globally not just
57  * per-auth (which raises threadsafety issues too)
58  * 
59  * @todo grep FIXME in dbus-auth.c
60  */
61
62 /**
63  * @defgroup DBusAuthInternals Authentication implementation details
64  * @ingroup  DBusInternals
65  * @brief DBusAuth implementation details
66  *
67  * Private details of authentication code.
68  *
69  * @{
70  */
71
72 /**
73  * Processes a command. Returns whether we had enough memory to
74  * complete the operation.
75  */
76 typedef dbus_bool_t (* DBusProcessAuthCommandFunction) (DBusAuth         *auth,
77                                                         const DBusString *command,
78                                                         const DBusString *args);
79
80 typedef struct
81 {
82   const char *command;
83   DBusProcessAuthCommandFunction func;
84 } DBusAuthCommandHandler;
85
86 /**
87  * This function appends an initial client response to the given string
88  */
89 typedef dbus_bool_t (* DBusInitialResponseFunction)  (DBusAuth         *auth,
90                                                       DBusString       *response);
91
92 /**
93  * This function processes a block of data received from the peer.
94  * i.e. handles a DATA command.
95  */
96 typedef dbus_bool_t (* DBusAuthDataFunction)     (DBusAuth         *auth,
97                                                   const DBusString *data);
98
99 /**
100  * This function encodes a block of data from the peer.
101  */
102 typedef dbus_bool_t (* DBusAuthEncodeFunction)   (DBusAuth         *auth,
103                                                   const DBusString *data,
104                                                   DBusString       *encoded);
105
106 /**
107  * This function decodes a block of data from the peer.
108  */
109 typedef dbus_bool_t (* DBusAuthDecodeFunction)   (DBusAuth         *auth,
110                                                   const DBusString *data,
111                                                   DBusString       *decoded);
112
113 /**
114  * This function is called when the mechanism is abandoned.
115  */
116 typedef void        (* DBusAuthShutdownFunction) (DBusAuth       *auth);
117
118 typedef struct
119 {
120   const char *mechanism;
121   DBusAuthDataFunction server_data_func;
122   DBusAuthEncodeFunction server_encode_func;
123   DBusAuthDecodeFunction server_decode_func;
124   DBusAuthShutdownFunction server_shutdown_func;
125   DBusInitialResponseFunction client_initial_response_func;
126   DBusAuthDataFunction client_data_func;
127   DBusAuthEncodeFunction client_encode_func;
128   DBusAuthDecodeFunction client_decode_func;
129   DBusAuthShutdownFunction client_shutdown_func;
130 } DBusAuthMechanismHandler;
131
132 /**
133  * Internal members of DBusAuth.
134  */
135 struct DBusAuth
136 {
137   int refcount;           /**< reference count */
138
139   DBusString incoming;    /**< Incoming data buffer */
140   DBusString outgoing;    /**< Outgoing data buffer */
141   
142   const DBusAuthCommandHandler *handlers; /**< Handlers for commands */
143
144   const DBusAuthMechanismHandler *mech;   /**< Current auth mechanism */
145
146   DBusString identity;                   /**< Current identity we're authorizing
147                                           *   as.
148                                           */
149   
150   DBusCredentials credentials;      /**< Credentials read from socket,
151                                      * fields may be -1
152                                      */
153
154   DBusCredentials authorized_identity; /**< Credentials that are authorized */
155
156   DBusCredentials desired_identity;    /**< Identity client has requested */
157   
158   DBusString context;               /**< Cookie scope */
159   DBusKeyring *keyring;             /**< Keyring for cookie mechanism. */
160   int cookie_id;                    /**< ID of cookie to use */
161   DBusString challenge;             /**< Challenge sent to client */
162   
163   unsigned int needed_memory : 1;   /**< We needed memory to continue since last
164                                      * successful getting something done
165                                      */
166   unsigned int need_disconnect : 1; /**< We've given up, time to disconnect */
167   unsigned int authenticated : 1;   /**< We are authenticated */
168   unsigned int authenticated_pending_output : 1; /**< Authenticated once we clear outgoing buffer */
169   unsigned int authenticated_pending_begin : 1;  /**< Authenticated once we get BEGIN */
170   unsigned int already_got_mechanisms : 1;       /**< Client already got mech list */
171   unsigned int already_asked_for_initial_response : 1; /**< Already sent a blank challenge to get an initial response */
172   unsigned int buffer_outstanding : 1; /**< Buffer is "checked out" for reading data into */
173 };
174
175 typedef struct
176 {
177   DBusAuth base;
178
179   DBusList *mechs_to_try; /**< Mechanisms we got from the server that we're going to try using */
180   
181 } DBusAuthClient;
182
183 typedef struct
184 {
185   DBusAuth base;
186
187   int failures;     /**< Number of times client has been rejected */
188   int max_failures; /**< Number of times we reject before disconnect */
189   
190 } DBusAuthServer;
191
192 static dbus_bool_t process_auth         (DBusAuth         *auth,
193                                          const DBusString *command,
194                                          const DBusString *args);
195 static dbus_bool_t process_cancel       (DBusAuth         *auth,
196                                          const DBusString *command,
197                                          const DBusString *args);
198 static dbus_bool_t process_begin        (DBusAuth         *auth,
199                                          const DBusString *command,
200                                          const DBusString *args);
201 static dbus_bool_t process_data_server  (DBusAuth         *auth,
202                                          const DBusString *command,
203                                          const DBusString *args);
204 static dbus_bool_t process_error_server (DBusAuth         *auth,
205                                          const DBusString *command,
206                                          const DBusString *args);
207 static dbus_bool_t process_rejected     (DBusAuth         *auth,
208                                          const DBusString *command,
209                                          const DBusString *args);
210 static dbus_bool_t process_ok           (DBusAuth         *auth,
211                                          const DBusString *command,
212                                          const DBusString *args);
213 static dbus_bool_t process_data_client  (DBusAuth         *auth,
214                                          const DBusString *command,
215                                          const DBusString *args);
216 static dbus_bool_t process_error_client (DBusAuth         *auth,
217                                          const DBusString *command,
218                                          const DBusString *args);
219
220
221 static dbus_bool_t client_try_next_mechanism (DBusAuth *auth);
222 static dbus_bool_t send_rejected             (DBusAuth *auth);
223
224 static DBusAuthCommandHandler
225 server_handlers[] = {
226   { "AUTH", process_auth },
227   { "CANCEL", process_cancel },
228   { "BEGIN", process_begin },
229   { "DATA", process_data_server },
230   { "ERROR", process_error_server },
231   { NULL, NULL }
232 };
233
234 static DBusAuthCommandHandler
235 client_handlers[] = {
236   { "REJECTED", process_rejected },
237   { "OK", process_ok },
238   { "DATA", process_data_client },
239   { "ERROR", process_error_client },
240   { NULL, NULL }
241 };
242
243 /**
244  * @param auth the auth conversation
245  * @returns #TRUE if the conversation is the server side
246  */
247 #define DBUS_AUTH_IS_SERVER(auth) ((auth)->handlers == server_handlers)
248 /**
249  * @param auth the auth conversation
250  * @returns #TRUE if the conversation is the client side
251  */
252 #define DBUS_AUTH_IS_CLIENT(auth) ((auth)->handlers == client_handlers)
253 /**
254  * @param auth the auth conversation
255  * @returns auth cast to DBusAuthClient
256  */
257 #define DBUS_AUTH_CLIENT(auth)    ((DBusAuthClient*)(auth))
258 /**
259  * @param auth the auth conversation
260  * @returns auth cast to DBusAuthServer
261  */
262 #define DBUS_AUTH_SERVER(auth)    ((DBusAuthServer*)(auth))
263
264 static DBusAuth*
265 _dbus_auth_new (int size)
266 {
267   DBusAuth *auth;
268   
269   auth = dbus_malloc0 (size);
270   if (auth == NULL)
271     return NULL;
272   
273   auth->refcount = 1;
274
275   auth->credentials.pid = -1;
276   auth->credentials.uid = -1;
277   auth->credentials.gid = -1;
278
279   auth->authorized_identity.pid = -1;
280   auth->authorized_identity.uid = -1;
281   auth->authorized_identity.gid = -1;
282
283   auth->desired_identity.pid = -1;
284   auth->desired_identity.uid = -1;
285   auth->desired_identity.gid = -1;
286   
287   auth->keyring = NULL;
288   auth->cookie_id = -1;
289   
290   /* note that we don't use the max string length feature,
291    * because you can't use that feature if you're going to
292    * try to recover from out-of-memory (it creates
293    * what looks like unrecoverable inability to alloc
294    * more space in the string). But we do handle
295    * overlong buffers in _dbus_auth_do_work().
296    */
297   
298   if (!_dbus_string_init (&auth->incoming))
299     goto enomem_0;
300
301   if (!_dbus_string_init (&auth->outgoing))
302     goto enomem_1;
303     
304   if (!_dbus_string_init (&auth->identity))
305     goto enomem_2;
306
307   if (!_dbus_string_init (&auth->context))
308     goto enomem_3;
309
310   if (!_dbus_string_init (&auth->challenge))
311     goto enomem_4;
312
313   /* default context if none is specified */
314   if (!_dbus_string_append (&auth->context, "org_freedesktop_general"))
315     goto enomem_5;
316   
317   return auth;
318
319  enomem_5:
320   _dbus_string_free (&auth->challenge);
321  enomem_4:
322   _dbus_string_free (&auth->context);
323  enomem_3:
324   _dbus_string_free (&auth->identity);
325  enomem_2:
326   _dbus_string_free (&auth->outgoing);
327  enomem_1:
328   _dbus_string_free (&auth->incoming);
329  enomem_0:
330   dbus_free (auth);
331   return NULL;
332 }
333
334 static void
335 shutdown_mech (DBusAuth *auth)
336 {
337   /* Cancel any auth */
338   auth->authenticated_pending_begin = FALSE;
339   auth->authenticated = FALSE;
340   auth->already_asked_for_initial_response = FALSE;
341   _dbus_string_set_length (&auth->identity, 0);
342   
343   auth->authorized_identity.pid = -1;
344   auth->authorized_identity.uid = -1;
345   auth->authorized_identity.gid = -1;
346
347   auth->desired_identity.pid = -1;
348   auth->desired_identity.uid = -1;
349   auth->desired_identity.gid = -1;
350   
351   if (auth->mech != NULL)
352     {
353       _dbus_verbose ("Shutting down mechanism %s\n",
354                      auth->mech->mechanism);
355       
356       if (DBUS_AUTH_IS_CLIENT (auth))
357         (* auth->mech->client_shutdown_func) (auth);
358       else
359         (* auth->mech->server_shutdown_func) (auth);
360       
361       auth->mech = NULL;
362     }
363 }
364
365 /* Returns TRUE but with an empty string hash if the
366  * cookie_id isn't known. As with all this code
367  * TRUE just means we had enough memory.
368  */
369 static dbus_bool_t
370 sha1_compute_hash (DBusAuth         *auth,
371                    int               cookie_id,
372                    const DBusString *server_challenge,
373                    const DBusString *client_challenge,
374                    DBusString       *hash)
375 {
376   DBusString cookie;
377   DBusString to_hash;
378   dbus_bool_t retval;
379   
380   _dbus_assert (auth->keyring != NULL);
381
382   retval = FALSE;
383   
384   if (!_dbus_string_init (&cookie))
385     return FALSE;
386
387   if (!_dbus_keyring_get_hex_key (auth->keyring, cookie_id,
388                                   &cookie))
389     goto out_0;
390
391   if (_dbus_string_get_length (&cookie) == 0)
392     {
393       retval = TRUE;
394       goto out_0;
395     }
396
397   if (!_dbus_string_init (&to_hash))
398     goto out_0;
399   
400   if (!_dbus_string_copy (server_challenge, 0,
401                           &to_hash, _dbus_string_get_length (&to_hash)))
402     goto out_1;
403
404   if (!_dbus_string_append (&to_hash, ":"))
405     goto out_1;
406   
407   if (!_dbus_string_copy (client_challenge, 0,
408                           &to_hash, _dbus_string_get_length (&to_hash)))
409     goto out_1;
410
411   if (!_dbus_string_append (&to_hash, ":"))
412     goto out_1;
413
414   if (!_dbus_string_copy (&cookie, 0,
415                           &to_hash, _dbus_string_get_length (&to_hash)))
416     goto out_1;
417
418   if (!_dbus_sha_compute (&to_hash, hash))
419     goto out_1;
420   
421   retval = TRUE;
422
423  out_1:
424   _dbus_string_zero (&to_hash);
425   _dbus_string_free (&to_hash);
426  out_0:
427   _dbus_string_zero (&cookie);
428   _dbus_string_free (&cookie);
429   return retval;
430 }
431
432 /** http://www.ietf.org/rfc/rfc2831.txt suggests at least 64 bits of
433  * entropy, we use 128. This is the number of bytes in the random
434  * challenge.
435  */
436 #define N_CHALLENGE_BYTES (128/8)
437
438 static dbus_bool_t
439 sha1_handle_first_client_response (DBusAuth         *auth,
440                                    const DBusString *data)
441 {
442   /* We haven't sent a challenge yet, we're expecting a desired
443    * username from the client.
444    */
445   DBusString tmp;
446   DBusString tmp2;
447   dbus_bool_t retval;
448   int old_len;
449   DBusError error;
450   
451   retval = FALSE;
452
453   _dbus_string_set_length (&auth->challenge, 0);
454   
455   if (_dbus_string_get_length (data) > 0)
456     {
457       if (_dbus_string_get_length (&auth->identity) > 0)
458         {
459           /* Tried to send two auth identities, wtf */
460           _dbus_verbose ("client tried to send auth identity, but we already have one\n");
461           return send_rejected (auth);
462         }
463       else
464         {
465           /* this is our auth identity */
466           if (!_dbus_string_copy (data, 0, &auth->identity, 0))
467             return FALSE;
468         }
469     }
470       
471   if (!_dbus_credentials_from_username (data, &auth->desired_identity))
472     {
473       _dbus_verbose ("Did not get a valid username from client\n");
474       return send_rejected (auth);
475     }
476       
477   if (!_dbus_string_init (&tmp))
478     return FALSE;
479
480   if (!_dbus_string_init (&tmp2))
481     {
482       _dbus_string_free (&tmp);
483       return FALSE;
484     }
485
486   old_len = _dbus_string_get_length (&auth->outgoing);
487   
488   /* we cache the keyring for speed, so here we drop it if it's the
489    * wrong one. FIXME caching the keyring here is useless since we use
490    * a different DBusAuth for every connection.
491    */
492   if (auth->keyring &&
493       !_dbus_keyring_is_for_user (auth->keyring,
494                                   data))
495     {
496       _dbus_keyring_unref (auth->keyring);
497       auth->keyring = NULL;
498     }
499   
500   if (auth->keyring == NULL)
501     {
502       DBusError error;
503
504       dbus_error_init (&error);
505       auth->keyring = _dbus_keyring_new_homedir (data,
506                                                  &auth->context,
507                                                  &error);
508
509       if (auth->keyring == NULL)
510         {
511           if (dbus_error_has_name (&error,
512                                    DBUS_ERROR_NO_MEMORY))
513             {
514               dbus_error_free (&error);
515               goto out;
516             }
517           else
518             {
519               _DBUS_ASSERT_ERROR_IS_SET (&error);
520               _dbus_verbose ("Error loading keyring: %s\n",
521                              error.message);
522               if (send_rejected (auth))
523                 retval = TRUE; /* retval is only about mem */
524               dbus_error_free (&error);
525               goto out;
526             }
527         }
528       else
529         {
530           _dbus_assert (!dbus_error_is_set (&error));
531         }
532     }
533
534   _dbus_assert (auth->keyring != NULL);
535
536   dbus_error_init (&error);
537   auth->cookie_id = _dbus_keyring_get_best_key (auth->keyring, &error);
538   if (auth->cookie_id < 0)
539     {
540       _DBUS_ASSERT_ERROR_IS_SET (&error);
541       _dbus_verbose ("Could not get a cookie ID to send to client: %s\n",
542                      error.message);
543       if (send_rejected (auth))
544         retval = TRUE;
545       dbus_error_free (&error);
546       goto out;
547     }
548   else
549     {
550       _dbus_assert (!dbus_error_is_set (&error));
551     }
552
553   if (!_dbus_string_copy (&auth->context, 0,
554                           &tmp2, _dbus_string_get_length (&tmp2)))
555     goto out;
556
557   if (!_dbus_string_append (&tmp2, " "))
558     goto out;
559
560   if (!_dbus_string_append_int (&tmp2, auth->cookie_id))
561     goto out;
562
563   if (!_dbus_string_append (&tmp2, " "))
564     goto out;  
565   
566   if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
567     goto out;
568
569   _dbus_string_set_length (&auth->challenge, 0);
570   if (!_dbus_string_hex_encode (&tmp, 0, &auth->challenge, 0))
571     goto out;
572   
573   if (!_dbus_string_hex_encode (&tmp, 0, &tmp2,
574                                 _dbus_string_get_length (&tmp2)))
575     goto out;
576
577   if (!_dbus_string_append (&auth->outgoing,
578                             "DATA "))
579     goto out;
580   
581   if (!_dbus_string_base64_encode (&tmp2, 0, &auth->outgoing,
582                                    _dbus_string_get_length (&auth->outgoing)))
583     goto out;
584
585   if (!_dbus_string_append (&auth->outgoing,
586                             "\r\n"))
587     goto out;
588       
589   retval = TRUE;
590   
591  out:
592   _dbus_string_zero (&tmp);
593   _dbus_string_free (&tmp);
594   _dbus_string_zero (&tmp2);
595   _dbus_string_free (&tmp2);
596   if (!retval)
597     _dbus_string_set_length (&auth->outgoing, old_len);
598   return retval;
599 }
600
601 static dbus_bool_t
602 sha1_handle_second_client_response (DBusAuth         *auth,
603                                     const DBusString *data)
604 {
605   /* We are expecting a response which is the hex-encoded client
606    * challenge, space, then SHA-1 hash of the concatenation of our
607    * challenge, ":", client challenge, ":", secret key, all
608    * hex-encoded.
609    */
610   int i;
611   DBusString client_challenge;
612   DBusString client_hash;
613   dbus_bool_t retval;
614   DBusString correct_hash;
615   
616   retval = FALSE;
617   
618   if (!_dbus_string_find_blank (data, 0, &i))
619     {
620       _dbus_verbose ("no space separator in client response\n");
621       return send_rejected (auth);
622     }
623   
624   if (!_dbus_string_init (&client_challenge))
625     goto out_0;
626
627   if (!_dbus_string_init (&client_hash))
628     goto out_1;  
629
630   if (!_dbus_string_copy_len (data, 0, i, &client_challenge,
631                               0))
632     goto out_2;
633
634   _dbus_string_skip_blank (data, i, &i);
635   
636   if (!_dbus_string_copy_len (data, i,
637                               _dbus_string_get_length (data) - i,
638                               &client_hash,
639                               0))
640     goto out_2;
641
642   if (_dbus_string_get_length (&client_challenge) == 0 ||
643       _dbus_string_get_length (&client_hash) == 0)
644     {
645       _dbus_verbose ("zero-length client challenge or hash\n");
646       if (send_rejected (auth))
647         retval = TRUE;
648       goto out_2;
649     }
650
651   if (!_dbus_string_init (&correct_hash))
652     goto out_2;
653
654   if (!sha1_compute_hash (auth, auth->cookie_id,
655                           &auth->challenge, 
656                           &client_challenge,
657                           &correct_hash))
658     goto out_3;
659
660   /* if cookie_id was invalid, then we get an empty hash */
661   if (_dbus_string_get_length (&correct_hash) == 0)
662     {
663       if (send_rejected (auth))
664         retval = TRUE;
665       goto out_3;
666     }
667   
668   if (!_dbus_string_equal (&client_hash, &correct_hash))
669     {
670       if (send_rejected (auth))
671         retval = TRUE;
672       goto out_3;
673     }
674       
675   if (!_dbus_string_append (&auth->outgoing,
676                             "OK\r\n"))
677     goto out_3;
678
679   _dbus_verbose ("authenticated client with UID %d using DBUS_COOKIE_SHA1\n",
680                  auth->desired_identity.uid);
681   
682   auth->authorized_identity = auth->desired_identity;
683   auth->authenticated_pending_begin = TRUE;
684   retval = TRUE;
685   
686  out_3:
687   _dbus_string_zero (&correct_hash);
688   _dbus_string_free (&correct_hash);
689  out_2:
690   _dbus_string_zero (&client_hash);
691   _dbus_string_free (&client_hash);
692  out_1:
693   _dbus_string_free (&client_challenge);
694  out_0:
695   return retval;
696 }
697
698 static dbus_bool_t
699 handle_server_data_cookie_sha1_mech (DBusAuth         *auth,
700                                      const DBusString *data)
701 {
702   if (auth->cookie_id < 0)
703     return sha1_handle_first_client_response (auth, data);
704   else
705     return sha1_handle_second_client_response (auth, data);
706 }
707
708 static void
709 handle_server_shutdown_cookie_sha1_mech (DBusAuth *auth)
710 {
711   auth->cookie_id = -1;  
712   _dbus_string_set_length (&auth->challenge, 0);
713 }
714
715 static dbus_bool_t
716 handle_client_initial_response_cookie_sha1_mech (DBusAuth   *auth,
717                                                  DBusString *response)
718 {
719   const DBusString *username;
720   dbus_bool_t retval;
721
722   retval = FALSE;
723
724   if (!_dbus_user_info_from_current_process (&username,
725                                              NULL, NULL))
726     goto out_0;
727
728   if (!_dbus_string_base64_encode (username, 0,
729                                    response,
730                                    _dbus_string_get_length (response)))
731     goto out_0;
732
733   retval = TRUE;
734   
735  out_0:
736   return retval;
737 }
738
739 /* FIXME if we send the server an error, right now both sides
740  * just hang. Server has to reject on getting an error, or
741  * client has to cancel. Should be in the spec.
742  */
743 static dbus_bool_t
744 handle_client_data_cookie_sha1_mech (DBusAuth         *auth,
745                                      const DBusString *data)
746 {
747   /* The data we get from the server should be the cookie context
748    * name, the cookie ID, and the server challenge, separated by
749    * spaces. We send back our challenge string and the correct hash.
750    */
751   dbus_bool_t retval;
752   DBusString context;
753   DBusString cookie_id_str;
754   DBusString server_challenge;
755   DBusString client_challenge;
756   DBusString correct_hash;
757   DBusString tmp;
758   int i, j;
759   long val;
760   int old_len;
761   
762   retval = FALSE;                 
763   
764   if (!_dbus_string_find_blank (data, 0, &i))
765     {
766       if (_dbus_string_append (&auth->outgoing,
767                                "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
768         retval = TRUE;
769       goto out_0;
770     }
771
772   if (!_dbus_string_init (&context))
773     goto out_0;
774
775   if (!_dbus_string_copy_len (data, 0, i,
776                               &context, 0))
777     goto out_1;
778   
779   _dbus_string_skip_blank (data, i, &i);
780   if (!_dbus_string_find_blank (data, i, &j))
781     {
782       if (_dbus_string_append (&auth->outgoing,
783                                "ERROR \"Server did not send context/ID/challenge properly\"\r\n"))
784         retval = TRUE;
785       goto out_1;
786     }
787
788   if (!_dbus_string_init (&cookie_id_str))
789     goto out_1;
790   
791   if (!_dbus_string_copy_len (data, i, j - i,
792                               &cookie_id_str, 0))
793     goto out_2;  
794
795   if (!_dbus_string_init (&server_challenge))
796     goto out_2;
797
798   i = j;
799   _dbus_string_skip_blank (data, i, &i);
800   j = _dbus_string_get_length (data);
801
802   if (!_dbus_string_copy_len (data, i, j - i,
803                               &server_challenge, 0))
804     goto out_3;
805
806   if (!_dbus_keyring_validate_context (&context))
807     {
808       if (_dbus_string_append (&auth->outgoing,
809                                "ERROR \"Server sent invalid cookie context\"\r\n"))
810         retval = TRUE;
811       goto out_3;
812     }
813
814   if (!_dbus_string_parse_int (&cookie_id_str, 0, &val, NULL))
815     {
816       if (_dbus_string_append (&auth->outgoing,
817                                "ERROR \"Could not parse cookie ID as an integer\"\r\n"))
818         retval = TRUE;
819       goto out_3;
820     }
821
822   if (_dbus_string_get_length (&server_challenge) == 0)
823     {
824       if (_dbus_string_append (&auth->outgoing,
825                                "ERROR \"Empty server challenge string\"\r\n"))
826         retval = TRUE;
827       goto out_3;
828     }
829
830   if (auth->keyring == NULL)
831     {
832       DBusError error;
833
834       dbus_error_init (&error);
835       auth->keyring = _dbus_keyring_new_homedir (NULL,
836                                                  &context,
837                                                  &error);
838
839       if (auth->keyring == NULL)
840         {
841           if (dbus_error_has_name (&error,
842                                    DBUS_ERROR_NO_MEMORY))
843             {
844               dbus_error_free (&error);
845               goto out_3;
846             }
847           else
848             {
849               _DBUS_ASSERT_ERROR_IS_SET (&error);
850
851               _dbus_verbose ("Error loading keyring: %s\n",
852                              error.message);
853               
854               if (_dbus_string_append (&auth->outgoing,
855                                        "ERROR \"Could not load cookie file\"\r\n"))
856                 retval = TRUE; /* retval is only about mem */
857               
858               dbus_error_free (&error);
859               goto out_3;
860             }
861         }
862       else
863         {
864           _dbus_assert (!dbus_error_is_set (&error));
865         }
866     }
867   
868   _dbus_assert (auth->keyring != NULL);
869   
870   if (!_dbus_string_init (&tmp))
871     goto out_3;
872   
873   if (!_dbus_generate_random_bytes (&tmp, N_CHALLENGE_BYTES))
874     goto out_4;
875
876   if (!_dbus_string_init (&client_challenge))
877     goto out_4;
878
879   if (!_dbus_string_hex_encode (&tmp, 0, &client_challenge, 0))
880     goto out_5;
881
882   if (!_dbus_string_init (&correct_hash))
883     goto out_6;
884   
885   if (!sha1_compute_hash (auth, val,
886                           &server_challenge,
887                           &client_challenge,
888                           &correct_hash))
889     goto out_6;
890
891   if (_dbus_string_get_length (&correct_hash) == 0)
892     {
893       /* couldn't find the cookie ID or something */
894       if (_dbus_string_append (&auth->outgoing,
895                                "ERROR \"Don't have the requested cookie ID\"\r\n"))
896         retval = TRUE;
897       goto out_6;
898     }
899   
900   _dbus_string_set_length (&tmp, 0);
901   
902   if (!_dbus_string_copy (&client_challenge, 0, &tmp,
903                           _dbus_string_get_length (&tmp)))
904     goto out_6;
905
906   if (!_dbus_string_append (&tmp, " "))
907     goto out_6;
908
909   if (!_dbus_string_copy (&correct_hash, 0, &tmp,
910                           _dbus_string_get_length (&tmp)))
911     goto out_6;
912
913   old_len = _dbus_string_get_length (&auth->outgoing);
914   if (!_dbus_string_append (&auth->outgoing, "DATA "))
915     goto out_6;
916
917   if (!_dbus_string_base64_encode (&tmp, 0,
918                                    &auth->outgoing,
919                                    _dbus_string_get_length (&auth->outgoing)))
920     {
921       _dbus_string_set_length (&auth->outgoing, old_len);
922       goto out_6;
923     }
924
925   if (!_dbus_string_append (&auth->outgoing, "\r\n"))
926     {
927       _dbus_string_set_length (&auth->outgoing, old_len);
928       goto out_6;
929     }
930   
931   retval = TRUE;
932
933  out_6:
934   _dbus_string_zero (&correct_hash);
935   _dbus_string_free (&correct_hash);
936  out_5:
937   _dbus_string_free (&client_challenge);
938  out_4:
939   _dbus_string_zero (&tmp);
940   _dbus_string_free (&tmp);
941  out_3:
942   _dbus_string_free (&server_challenge);
943  out_2:
944   _dbus_string_free (&cookie_id_str);
945  out_1:
946   _dbus_string_free (&context);
947  out_0:
948   return retval;
949 }
950
951 static void
952 handle_client_shutdown_cookie_sha1_mech (DBusAuth *auth)
953 {
954   auth->cookie_id = -1;  
955   _dbus_string_set_length (&auth->challenge, 0);
956 }
957
958 static dbus_bool_t
959 handle_server_data_external_mech (DBusAuth         *auth,
960                                   const DBusString *data)
961 {
962   if (auth->credentials.uid < 0)
963     {
964       _dbus_verbose ("no credentials, mechanism EXTERNAL can't authenticate\n");
965       return send_rejected (auth);
966     }
967   
968   if (_dbus_string_get_length (data) > 0)
969     {
970       if (_dbus_string_get_length (&auth->identity) > 0)
971         {
972           /* Tried to send two auth identities, wtf */
973           _dbus_verbose ("client tried to send auth identity, but we already have one\n");
974           return send_rejected (auth);
975         }
976       else
977         {
978           /* this is our auth identity */
979           if (!_dbus_string_copy (data, 0, &auth->identity, 0))
980             return FALSE;
981         }
982     }
983
984   /* Poke client for an auth identity, if none given */
985   if (_dbus_string_get_length (&auth->identity) == 0 &&
986       !auth->already_asked_for_initial_response)
987     {
988       if (_dbus_string_append (&auth->outgoing,
989                                "DATA\r\n"))
990         {
991           _dbus_verbose ("sending empty challenge asking client for auth identity\n");
992           auth->already_asked_for_initial_response = TRUE;
993           return TRUE;
994         }
995       else
996         return FALSE;
997     }
998
999   auth->desired_identity.pid = -1;
1000   auth->desired_identity.uid = -1;
1001   auth->desired_identity.gid = -1;
1002   
1003   /* If auth->identity is still empty here, then client
1004    * responded with an empty string after we poked it for
1005    * an initial response. This means to try to auth the
1006    * identity provided in the credentials.
1007    */
1008   if (_dbus_string_get_length (&auth->identity) == 0)
1009     {
1010       auth->desired_identity.uid = auth->credentials.uid;
1011     }
1012   else
1013     {
1014       if (!_dbus_credentials_from_uid_string (&auth->identity,
1015                                               &auth->desired_identity))
1016         {
1017           _dbus_verbose ("could not get credentials from uid string\n");
1018           return send_rejected (auth);
1019         }
1020     }
1021
1022   if (auth->desired_identity.uid < 0)
1023     {
1024       _dbus_verbose ("desired UID %d is no good\n", auth->desired_identity.uid);
1025       return send_rejected (auth);
1026     }
1027   
1028   if (_dbus_credentials_match (&auth->desired_identity,
1029                                &auth->credentials))
1030     {
1031       /* client has authenticated */      
1032       if (!_dbus_string_append (&auth->outgoing,
1033                                 "OK\r\n"))
1034         return FALSE;
1035
1036       _dbus_verbose ("authenticated client with UID %d matching socket credentials UID %d\n",
1037                      auth->desired_identity.uid,
1038                      auth->credentials.uid);
1039       
1040       auth->authorized_identity.uid = auth->desired_identity.uid;
1041       
1042       auth->authenticated_pending_begin = TRUE;
1043       
1044       return TRUE;
1045     }
1046   else
1047     {
1048       _dbus_verbose ("credentials uid=%d gid=%d do not allow uid=%d gid=%d\n",
1049                      auth->credentials.uid, auth->credentials.gid,
1050                      auth->desired_identity.uid, auth->desired_identity.gid);
1051       return send_rejected (auth);
1052     }
1053 }
1054
1055 static void
1056 handle_server_shutdown_external_mech (DBusAuth *auth)
1057 {
1058
1059 }
1060
1061 static dbus_bool_t
1062 handle_client_initial_response_external_mech (DBusAuth         *auth,
1063                                               DBusString       *response)
1064 {
1065   /* We always append our UID as an initial response, so the server
1066    * doesn't have to send back an empty challenge to check whether we
1067    * want to specify an identity. i.e. this avoids a round trip that
1068    * the spec for the EXTERNAL mechanism otherwise requires.
1069    */
1070   DBusString plaintext;
1071
1072   if (!_dbus_string_init (&plaintext))
1073     return FALSE;
1074   
1075   if (!_dbus_string_append_our_uid (&plaintext))
1076     goto failed;
1077
1078   if (!_dbus_string_base64_encode (&plaintext, 0,
1079                                    response,
1080                                    _dbus_string_get_length (response)))
1081     goto failed;
1082
1083   _dbus_string_free (&plaintext);
1084   
1085   return TRUE;
1086
1087  failed:
1088   _dbus_string_free (&plaintext);
1089   return FALSE;  
1090 }
1091
1092 static dbus_bool_t
1093 handle_client_data_external_mech (DBusAuth         *auth,
1094                                   const DBusString *data)
1095 {
1096   
1097   return TRUE;
1098 }
1099
1100 static void
1101 handle_client_shutdown_external_mech (DBusAuth *auth)
1102 {
1103
1104 }
1105
1106 /* Put mechanisms here in order of preference.
1107  * What I eventually want to have is:
1108  *
1109  *  - a mechanism that checks UNIX domain socket credentials
1110  *  - a simple magic cookie mechanism like X11 or ICE
1111  *  - mechanisms that chain to Cyrus SASL, so we can use anything it
1112  *    offers such as Kerberos, X509, whatever.
1113  * 
1114  */
1115 static const DBusAuthMechanismHandler
1116 all_mechanisms[] = {
1117   { "EXTERNAL",
1118     handle_server_data_external_mech,
1119     NULL, NULL,
1120     handle_server_shutdown_external_mech,
1121     handle_client_initial_response_external_mech,
1122     handle_client_data_external_mech,
1123     NULL, NULL,
1124     handle_client_shutdown_external_mech },
1125   { "DBUS_COOKIE_SHA1",
1126     handle_server_data_cookie_sha1_mech,
1127     NULL, NULL,
1128     handle_server_shutdown_cookie_sha1_mech,
1129     handle_client_initial_response_cookie_sha1_mech,
1130     handle_client_data_cookie_sha1_mech,
1131     NULL, NULL,
1132     handle_client_shutdown_cookie_sha1_mech },
1133   { NULL, NULL }
1134 };
1135
1136 static const DBusAuthMechanismHandler*
1137 find_mech (const DBusString *name)
1138 {
1139   int i;
1140   
1141   i = 0;
1142   while (all_mechanisms[i].mechanism != NULL)
1143     {
1144       if (_dbus_string_equal_c_str (name,
1145                                     all_mechanisms[i].mechanism))
1146
1147         return &all_mechanisms[i];
1148       
1149       ++i;
1150     }
1151   
1152   return NULL;
1153 }
1154
1155 static dbus_bool_t
1156 send_rejected (DBusAuth *auth)
1157 {
1158   DBusString command;
1159   DBusAuthServer *server_auth;
1160   int i;
1161   
1162   if (!_dbus_string_init (&command))
1163     return FALSE;
1164   
1165   if (!_dbus_string_append (&command,
1166                             "REJECTED"))
1167     goto nomem;
1168
1169   i = 0;
1170   while (all_mechanisms[i].mechanism != NULL)
1171     {
1172       if (!_dbus_string_append (&command,
1173                                 " "))
1174         goto nomem;
1175
1176       if (!_dbus_string_append (&command,
1177                                 all_mechanisms[i].mechanism))
1178         goto nomem;
1179       
1180       ++i;
1181     }
1182   
1183   if (!_dbus_string_append (&command, "\r\n"))
1184     goto nomem;
1185
1186   if (!_dbus_string_copy (&command, 0, &auth->outgoing,
1187                           _dbus_string_get_length (&auth->outgoing)))
1188     goto nomem;
1189
1190   shutdown_mech (auth);
1191   
1192   _dbus_assert (DBUS_AUTH_IS_SERVER (auth));
1193   server_auth = DBUS_AUTH_SERVER (auth);
1194   server_auth->failures += 1;
1195
1196   _dbus_string_free (&command);
1197   
1198   return TRUE;
1199
1200  nomem:
1201   _dbus_string_free (&command);
1202   return FALSE;
1203 }
1204
1205 static dbus_bool_t
1206 process_auth (DBusAuth         *auth,
1207               const DBusString *command,
1208               const DBusString *args)
1209 {
1210   if (auth->mech)
1211     {
1212       /* We are already using a mechanism, client is on crack */
1213       if (!_dbus_string_append (&auth->outgoing,
1214                                 "ERROR \"Sent AUTH while another AUTH in progress\"\r\n"))
1215         return FALSE;
1216
1217       return TRUE;
1218     }
1219   else if (_dbus_string_get_length (args) == 0)
1220     {
1221       /* No args to the auth, send mechanisms */
1222       if (!send_rejected (auth))
1223         return FALSE;
1224
1225       return TRUE;
1226     }
1227   else
1228     {
1229       int i;
1230       DBusString mech;
1231       DBusString base64_response;
1232       DBusString decoded_response;
1233       
1234       _dbus_string_find_blank (args, 0, &i);
1235
1236       if (!_dbus_string_init (&mech))
1237         return FALSE;
1238
1239       if (!_dbus_string_init (&base64_response))
1240         {
1241           _dbus_string_free (&mech);
1242           return FALSE;
1243         }
1244       
1245       if (!_dbus_string_init (&decoded_response))
1246         {
1247           _dbus_string_free (&mech);
1248           _dbus_string_free (&base64_response);
1249           return FALSE;
1250         }
1251
1252       if (!_dbus_string_copy_len (args, 0, i, &mech, 0))
1253         goto failed;
1254
1255       if (!_dbus_string_copy (args, i, &base64_response, 0))
1256         goto failed;
1257
1258       if (!_dbus_string_base64_decode (&base64_response, 0,
1259                                        &decoded_response, 0))
1260         goto failed;
1261       
1262       auth->mech = find_mech (&mech);
1263       if (auth->mech != NULL)
1264         {
1265           _dbus_verbose ("Trying mechanism %s with initial response of %d bytes\n",
1266                          auth->mech->mechanism,
1267                          _dbus_string_get_length (&decoded_response));
1268           
1269           if (!(* auth->mech->server_data_func) (auth,
1270                                                  &decoded_response))
1271             goto failed;
1272         }
1273       else
1274         {
1275           /* Unsupported mechanism */
1276           if (!send_rejected (auth))
1277             goto failed;
1278         }
1279
1280       _dbus_string_free (&mech);      
1281       _dbus_string_free (&base64_response);
1282       _dbus_string_free (&decoded_response);
1283
1284       return TRUE;
1285       
1286     failed:
1287       auth->mech = NULL;
1288       _dbus_string_free (&mech);
1289       _dbus_string_free (&base64_response);
1290       _dbus_string_free (&decoded_response);
1291       return FALSE;
1292     }
1293 }
1294
1295 static dbus_bool_t
1296 process_cancel (DBusAuth         *auth,
1297                 const DBusString *command,
1298                 const DBusString *args)
1299 {
1300   shutdown_mech (auth);
1301   
1302   return TRUE;
1303 }
1304
1305 static dbus_bool_t
1306 process_begin (DBusAuth         *auth,
1307                const DBusString *command,
1308                const DBusString *args)
1309 {
1310   if (auth->authenticated_pending_begin)
1311     auth->authenticated = TRUE;
1312   else
1313     {
1314       auth->need_disconnect = TRUE; /* client trying to send data before auth,
1315                                      * kick it
1316                                      */
1317       shutdown_mech (auth);
1318     }
1319   
1320   return TRUE;
1321 }
1322
1323 static dbus_bool_t
1324 process_data_server (DBusAuth         *auth,
1325                      const DBusString *command,
1326                      const DBusString *args)
1327 {
1328   if (auth->mech != NULL)
1329     {
1330       DBusString decoded;
1331
1332       if (!_dbus_string_init (&decoded))
1333         return FALSE;
1334
1335       if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1336         {
1337           _dbus_string_free (&decoded);
1338           return FALSE;
1339         }
1340
1341 #ifdef DBUS_ENABLE_VERBOSE_MODE
1342       if (_dbus_string_validate_ascii (&decoded, 0,
1343                                        _dbus_string_get_length (&decoded)))
1344         _dbus_verbose ("data: '%s'\n", _dbus_string_get_const_data (&decoded));
1345 #endif
1346       
1347       if (!(* auth->mech->server_data_func) (auth, &decoded))
1348         {
1349           _dbus_string_free (&decoded);
1350           return FALSE;
1351         }
1352
1353       _dbus_string_free (&decoded);
1354     }
1355   else
1356     {
1357       if (!_dbus_string_append (&auth->outgoing,
1358                                 "ERROR \"Not currently in an auth conversation\"\r\n"))
1359         return FALSE;
1360     }
1361   
1362   return TRUE;
1363 }
1364
1365 static dbus_bool_t
1366 process_error_server (DBusAuth         *auth,
1367                       const DBusString *command,
1368                       const DBusString *args)
1369 {
1370   
1371   return TRUE;
1372 }
1373
1374 /* return FALSE if no memory, TRUE if all OK */
1375 static dbus_bool_t
1376 get_word (const DBusString *str,
1377           int              *start,
1378           DBusString       *word)
1379 {
1380   int i;
1381
1382   _dbus_string_skip_blank (str, *start, start);
1383   _dbus_string_find_blank (str, *start, &i);
1384   
1385   if (i > *start)
1386     {
1387       if (!_dbus_string_copy_len (str, *start, i - *start, word, 0))
1388         return FALSE;
1389       
1390       *start = i;
1391     }
1392
1393   return TRUE;
1394 }
1395
1396 static dbus_bool_t
1397 record_mechanisms (DBusAuth         *auth,
1398                    const DBusString *command,
1399                    const DBusString *args)
1400 {
1401   int next;
1402   int len;
1403
1404   if (auth->already_got_mechanisms)
1405     return TRUE;
1406   
1407   len = _dbus_string_get_length (args);
1408   
1409   next = 0;
1410   while (next < len)
1411     {
1412       DBusString m;
1413       const DBusAuthMechanismHandler *mech;
1414       
1415       if (!_dbus_string_init (&m))
1416         goto nomem;
1417       
1418       if (!get_word (args, &next, &m))
1419         goto nomem;
1420
1421       mech = find_mech (&m);
1422
1423       if (mech != NULL)
1424         {
1425           /* FIXME right now we try mechanisms in the order
1426            * the server lists them; should we do them in
1427            * some more deterministic order?
1428            *
1429            * Probably in all_mechanisms order, our order of
1430            * preference. Of course when the server is us,
1431            * it lists things in that order anyhow.
1432            */
1433
1434           _dbus_verbose ("Adding mechanism %s to list we will try\n",
1435                          mech->mechanism);
1436           
1437           if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1438                                   (void*) mech))
1439             goto nomem;
1440         }
1441       else
1442         {
1443           _dbus_verbose ("Server offered mechanism \"%s\" that we don't know how to use\n",
1444                          _dbus_string_get_const_data (&m));
1445         }
1446
1447       _dbus_string_free (&m);
1448     }
1449   
1450   auth->already_got_mechanisms = TRUE;
1451   
1452   return TRUE;
1453
1454  nomem:
1455   _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1456   
1457   return FALSE;
1458 }
1459
1460 static dbus_bool_t
1461 client_try_next_mechanism (DBusAuth *auth)
1462 {
1463   const DBusAuthMechanismHandler *mech;
1464   DBusString auth_command;
1465
1466   if (DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1467     return FALSE;
1468
1469   mech = DBUS_AUTH_CLIENT (auth)->mechs_to_try->data;
1470
1471   if (!_dbus_string_init (&auth_command))
1472     return FALSE;
1473       
1474   if (!_dbus_string_append (&auth_command,
1475                             "AUTH "))
1476     {
1477       _dbus_string_free (&auth_command);
1478       return FALSE;
1479     }  
1480   
1481   if (!_dbus_string_append (&auth_command,
1482                             mech->mechanism))
1483     {
1484       _dbus_string_free (&auth_command);
1485       return FALSE;
1486     }
1487
1488   if (mech->client_initial_response_func != NULL)
1489     {
1490       if (!_dbus_string_append (&auth_command, " "))
1491         {
1492           _dbus_string_free (&auth_command);
1493           return FALSE;
1494         }
1495       
1496       if (!(* mech->client_initial_response_func) (auth, &auth_command))
1497         {
1498           _dbus_string_free (&auth_command);
1499           return FALSE;
1500         }
1501     }
1502   
1503   if (!_dbus_string_append (&auth_command,
1504                             "\r\n"))
1505     {
1506       _dbus_string_free (&auth_command);
1507       return FALSE;
1508     }
1509
1510   if (!_dbus_string_copy (&auth_command, 0,
1511                           &auth->outgoing,
1512                           _dbus_string_get_length (&auth->outgoing)))
1513     {
1514       _dbus_string_free (&auth_command);
1515       return FALSE;
1516     }
1517
1518   auth->mech = mech;      
1519   _dbus_list_pop_first (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1520
1521   _dbus_verbose ("Trying mechanism %s\n",
1522                  auth->mech->mechanism);
1523
1524   _dbus_string_free (&auth_command);
1525   
1526   return TRUE;
1527 }
1528
1529 static dbus_bool_t
1530 process_rejected (DBusAuth         *auth,
1531                   const DBusString *command,
1532                   const DBusString *args)
1533 {
1534   shutdown_mech (auth);
1535   
1536   if (!auth->already_got_mechanisms)
1537     {
1538       if (!record_mechanisms (auth, command, args))
1539         return FALSE;
1540     }
1541   
1542   if (DBUS_AUTH_CLIENT (auth)->mechs_to_try != NULL)
1543     {
1544       client_try_next_mechanism (auth);
1545     }
1546   else
1547     {
1548       /* Give up */
1549       auth->need_disconnect = TRUE;
1550     }
1551   
1552   return TRUE;
1553 }
1554
1555 static dbus_bool_t
1556 process_ok (DBusAuth         *auth,
1557             const DBusString *command,
1558             const DBusString *args)
1559 {
1560   if (!_dbus_string_append (&auth->outgoing,
1561                             "BEGIN\r\n"))
1562     return FALSE;
1563   
1564   auth->authenticated_pending_output = TRUE;
1565   
1566   return TRUE;
1567 }
1568
1569 static dbus_bool_t
1570 process_data_client (DBusAuth         *auth,
1571                      const DBusString *command,
1572                      const DBusString *args)
1573 {
1574   if (auth->mech != NULL)
1575     {
1576       DBusString decoded;
1577
1578       if (!_dbus_string_init (&decoded))
1579         return FALSE;
1580
1581       if (!_dbus_string_base64_decode (args, 0, &decoded, 0))
1582         {
1583           _dbus_string_free (&decoded);
1584           return FALSE;
1585         }
1586
1587 #ifdef DBUS_ENABLE_VERBOSE_MODE
1588       if (_dbus_string_validate_ascii (&decoded, 0,
1589                                        _dbus_string_get_length (&decoded)))
1590         {
1591           _dbus_verbose ("data: '%s'\n",
1592                          _dbus_string_get_const_data (&decoded));
1593         }
1594 #endif
1595       
1596       if (!(* auth->mech->client_data_func) (auth, &decoded))
1597         {
1598           _dbus_string_free (&decoded);
1599           return FALSE;
1600         }
1601
1602       _dbus_string_free (&decoded);
1603     }
1604   else
1605     {
1606       if (!_dbus_string_append (&auth->outgoing,
1607                                 "ERROR \"Got DATA when not in an auth exchange\"\r\n"))
1608         return FALSE;
1609     }
1610   
1611   return TRUE;
1612 }
1613
1614 static dbus_bool_t
1615 process_error_client (DBusAuth         *auth,
1616                       const DBusString *command,
1617                       const DBusString *args)
1618 {
1619   return TRUE;
1620 }
1621
1622 static dbus_bool_t
1623 process_unknown (DBusAuth         *auth,
1624                  const DBusString *command,
1625                  const DBusString *args)
1626 {
1627   if (!_dbus_string_append (&auth->outgoing,
1628                             "ERROR \"Unknown command\"\r\n"))
1629     return FALSE;
1630
1631   return TRUE;
1632 }
1633
1634 /* returns whether to call it again right away */
1635 static dbus_bool_t
1636 process_command (DBusAuth *auth)
1637 {
1638   DBusString command;
1639   DBusString args;
1640   int eol;
1641   int i, j;
1642   dbus_bool_t retval;
1643
1644   /* _dbus_verbose ("  trying process_command()\n"); */
1645   
1646   retval = FALSE;
1647   
1648   eol = 0;
1649   if (!_dbus_string_find (&auth->incoming, 0, "\r\n", &eol))
1650     return FALSE;
1651   
1652   if (!_dbus_string_init (&command))
1653     {
1654       auth->needed_memory = TRUE;
1655       return FALSE;
1656     }
1657
1658   if (!_dbus_string_init (&args))
1659     {
1660       _dbus_string_free (&command);
1661       auth->needed_memory = TRUE;
1662       return FALSE;
1663     }
1664   
1665   if (eol > _DBUS_ONE_MEGABYTE)
1666     {
1667       /* This is a giant line, someone is trying to hose us. */
1668       if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command too long\"\r\n"))
1669         goto out;
1670       else
1671         goto next_command;
1672     }
1673
1674   if (!_dbus_string_copy_len (&auth->incoming, 0, eol, &command, 0))
1675     goto out;
1676
1677   if (!_dbus_string_validate_ascii (&command, 0,
1678                                     _dbus_string_get_length (&command)))
1679     {
1680       _dbus_verbose ("Command contained non-ASCII chars or embedded nul\n");
1681       if (!_dbus_string_append (&auth->outgoing, "ERROR \"Command contained non-ASCII\"\r\n"))
1682         goto out;
1683       else
1684         goto next_command;
1685     }
1686   
1687   _dbus_verbose ("got command \"%s\"\n", _dbus_string_get_const_data (&command));
1688   
1689   _dbus_string_find_blank (&command, 0, &i);
1690   _dbus_string_skip_blank (&command, i, &j);
1691
1692   if (j > i)
1693     _dbus_string_delete (&command, i, j - i);
1694   
1695   if (!_dbus_string_move (&command, i, &args, 0))
1696     goto out;
1697   
1698   i = 0;
1699   while (auth->handlers[i].command != NULL)
1700     {
1701       if (_dbus_string_equal_c_str (&command,
1702                                     auth->handlers[i].command))
1703         {
1704           _dbus_verbose ("Processing auth command %s\n",
1705                          auth->handlers[i].command);
1706           
1707           if (!(* auth->handlers[i].func) (auth, &command, &args))
1708             goto out;
1709
1710           break;
1711         }
1712       ++i;
1713     }
1714
1715   if (auth->handlers[i].command == NULL)
1716     {
1717       if (!process_unknown (auth, &command, &args))
1718         goto out;
1719     }
1720
1721  next_command:
1722   
1723   /* We've succeeded in processing the whole command so drop it out
1724    * of the incoming buffer and return TRUE to try another command.
1725    */
1726
1727   _dbus_string_delete (&auth->incoming, 0, eol);
1728   
1729   /* kill the \r\n */
1730   _dbus_string_delete (&auth->incoming, 0, 2);
1731
1732   retval = TRUE;
1733   
1734  out:
1735   _dbus_string_free (&args);
1736   _dbus_string_free (&command);
1737
1738   if (!retval)
1739     auth->needed_memory = TRUE;
1740   else
1741     auth->needed_memory = FALSE;
1742   
1743   return retval;
1744 }
1745
1746
1747 /** @} */
1748
1749 /**
1750  * @addtogroup DBusAuth
1751  * @{
1752  */
1753
1754 /**
1755  * Creates a new auth conversation object for the server side.
1756  * See doc/dbus-sasl-profile.txt for full details on what
1757  * this object does.
1758  *
1759  * @returns the new object or #NULL if no memory
1760  */
1761 DBusAuth*
1762 _dbus_auth_server_new (void)
1763 {
1764   DBusAuth *auth;
1765   DBusAuthServer *server_auth;
1766
1767   auth = _dbus_auth_new (sizeof (DBusAuthServer));
1768   if (auth == NULL)
1769     return NULL;
1770
1771   auth->handlers = server_handlers;
1772
1773   server_auth = DBUS_AUTH_SERVER (auth);
1774
1775   /* perhaps this should be per-mechanism with a lower
1776    * max
1777    */
1778   server_auth->failures = 0;
1779   server_auth->max_failures = 6;
1780   
1781   return auth;
1782 }
1783
1784 /**
1785  * Creates a new auth conversation object for the client side.
1786  * See doc/dbus-sasl-profile.txt for full details on what
1787  * this object does.
1788  *
1789  * @returns the new object or #NULL if no memory
1790  */
1791 DBusAuth*
1792 _dbus_auth_client_new (void)
1793 {
1794   DBusAuth *auth;
1795
1796   auth = _dbus_auth_new (sizeof (DBusAuthClient));
1797   if (auth == NULL)
1798     return NULL;
1799
1800   auth->handlers = client_handlers;
1801
1802   /* Add a default mechanism to try */
1803   if (!_dbus_list_append (& DBUS_AUTH_CLIENT (auth)->mechs_to_try,
1804                           (void*) &all_mechanisms[0]))
1805     {
1806       _dbus_auth_unref (auth);
1807       return NULL;
1808     }
1809
1810   /* Now try the mechanism we just added */
1811   if (!client_try_next_mechanism (auth))
1812     {
1813       _dbus_auth_unref (auth);
1814       return NULL;
1815     }
1816   
1817   return auth;
1818 }
1819
1820 /**
1821  * Increments the refcount of an auth object.
1822  *
1823  * @param auth the auth conversation
1824  */
1825 void
1826 _dbus_auth_ref (DBusAuth *auth)
1827 {
1828   _dbus_assert (auth != NULL);
1829   
1830   auth->refcount += 1;
1831 }
1832
1833 /**
1834  * Decrements the refcount of an auth object.
1835  *
1836  * @param auth the auth conversation
1837  */
1838 void
1839 _dbus_auth_unref (DBusAuth *auth)
1840 {
1841   _dbus_assert (auth != NULL);
1842   _dbus_assert (auth->refcount > 0);
1843
1844   auth->refcount -= 1;
1845   if (auth->refcount == 0)
1846     {
1847       shutdown_mech (auth);
1848
1849       if (DBUS_AUTH_IS_CLIENT (auth))
1850         {
1851           _dbus_list_clear (& DBUS_AUTH_CLIENT (auth)->mechs_to_try);
1852         }
1853
1854       if (auth->keyring)
1855         _dbus_keyring_unref (auth->keyring);
1856
1857       _dbus_string_free (&auth->context);
1858       _dbus_string_free (&auth->challenge);
1859       _dbus_string_free (&auth->identity);
1860       _dbus_string_free (&auth->incoming);
1861       _dbus_string_free (&auth->outgoing);
1862       dbus_free (auth);
1863     }
1864 }
1865
1866 /**
1867  * @param auth the auth conversation object
1868  * @returns #TRUE if we're in a final state
1869  */
1870 #define DBUS_AUTH_IN_END_STATE(auth) ((auth)->need_disconnect || (auth)->authenticated)
1871
1872 /**
1873  * Analyzes buffered input and moves the auth conversation forward,
1874  * returning the new state of the auth conversation.
1875  *
1876  * @param auth the auth conversation
1877  * @returns the new state
1878  */
1879 DBusAuthState
1880 _dbus_auth_do_work (DBusAuth *auth)
1881 {
1882   auth->needed_memory = FALSE;
1883
1884   /* Max amount we'll buffer up before deciding someone's on crack */
1885 #define MAX_BUFFER (16 * _DBUS_ONE_KILOBYTE)
1886
1887   do
1888     {
1889       if (DBUS_AUTH_IN_END_STATE (auth))
1890         break;
1891       
1892       if (_dbus_string_get_length (&auth->incoming) > MAX_BUFFER ||
1893           _dbus_string_get_length (&auth->outgoing) > MAX_BUFFER)
1894         {
1895           auth->need_disconnect = TRUE;
1896           _dbus_verbose ("Disconnecting due to excessive data buffered in auth phase\n");
1897           break;
1898         }
1899
1900       if (auth->mech == NULL &&
1901           auth->already_got_mechanisms &&
1902           DBUS_AUTH_CLIENT (auth)->mechs_to_try == NULL)
1903         {
1904           auth->need_disconnect = TRUE;
1905           _dbus_verbose ("Disconnecting because we are out of mechanisms to try using\n");
1906           break;
1907         }
1908     }
1909   while (process_command (auth));
1910
1911   if (DBUS_AUTH_IS_SERVER (auth) &&
1912       DBUS_AUTH_SERVER (auth)->failures >=
1913       DBUS_AUTH_SERVER (auth)->max_failures)
1914     auth->need_disconnect = TRUE;
1915
1916   if (auth->need_disconnect)
1917     return DBUS_AUTH_STATE_NEED_DISCONNECT;
1918   else if (auth->authenticated)
1919     {
1920       if (_dbus_string_get_length (&auth->incoming) > 0)
1921         return DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES;
1922       else
1923         return DBUS_AUTH_STATE_AUTHENTICATED;
1924     }
1925   else if (auth->needed_memory)
1926     return DBUS_AUTH_STATE_WAITING_FOR_MEMORY;
1927   else if (_dbus_string_get_length (&auth->outgoing) > 0)
1928     return DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
1929   else
1930     return DBUS_AUTH_STATE_WAITING_FOR_INPUT;
1931 }
1932
1933 /**
1934  * Gets bytes that need to be sent to the peer we're conversing with.
1935  * After writing some bytes, _dbus_auth_bytes_sent() must be called
1936  * to notify the auth object that they were written.
1937  *
1938  * @param auth the auth conversation
1939  * @param str return location for a ref to the buffer to send
1940  * @returns #FALSE if nothing to send
1941  */
1942 dbus_bool_t
1943 _dbus_auth_get_bytes_to_send (DBusAuth          *auth,
1944                               const DBusString **str)
1945 {
1946   _dbus_assert (auth != NULL);
1947   _dbus_assert (str != NULL);
1948
1949   *str = NULL;
1950   
1951   if (DBUS_AUTH_IN_END_STATE (auth))
1952     return FALSE;
1953
1954   if (_dbus_string_get_length (&auth->outgoing) == 0)
1955     return FALSE;
1956
1957   *str = &auth->outgoing;
1958
1959   return TRUE;
1960 }
1961
1962 /**
1963  * Notifies the auth conversation object that
1964  * the given number of bytes of the outgoing buffer
1965  * have been written out.
1966  *
1967  * @param auth the auth conversation
1968  * @param bytes_sent number of bytes written out
1969  */
1970 void
1971 _dbus_auth_bytes_sent (DBusAuth *auth,
1972                        int       bytes_sent)
1973 {
1974   _dbus_verbose ("Sent %d bytes of: %s\n", bytes_sent,
1975                  _dbus_string_get_const_data (&auth->outgoing));
1976   
1977   _dbus_string_delete (&auth->outgoing,
1978                        0, bytes_sent);
1979   
1980   if (auth->authenticated_pending_output &&
1981       _dbus_string_get_length (&auth->outgoing) == 0)
1982     auth->authenticated = TRUE;
1983 }
1984
1985 /**
1986  * Get a buffer to be used for reading bytes from the peer we're conversing
1987  * with. Bytes should be appended to this buffer.
1988  *
1989  * @param auth the auth conversation
1990  * @param buffer return location for buffer to append bytes to
1991  */
1992 void
1993 _dbus_auth_get_buffer (DBusAuth     *auth,
1994                        DBusString **buffer)
1995 {
1996   _dbus_assert (auth != NULL);
1997   _dbus_assert (!auth->buffer_outstanding);
1998   
1999   *buffer = &auth->incoming;
2000
2001   auth->buffer_outstanding = TRUE;
2002 }
2003
2004 /**
2005  * Returns a buffer with new data read into it.
2006  *
2007  * @param auth the auth conversation
2008  * @param buffer the buffer being returned
2009  * @param bytes_read number of new bytes added
2010  */
2011 void
2012 _dbus_auth_return_buffer (DBusAuth               *auth,
2013                           DBusString             *buffer,
2014                           int                     bytes_read)
2015 {
2016   _dbus_assert (buffer == &auth->incoming);
2017   _dbus_assert (auth->buffer_outstanding);
2018
2019   auth->buffer_outstanding = FALSE;
2020 }
2021
2022 /**
2023  * Returns leftover bytes that were not used as part of the auth
2024  * conversation.  These bytes will be part of the message stream
2025  * instead. This function may not be called until authentication has
2026  * succeeded.
2027  *
2028  * @param auth the auth conversation
2029  * @param str return location for pointer to string of unused bytes
2030  */
2031 void
2032 _dbus_auth_get_unused_bytes (DBusAuth           *auth,
2033                              const DBusString **str)
2034 {
2035   if (!DBUS_AUTH_IN_END_STATE (auth))
2036     return;
2037
2038   *str = &auth->incoming;
2039 }
2040
2041
2042 /**
2043  * Gets rid of unused bytes returned by _dbus_auth_get_unused_bytes()
2044  * after we've gotten them and successfully moved them elsewhere.
2045  *
2046  * @param auth the auth conversation
2047  */
2048 void
2049 _dbus_auth_delete_unused_bytes (DBusAuth *auth)
2050 {
2051   if (!DBUS_AUTH_IN_END_STATE (auth))
2052     return;
2053
2054   _dbus_string_set_length (&auth->incoming, 0);
2055 }
2056
2057 /**
2058  * Called post-authentication, indicates whether we need to encode
2059  * the message stream with _dbus_auth_encode_data() prior to
2060  * sending it to the peer.
2061  *
2062  * @param auth the auth conversation
2063  * @returns #TRUE if we need to encode the stream
2064  */
2065 dbus_bool_t
2066 _dbus_auth_needs_encoding (DBusAuth *auth)
2067 {
2068   if (!auth->authenticated)
2069     return FALSE;
2070   
2071   if (auth->mech != NULL)
2072     {
2073       if (DBUS_AUTH_IS_CLIENT (auth))
2074         return auth->mech->client_encode_func != NULL;
2075       else
2076         return auth->mech->server_encode_func != NULL;
2077     }
2078   else
2079     return FALSE;
2080 }
2081
2082 /**
2083  * Called post-authentication, encodes a block of bytes for sending to
2084  * the peer. If no encoding was negotiated, just copies the bytes
2085  * (you can avoid this by checking _dbus_auth_needs_encoding()).
2086  *
2087  * @param auth the auth conversation
2088  * @param plaintext the plain text data
2089  * @param encoded initialized string to where encoded data is appended
2090  * @returns #TRUE if we had enough memory and successfully encoded
2091  */
2092 dbus_bool_t
2093 _dbus_auth_encode_data (DBusAuth         *auth,
2094                         const DBusString *plaintext,
2095                         DBusString       *encoded)
2096 {
2097   _dbus_assert (plaintext != encoded);
2098   
2099   if (!auth->authenticated)
2100     return FALSE;
2101   
2102   if (_dbus_auth_needs_encoding (auth))
2103     {
2104       if (DBUS_AUTH_IS_CLIENT (auth))
2105         return (* auth->mech->client_encode_func) (auth, plaintext, encoded);
2106       else
2107         return (* auth->mech->server_encode_func) (auth, plaintext, encoded);
2108     }
2109   else
2110     {
2111       return _dbus_string_copy (plaintext, 0, encoded,
2112                                 _dbus_string_get_length (encoded));
2113     }
2114 }
2115
2116 /**
2117  * Called post-authentication, indicates whether we need to decode
2118  * the message stream with _dbus_auth_decode_data() after
2119  * receiving it from the peer.
2120  *
2121  * @param auth the auth conversation
2122  * @returns #TRUE if we need to encode the stream
2123  */
2124 dbus_bool_t
2125 _dbus_auth_needs_decoding (DBusAuth *auth)
2126 {
2127   if (!auth->authenticated)
2128     return FALSE;
2129     
2130   if (auth->mech != NULL)
2131     {
2132       if (DBUS_AUTH_IS_CLIENT (auth))
2133         return auth->mech->client_decode_func != NULL;
2134       else
2135         return auth->mech->server_decode_func != NULL;
2136     }
2137   else
2138     return FALSE;
2139 }
2140
2141
2142 /**
2143  * Called post-authentication, decodes a block of bytes received from
2144  * the peer. If no encoding was negotiated, just copies the bytes (you
2145  * can avoid this by checking _dbus_auth_needs_decoding()).
2146  *
2147  * @todo We need to be able to distinguish "out of memory" error
2148  * from "the data is hosed" error.
2149  *
2150  * @param auth the auth conversation
2151  * @param encoded the encoded data
2152  * @param plaintext initialized string where decoded data is appended
2153  * @returns #TRUE if we had enough memory and successfully decoded
2154  */
2155 dbus_bool_t
2156 _dbus_auth_decode_data (DBusAuth         *auth,
2157                         const DBusString *encoded,
2158                         DBusString       *plaintext)
2159 {
2160   _dbus_assert (plaintext != encoded);
2161   
2162   if (!auth->authenticated)
2163     return FALSE;
2164   
2165   if (_dbus_auth_needs_decoding (auth))
2166     {
2167       if (DBUS_AUTH_IS_CLIENT (auth))
2168         return (* auth->mech->client_decode_func) (auth, encoded, plaintext);
2169       else
2170         return (* auth->mech->server_decode_func) (auth, encoded, plaintext);
2171     }
2172   else
2173     {
2174       return _dbus_string_copy (encoded, 0, plaintext,
2175                                 _dbus_string_get_length (plaintext));
2176     }
2177 }
2178
2179 /**
2180  * Sets credentials received via reliable means from the operating
2181  * system.
2182  *
2183  * @param auth the auth conversation
2184  * @param credentials the credentials received
2185  */
2186 void
2187 _dbus_auth_set_credentials (DBusAuth               *auth,
2188                             const DBusCredentials  *credentials)
2189 {
2190   auth->credentials = *credentials;
2191 }
2192
2193 /**
2194  * Gets the identity we authorized the client as.  Apps may have
2195  * different policies as to what identities they allow.
2196  *
2197  * @param auth the auth conversation
2198  * @param credentials the credentials we've authorized
2199  */
2200 void
2201 _dbus_auth_get_identity (DBusAuth               *auth,
2202                          DBusCredentials        *credentials)
2203 {
2204   if (auth->authenticated)
2205     {
2206       *credentials = auth->authorized_identity;
2207     }
2208   else
2209     {
2210       credentials->pid = -1;
2211       credentials->uid = -1;
2212       credentials->gid = -1;
2213     }
2214 }
2215
2216 /**
2217  * Sets the "authentication context" which scopes cookies
2218  * with the DBUS_COOKIE_SHA1 auth mechanism for example.
2219  *
2220  * @param auth the auth conversation
2221  * @param context the context
2222  * @returns #FALSE if no memory
2223  */
2224 dbus_bool_t
2225 _dbus_auth_set_context (DBusAuth               *auth,
2226                         const DBusString       *context)
2227 {
2228   return _dbus_string_replace_len (context, 0, _dbus_string_get_length (context),
2229                                    &auth->context, 0, _dbus_string_get_length (context));
2230 }
2231
2232 /** @} */
2233
2234 #ifdef DBUS_BUILD_TESTS
2235 #include "dbus-test.h"
2236 #include "dbus-auth-script.h"
2237 #include <stdio.h>
2238
2239 static dbus_bool_t
2240 process_test_subdir (const DBusString          *test_base_dir,
2241                      const char                *subdir)
2242 {
2243   DBusString test_directory;
2244   DBusString filename;
2245   DBusDirIter *dir;
2246   dbus_bool_t retval;
2247   DBusError error;
2248
2249   retval = FALSE;
2250   dir = NULL;
2251   
2252   if (!_dbus_string_init (&test_directory))
2253     _dbus_assert_not_reached ("didn't allocate test_directory\n");
2254
2255   _dbus_string_init_const (&filename, subdir);
2256   
2257   if (!_dbus_string_copy (test_base_dir, 0,
2258                           &test_directory, 0))
2259     _dbus_assert_not_reached ("couldn't copy test_base_dir to test_directory");
2260   
2261   if (!_dbus_concat_dir_and_file (&test_directory, &filename))    
2262     _dbus_assert_not_reached ("couldn't allocate full path");
2263
2264   _dbus_string_free (&filename);
2265   if (!_dbus_string_init (&filename))
2266     _dbus_assert_not_reached ("didn't allocate filename string\n");
2267
2268   dbus_error_init (&error);
2269   dir = _dbus_directory_open (&test_directory, &error);
2270   if (dir == NULL)
2271     {
2272       _dbus_warn ("Could not open %s: %s\n",
2273                   _dbus_string_get_const_data (&test_directory),
2274                   error.message);
2275       dbus_error_free (&error);
2276       goto failed;
2277     }
2278
2279   printf ("Testing:\n");
2280   
2281  next:
2282   while (_dbus_directory_get_next_file (dir, &filename, &error))
2283     {
2284       DBusString full_path;
2285       
2286       if (!_dbus_string_init (&full_path))
2287         _dbus_assert_not_reached ("couldn't init string");
2288
2289       if (!_dbus_string_copy (&test_directory, 0, &full_path, 0))
2290         _dbus_assert_not_reached ("couldn't copy dir to full_path");
2291
2292       if (!_dbus_concat_dir_and_file (&full_path, &filename))
2293         _dbus_assert_not_reached ("couldn't concat file to dir");
2294
2295       if (!_dbus_string_ends_with_c_str (&filename, ".auth-script"))
2296         {
2297           _dbus_verbose ("Skipping non-.auth-script file %s\n",
2298                          _dbus_string_get_const_data (&filename));
2299           _dbus_string_free (&full_path);
2300           goto next;
2301         }
2302
2303       printf ("    %s\n", _dbus_string_get_const_data (&filename));
2304       
2305       if (!_dbus_auth_script_run (&full_path))
2306         {
2307           _dbus_string_free (&full_path);
2308           goto failed;
2309         }
2310       else
2311         _dbus_string_free (&full_path);
2312     }
2313
2314   if (dbus_error_is_set (&error))
2315     {
2316       _dbus_warn ("Could not get next file in %s: %s\n",
2317                   _dbus_string_get_const_data (&test_directory), error.message);
2318       dbus_error_free (&error);
2319       goto failed;
2320     }
2321     
2322   retval = TRUE;
2323   
2324  failed:
2325
2326   if (dir)
2327     _dbus_directory_close (dir);
2328   _dbus_string_free (&test_directory);
2329   _dbus_string_free (&filename);
2330
2331   return retval;
2332 }
2333
2334 static dbus_bool_t
2335 process_test_dirs (const char *test_data_dir)
2336 {
2337   DBusString test_directory;
2338   dbus_bool_t retval;
2339
2340   retval = FALSE;
2341   
2342   _dbus_string_init_const (&test_directory, test_data_dir);
2343
2344   if (!process_test_subdir (&test_directory, "auth"))
2345     goto failed;
2346
2347   retval = TRUE;
2348   
2349  failed:
2350
2351   _dbus_string_free (&test_directory);
2352   
2353   return retval;
2354 }
2355
2356 dbus_bool_t
2357 _dbus_auth_test (const char *test_data_dir)
2358 {
2359   
2360   if (test_data_dir == NULL)
2361     return TRUE;
2362   
2363   if (!process_test_dirs (test_data_dir))
2364     return FALSE;
2365
2366   return TRUE;
2367 }
2368
2369 #endif /* DBUS_BUILD_TESTS */