Initialize Tizen 2.3
[external/shadow-utils.git] / libmisc / pam_pass.c
1 /*
2  * Copyright (c) 1997 - 1999, Marek Michałkiewicz
3  * Copyright (c) 2001 - 2005, Tomasz Kłoczko
4  * Copyright (c) 2008       , Nicolas François
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the copyright holders or contributors may not be used to
16  *    endorse or promote products derived from this software without
17  *    specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
23  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include <config.h>
33
34 #ifdef USE_PAM
35
36 #ident "$Id: pam_pass.c 2870 2009-05-09 13:15:17Z nekral-guest $"
37
38
39 /*
40  * Change the user's password using PAM.
41  */
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <sys/types.h>
46 #include "defines.h"
47 #include "pam_defs.h"
48 #include "prototypes.h"
49
50 void do_pam_passwd (const char *user, bool silent, bool change_expired)
51 {
52         pam_handle_t *pamh = NULL;
53         int flags = 0, ret;
54
55         if (silent)
56                 flags |= PAM_SILENT;
57         if (change_expired)
58                 flags |= PAM_CHANGE_EXPIRED_AUTHTOK;
59
60         ret = pam_start ("passwd", user, &conv, &pamh);
61         if (ret != PAM_SUCCESS) {
62                 fprintf (stderr,
63                          _("passwd: pam_start() failed, error %d\n"), ret);
64                 exit (10);      /* XXX */
65         }
66
67         ret = pam_chauthtok (pamh, flags);
68         if (ret != PAM_SUCCESS) {
69                 fprintf (stderr, _("passwd: %s\n"), pam_strerror (pamh, ret));
70                 fputs (_("passwd: password unchanged\n"), stderr);
71                 pam_end (pamh, ret);
72                 exit (10);      /* XXX */
73         }
74
75         fputs (_("passwd: password updated successfully\n"), stderr);
76         (void) pam_end (pamh, PAM_SUCCESS);
77 }
78 #else                           /* !USE_PAM */
79 extern int errno;               /* warning: ANSI C forbids an empty source file */
80 #endif                          /* !USE_PAM */