run-passwd.c 30.7 KB
Newer Older
William Jon McCann's avatar
William Jon McCann committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* run-passwd.c: this file is part of users-admin, a gnome-system-tools frontend
 * for user administration.
 *
 * Copyright (C) 2002 Diego Gonzalez
 * Copyright (C) 2006 Johannes H. Jensen
 * Copyright (C) 2010 Milan Bouchet-Valat
 *
 * Written by: Diego Gonzalez <diego@pemas.net>
 * Modified by: Johannes H. Jensen <joh@deworks.net>,
 *              Milan Bouchet-Valat <nalimilan@club.fr>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Most of this code originally comes from gnome-about-me-password.c,
 * from gnome-control-center.
 */

#include <config.h>
#include <glib/gi18n.h>

#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>

#if __sun
#include <sys/types.h>
#include <signal.h>
#endif

#include "run-passwd.h"

/* Passwd states */
typedef enum {
        PASSWD_STATE_NONE,              /* Passwd is not asking for anything */
        PASSWD_STATE_AUTH,              /* Passwd is asking for our current password */
        PASSWD_STATE_NEW,               /* Passwd is asking for our new password */
        PASSWD_STATE_RETYPE,            /* Passwd is asking for our retyped new password */
        PASSWD_STATE_DONE,              /* Passwd succeeded but has not yet exited */
        PASSWD_STATE_ERR                /* Passwd reported an error but has not yet exited */
} PasswdState;

struct PasswdHandler {
        const char *current_password;
        const char *new_password;

        /* Communication with the passwd program */
        GPid backend_pid;

        GIOChannel *backend_stdin;
        GIOChannel *backend_stdout;

        GQueue *backend_stdin_queue;            /* Write queue to backend_stdin */

        /* GMainLoop IDs */
        guint backend_child_watch_id;           /* g_child_watch_add (PID) */
        guint backend_stdout_watch_id;          /* g_io_add_watch (stdout) */

        /* State of the passwd program */
        PasswdState backend_state;
        gboolean    changing_password;

        PasswdCallback auth_cb;
        gpointer       auth_cb_data;

        PasswdCallback chpasswd_cb;
        gpointer       chpasswd_cb_data;
};

/* Buffer size for backend output */
#define BUFSIZE 64


static GQuark
passwd_error_quark (void)
{
        static GQuark q = 0;

        if (q == 0) {
                q = g_quark_from_static_string("passwd_error");
        }

        return q;
}

/* Error handling */
#define PASSWD_ERROR (passwd_error_quark ())


static void
stop_passwd (PasswdHandler *passwd_handler);

static void
free_passwd_resources (PasswdHandler *passwd_handler);

static gboolean
io_watch_stdout (GIOChannel *source, GIOCondition condition, PasswdHandler *passwd_handler);


/*
 * Spawning and closing of backend {{
 */

/* Child watcher */
static void
child_watch_cb (GPid pid, gint status, PasswdHandler *passwd_handler)
{
        if (WIFEXITED (status)) {
                if (WEXITSTATUS (status) >= 255) {
                        g_warning ("Child exited unexpectedly");
                }
                if (WEXITSTATUS (status) == 0) {
                        if (passwd_handler->backend_state == PASSWD_STATE_RETYPE) {
                                passwd_handler->backend_state = PASSWD_STATE_DONE;
                                if (passwd_handler->chpasswd_cb)
                                                passwd_handler->chpasswd_cb (passwd_handler,
                                                                             NULL,
132
                                                                             passwd_handler->chpasswd_cb_data);
William Jon McCann's avatar
William Jon McCann committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
                        }
                }
        }

        free_passwd_resources (passwd_handler);
}

static void
ignore_sigpipe (gpointer data)
{
        signal (SIGPIPE, SIG_IGN);
}

/* Spawn passwd backend
 * Returns: TRUE on success, FALSE otherwise and sets error appropriately */
static gboolean
spawn_passwd (PasswdHandler *passwd_handler, GError **error)
{
        gchar   *argv[2];
152
        gchar  **envp;
William Jon McCann's avatar
William Jon McCann committed
153 154 155 156 157
        gint    my_stdin, my_stdout, my_stderr;

        argv[0] = "/usr/bin/passwd";    /* Is it safe to rely on a hard-coded path? */
        argv[1] = NULL;

158 159
        envp = g_get_environ ();
        envp = g_environ_setenv (envp, "LC_ALL", "C", TRUE);
William Jon McCann's avatar
William Jon McCann committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

        if (!g_spawn_async_with_pipes (NULL,                            /* Working directory */
                                       argv,                            /* Argument vector */
                                       envp,                            /* Environment */
                                       G_SPAWN_DO_NOT_REAP_CHILD,       /* Flags */
                                       ignore_sigpipe,                  /* Child setup */
                                       NULL,                            /* Data to child setup */
                                       &passwd_handler->backend_pid,    /* PID */
                                       &my_stdin,                       /* Stdin */
                                       &my_stdout,                      /* Stdout */
                                       &my_stderr,                      /* Stderr */
                                       error)) {                        /* GError */

                /* An error occured */
                free_passwd_resources (passwd_handler);

176 177
                g_strfreev (envp);

William Jon McCann's avatar
William Jon McCann committed
178 179 180
                return FALSE;
        }

181 182
        g_strfreev (envp);

William Jon McCann's avatar
William Jon McCann committed
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
        /* 2>&1 */
        if (dup2 (my_stderr, my_stdout) == -1) {
                /* Failed! */
                g_set_error_literal (error,
                                     PASSWD_ERROR,
                                     PASSWD_ERROR_BACKEND,
                                     strerror (errno));

                /* Clean up */
                stop_passwd (passwd_handler);

                return FALSE;
        }

        /* Open IO Channels */
        passwd_handler->backend_stdin = g_io_channel_unix_new (my_stdin);
        passwd_handler->backend_stdout = g_io_channel_unix_new (my_stdout);

        /* Set raw encoding */
        /* Set nonblocking mode */
        if (g_io_channel_set_encoding (passwd_handler->backend_stdin, NULL, error) != G_IO_STATUS_NORMAL ||
                g_io_channel_set_encoding (passwd_handler->backend_stdout, NULL, error) != G_IO_STATUS_NORMAL ||
                g_io_channel_set_flags (passwd_handler->backend_stdin, G_IO_FLAG_NONBLOCK, error) != G_IO_STATUS_NORMAL ||
                g_io_channel_set_flags (passwd_handler->backend_stdout, G_IO_FLAG_NONBLOCK, error) != G_IO_STATUS_NORMAL ) {

                /* Clean up */
                stop_passwd (passwd_handler);
                return FALSE;
        }

        /* Turn off buffering */
        g_io_channel_set_buffered (passwd_handler->backend_stdin, FALSE);
        g_io_channel_set_buffered (passwd_handler->backend_stdout, FALSE);

        /* Add IO Channel watcher */
        passwd_handler->backend_stdout_watch_id = g_io_add_watch (passwd_handler->backend_stdout,
                                                                  G_IO_IN | G_IO_PRI,
                                                                  (GIOFunc) io_watch_stdout, passwd_handler);

        /* Add child watcher */
        passwd_handler->backend_child_watch_id = g_child_watch_add (passwd_handler->backend_pid, (GChildWatchFunc) child_watch_cb, passwd_handler);

        /* Success! */

        return TRUE;
}

/* Stop passwd backend */
static void
stop_passwd (PasswdHandler *passwd_handler)
{
        /* This is the standard way of returning from the dialog with passwd.
         * If we return this way we can safely kill passwd as it has completed
         * its task.
         */

        if (passwd_handler->backend_pid != -1) {
                kill (passwd_handler->backend_pid, 9);
        }

        /* We must run free_passwd_resources here and not let our child
         * watcher do it, since it will access invalid memory after the
         * dialog has been closed and cleaned up.
         *
         * If we had more than a single thread we'd need to remove
         * the child watch before trying to kill the child.
         */
        free_passwd_resources (passwd_handler);
}

/* Clean up passwd resources */
static void
free_passwd_resources (PasswdHandler *passwd_handler)
{
        GError  *error = NULL;

        /* Remove the child watcher */
        if (passwd_handler->backend_child_watch_id != 0) {

                g_source_remove (passwd_handler->backend_child_watch_id);

                passwd_handler->backend_child_watch_id = 0;
        }


        /* Close IO channels (internal file descriptors are automatically closed) */
        if (passwd_handler->backend_stdin != NULL) {

                if (g_io_channel_shutdown (passwd_handler->backend_stdin, TRUE, &error) != G_IO_STATUS_NORMAL) {
                        g_warning ("Could not shutdown backend_stdin IO channel: %s", error->message);
                        g_error_free (error);
                        error = NULL;
                }

                g_io_channel_unref (passwd_handler->backend_stdin);
                passwd_handler->backend_stdin = NULL;
        }

        if (passwd_handler->backend_stdout != NULL) {

                if (g_io_channel_shutdown (passwd_handler->backend_stdout, TRUE, &error) != G_IO_STATUS_NORMAL) {
                        g_warning ("Could not shutdown backend_stdout IO channel: %s", error->message);
                        g_error_free (error);
                        error = NULL;
                }

                g_io_channel_unref (passwd_handler->backend_stdout);

                passwd_handler->backend_stdout = NULL;
        }

        /* Remove IO watcher */
        if (passwd_handler->backend_stdout_watch_id != 0) {

                g_source_remove (passwd_handler->backend_stdout_watch_id);

                passwd_handler->backend_stdout_watch_id = 0;
        }

        /* Close PID */
        if (passwd_handler->backend_pid != -1) {

                g_spawn_close_pid (passwd_handler->backend_pid);

                passwd_handler->backend_pid = -1;
        }

        /* Clear backend state */
        passwd_handler->backend_state = PASSWD_STATE_NONE;
}

/*
 * }} Spawning and closing of backend
 */

/*
 * Backend communication code {{
 */

/* Write the first element of queue through channel */
static void
io_queue_pop (GQueue *queue, GIOChannel *channel)
{
        gchar   *buf;
        gsize   bytes_written;
        GError  *error = NULL;

        buf = g_queue_pop_head (queue);

        if (buf != NULL) {

                if (g_io_channel_write_chars (channel, buf, -1, &bytes_written, &error) != G_IO_STATUS_NORMAL) {
                        g_warning ("Could not write queue element \"%s\" to channel: %s", buf, error->message);
                        g_error_free (error);
                }

                /* Ensure passwords are cleared from memory */
                memset (buf, 0, strlen (buf));
                g_free (buf);
        }
}

/* Goes through the argument list, checking if one of them occurs in str
 * Returns: TRUE as soon as an element is found to match, FALSE otherwise */
static gboolean
is_string_complete (gchar *str, ...)
{
        va_list ap;
        gchar   *arg;

        if (strlen (str) == 0) {
                return FALSE;
        }

        va_start (ap, str);

        while ((arg = va_arg (ap, char *)) != NULL) {
                if (strstr (str, arg) != NULL) {
                        va_end (ap);
                        return TRUE;
                }
        }

        va_end (ap);

        return FALSE;
}

/*
 * IO watcher for stdout, called whenever there is data to read from the backend.
 * This is where most of the actual IO handling happens.
 */
static gboolean
io_watch_stdout (GIOChannel *source, GIOCondition condition, PasswdHandler *passwd_handler)
{
        static GString *str = NULL;     /* Persistent buffer */

        gchar           buf[BUFSIZE];           /* Temporary buffer */
        gsize           bytes_read;
        GError          *gio_error = NULL;      /* Error returned by functions */
        GError          *error = NULL;          /* Error sent to callbacks */

        gboolean        reinit = FALSE;

        /* Initialize buffer */
        if (str == NULL) {
                str = g_string_new ("");
        }

        if (g_io_channel_read_chars (source, buf, BUFSIZE, &bytes_read, &gio_error)
            != G_IO_STATUS_NORMAL) {
                g_warning ("IO Channel read error: %s", gio_error->message);
                g_error_free (gio_error);

                return TRUE;
        }

        str = g_string_append_len (str, buf, bytes_read);

        /* In which state is the backend? */
        switch (passwd_handler->backend_state) {
                case PASSWD_STATE_AUTH:
                        /* Passwd is asking for our current password */

                        if (is_string_complete (str->str, "assword: ", "failure", "wrong", "error", NULL)) {

                                if (strstr (str->str, "assword: ") != NULL) {
                                        /* Authentication successful */

                                        passwd_handler->backend_state = PASSWD_STATE_NEW;

                                        /* Trigger callback to update authentication status */
                                        if (passwd_handler->auth_cb)
                                                passwd_handler->auth_cb (passwd_handler,
                                                                         NULL,
                                                                         passwd_handler->auth_cb_data);

                                } else {
                                        /* Authentication failed */

                                        error = g_error_new_literal (PASSWD_ERROR, PASSWD_ERROR_AUTH_FAILED,
                                                                     _("Authentication failed"));

                                        passwd_handler->changing_password = FALSE;

                                        /* This error can happen both while authenticating or while changing password:
                                         * if chpasswd_cb is set, this means we're already changing password */
                                        if (passwd_handler->chpasswd_cb)
                                                passwd_handler->chpasswd_cb (passwd_handler,
                                                                             error,
433
                                                                             passwd_handler->chpasswd_cb_data);
William Jon McCann's avatar
William Jon McCann committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770
                                        else if (passwd_handler->auth_cb)
                                                passwd_handler->auth_cb (passwd_handler,
                                                                         error,
                                                                         passwd_handler->auth_cb_data);

                                        g_error_free (error);
                                }

                                reinit = TRUE;
                        }
                        break;
                case PASSWD_STATE_NEW:
                        /* Passwd is asking for our new password */

                        if (is_string_complete (str->str, "assword: ", NULL)) {
                                /* Advance to next state */
                                passwd_handler->backend_state = PASSWD_STATE_RETYPE;

                                /* Pop retyped password from queue and into IO channel */
                                io_queue_pop (passwd_handler->backend_stdin_queue, passwd_handler->backend_stdin);

                                reinit = TRUE;
                        }
                        break;
                case PASSWD_STATE_RETYPE:
                        /* Passwd is asking for our retyped new password */

                        if (is_string_complete (str->str,
                                                "successfully",
                                                "short",
                                                "longer",
                                                "palindrome",
                                                "dictionary",
                                                "simple",
                                                "simplistic",
                                                "similar",
                                                "case",
                                                "different",
                                                "wrapped",
                                                "recovered",
                                                "recent",
                                                "unchanged",
                                                "match",
                                                "1 numeric or special",
                                                "failure",
                                                "DIFFERENT",
                                                "BAD PASSWORD",
                                                NULL)) {

                                if (strstr (str->str, "successfully") != NULL) {
                                        /* Hooray! */

                                        passwd_handler->backend_state = PASSWD_STATE_DONE;
                                        /* Trigger callback to update status */
                                        if (passwd_handler->chpasswd_cb)
                                                passwd_handler->chpasswd_cb (passwd_handler,
                                                                             NULL,
                                                                             passwd_handler->chpasswd_cb_data);
                                }
                                else {
                                        /* Ohnoes! */

                                        if (strstr (str->str, "recovered") != NULL) {
                                                /* What does this indicate?
                                                 * "Authentication information cannot be recovered?" from libpam? */
                                                error = g_error_new_literal (PASSWD_ERROR, PASSWD_ERROR_UNKNOWN,
                                                                             str->str);
                                        } else if (strstr (str->str, "short") != NULL ||
                                                   strstr (str->str, "longer") != NULL) {
                                                error = g_error_new (PASSWD_ERROR, PASSWD_ERROR_REJECTED,
                                                                     _("The new password is too short"));
                                        } else if (strstr (str->str, "palindrome") != NULL ||
                                                   strstr (str->str, "simple") != NULL ||
                                                   strstr (str->str, "simplistic") != NULL ||
                                                   strstr (str->str, "dictionary") != NULL) {
                                                error = g_error_new (PASSWD_ERROR, PASSWD_ERROR_REJECTED,
                                                                     _("The new password is too simple"));
                                        } else if (strstr (str->str, "similar") != NULL ||
                                                   strstr (str->str, "different") != NULL ||
                                                   strstr (str->str, "case") != NULL ||
                                                   strstr (str->str, "wrapped") != NULL) {
                                                error = g_error_new (PASSWD_ERROR, PASSWD_ERROR_REJECTED,
                                                                     _("The old and new passwords are too similar"));
                                        } else if (strstr (str->str, "recent") != NULL) {
                                                error = g_error_new (PASSWD_ERROR, PASSWD_ERROR_REJECTED,
                                                                     _("The new password has already been used recently."));
                                        } else if (strstr (str->str, "1 numeric or special") != NULL) {
                                                error = g_error_new (PASSWD_ERROR, PASSWD_ERROR_REJECTED,
                                                                     _("The new password must contain numeric or special characters"));
                                        } else if (strstr (str->str, "unchanged") != NULL ||
                                                   strstr (str->str, "match") != NULL) {
                                                error = g_error_new (PASSWD_ERROR, PASSWD_ERROR_REJECTED,
                                                                     _("The old and new passwords are the same"));
                                        } else if (strstr (str->str, "failure") != NULL) {
                                                /* Authentication failure */
                                                error = g_error_new (PASSWD_ERROR, PASSWD_ERROR_AUTH_FAILED,
                                                                     _("Your password has been changed since you initially authenticated!"));
                                        }
                                        else if (strstr (str->str, "DIFFERENT")) {
                                                error = g_error_new (PASSWD_ERROR, PASSWD_ERROR_REJECTED,
                                                                     _("The new password does not contain enough different characters"));
                                        }
                                        else {
                                                error = g_error_new (PASSWD_ERROR, PASSWD_ERROR_UNKNOWN,
                                                                     _("Unknown error"));
                                        }

                                        /* At this point, passwd might have exited, in which case
                                         * child_watch_cb should clean up for us and remove this watcher.
                                         * On some error conditions though, passwd just re-prompts us
                                         * for our new password. */
                                        passwd_handler->backend_state = PASSWD_STATE_ERR;

                                        passwd_handler->changing_password = FALSE;

                                        /* Trigger callback to update status */
                                        if (passwd_handler->chpasswd_cb)
                                                passwd_handler->chpasswd_cb (passwd_handler,
                                                                             error,
                                                                             passwd_handler->chpasswd_cb_data);

                                        g_error_free (error);

                                }

                                reinit = TRUE;

                                /* child_watch_cb should clean up for us now */
                        }
                        break;
                case PASSWD_STATE_NONE:
                        /* Passwd is not asking for anything yet */
                        if (is_string_complete (str->str, "assword: ", NULL)) {

                                /* If the user does not have a password set,
                                 * passwd will immediately ask for the new password,
                                 * so skip the AUTH phase */
                                if (is_string_complete (str->str, "new", "New", NULL)) {
                                        gchar *pw;

                                        passwd_handler->backend_state = PASSWD_STATE_NEW;

                                        /* since passwd didn't ask for our old password
                                         * in this case, simply remove it from the queue */
                                        pw = g_queue_pop_head (passwd_handler->backend_stdin_queue);
                                        g_free (pw);

                                        /* Pop the IO queue, i.e. send new password */
                                        io_queue_pop (passwd_handler->backend_stdin_queue, passwd_handler->backend_stdin);

                                } else {

                                        passwd_handler->backend_state = PASSWD_STATE_AUTH;

                                        /* Pop the IO queue, i.e. send current password */
                                        io_queue_pop (passwd_handler->backend_stdin_queue, passwd_handler->backend_stdin);
                                }

                                reinit = TRUE;
                        }
                        break;
                default:
                        /* Passwd has returned an error */
                        reinit = TRUE;
                        break;
        }

        if (reinit) {
                g_string_free (str, TRUE);
                str = NULL;
        }

        /* Continue calling us */
        return TRUE;
}

/*
 * }} Backend communication code
 */

/* Adds the current password to the IO queue */
static void
authenticate (PasswdHandler *passwd_handler)
{
        gchar   *s;

        s = g_strdup_printf ("%s\n", passwd_handler->current_password);

        g_queue_push_tail (passwd_handler->backend_stdin_queue, s);
}

/* Adds the new password twice to the IO queue */
static void
update_password (PasswdHandler *passwd_handler)
{
        gchar   *s;

        s = g_strdup_printf ("%s\n", passwd_handler->new_password);

        g_queue_push_tail (passwd_handler->backend_stdin_queue, s);
        /* We need to allocate new space because io_queue_pop() g_free()s
         * every element of the queue after it's done */
        g_queue_push_tail (passwd_handler->backend_stdin_queue, g_strdup (s));
}


PasswdHandler *
passwd_init (void)
{
        PasswdHandler *passwd_handler;

        passwd_handler = g_new0 (PasswdHandler, 1);

        /* Initialize backend_pid. -1 means the backend is not running */
        passwd_handler->backend_pid = -1;

        /* Initialize IO Channels */
        passwd_handler->backend_stdin = NULL;
        passwd_handler->backend_stdout = NULL;

        /* Initialize write queue */
        passwd_handler->backend_stdin_queue = g_queue_new ();

        /* Initialize watchers */
        passwd_handler->backend_child_watch_id = 0;
        passwd_handler->backend_stdout_watch_id = 0;

        /* Initialize backend state */
        passwd_handler->backend_state = PASSWD_STATE_NONE;
        passwd_handler->changing_password = FALSE;

        return passwd_handler;
}

void
passwd_destroy (PasswdHandler *passwd_handler)
{
        g_queue_free (passwd_handler->backend_stdin_queue);
        stop_passwd (passwd_handler);
        g_free (passwd_handler);
}

void
passwd_authenticate (PasswdHandler *passwd_handler,
                     const char    *current_password,
                     PasswdCallback cb,
                     const gpointer user_data)
{
        GError *error = NULL;

        /* Don't stop if we've already started chaging password */
        if (passwd_handler->changing_password)
                return;

        /* Clear data from possible previous attempts to change password */
        passwd_handler->new_password = NULL;
        passwd_handler->chpasswd_cb = NULL;
        passwd_handler->chpasswd_cb_data = NULL;
        g_queue_foreach (passwd_handler->backend_stdin_queue, (GFunc) g_free, NULL);
        g_queue_clear (passwd_handler->backend_stdin_queue);

        passwd_handler->current_password = current_password;
        passwd_handler->auth_cb = cb;
        passwd_handler->auth_cb_data = user_data;

        /* Spawn backend */
        stop_passwd (passwd_handler);

        if (!spawn_passwd (passwd_handler, &error)) {
                g_warning ("%s", error->message);
                g_error_free (error);

                return;
        }

        authenticate (passwd_handler);

        /* Our IO watcher should now handle the rest */
}

gboolean
passwd_change_password (PasswdHandler *passwd_handler,
                        const char    *new_password,
                        PasswdCallback cb,
                        const gpointer user_data)
{
        GError *error = NULL;

        passwd_handler->changing_password = TRUE;

        passwd_handler->new_password = new_password;
        passwd_handler->chpasswd_cb = cb;
        passwd_handler->chpasswd_cb_data = user_data;

        /* Stop passwd if an error occured and it is still running */
        if (passwd_handler->backend_state == PASSWD_STATE_ERR) {

                /* Stop passwd, free resources */
                stop_passwd (passwd_handler);
        }

        /* Check that the backend is still running, or that an error
         * has occured but it has not yet exited */
        if (passwd_handler->backend_pid == -1) {
                /* If it is not, re-run authentication */

                /* Spawn backend */
                stop_passwd (passwd_handler);

                if (!spawn_passwd (passwd_handler, &error)) {
                        g_warning ("%s", error->message);
                        g_error_free (error);

                        return FALSE;
                }

                /* Add current and new passwords to queue */
                authenticate (passwd_handler);
                update_password (passwd_handler);
        } else {
                /* Only add new passwords to queue */
                update_password (passwd_handler);
        }

        /* Pop new password through the backend.
         * If user has no password, popping the queue would output current
         * password, while 'passwd' is waiting for the new one. So wait for
         * io_watch_stdout() to remove current password from the queue,
         * and output the new one for us.
         */
        if (passwd_handler->current_password)
                io_queue_pop (passwd_handler->backend_stdin_queue, passwd_handler->backend_stdin);

        /* Our IO watcher should now handle the rest */

        return TRUE;
}