]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/passwd/passwd.c
#include <stdlib.h> for exit(3) prototype.
[FreeBSD/FreeBSD.git] / usr.bin / passwd / passwd.c
1 /*-
2  * Copyright (c) 2002 Networks Associates Technologies, Inc.
3  * All rights reserved.
4  *
5  * This software was developed for the FreeBSD Project by ThinkSec AS and
6  * NAI Labs, the Security Research Division of Network Associates, Inc.
7  * under DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the
8  * DARPA CHATS research program.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. The name of the author may not be used to endorse or promote
19  *    products derived from this software without specific prior written
20  *    permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37
38 #include <sys/param.h>
39
40 #include <err.h>
41 #include <pwd.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <syslog.h>
45 #include <unistd.h>
46
47 #include <security/pam_appl.h>
48 #include <security/openpam.h>
49
50 static pam_handle_t *pamh;
51 static struct pam_conv pamc = {
52         openpam_ttyconv,
53         NULL
54 };
55
56 static char     *yp_domain;
57 static char     *yp_host;
58
59 static void
60 usage(void)
61 {
62         fprintf(stderr, "usage: passwd [-ly] [-d domain] [-h host] [user]\n");
63         exit(1);
64 }
65
66 int
67 main(int argc, char *argv[])
68 {
69         char hostname[MAXHOSTNAMELEN];
70         struct passwd *pwd;
71         int o, pam_err;
72         uid_t uid;
73
74         while ((o = getopt(argc, argv, "d:h:loy")) != -1)
75                 switch (o) {
76                 case 'd':
77                         yp_domain = optarg;
78                         break;
79                 case 'h':
80                         yp_host = optarg;
81                         break;
82                 case 'l':
83                 case 'o':
84                 case 'y':
85                         /* compatibility */
86                         break;
87                 default:
88                         usage();
89                 }
90
91         argc -= optind;
92         argv += optind;
93
94         uid = getuid();
95
96         switch (argc) {
97         case 0:
98                 if ((pwd = getpwuid(uid)) == NULL)
99                         errx(1, "who are you?");
100                 break;
101         case 1:
102                 if ((pwd = getpwnam(*argv)) == NULL)
103                         errx(1, "%s: no such user", *argv);
104                 break;
105         default:
106                 usage();
107         }
108
109         if (uid != 0 && uid != pwd->pw_uid)
110                 errx(1, "permission denied");
111
112         /* check where the user's from */
113         switch (pwd->pw_fields & _PWF_SOURCE) {
114         case _PWF_FILES:
115                 fprintf(stderr, "Changing local password for %s\n",
116                     pwd->pw_name);
117                 break;
118         case _PWF_NIS:
119                 fprintf(stderr, "Changing NIS password for %s\n",
120                     pwd->pw_name);
121                 break;
122         case _PWF_HESIOD:
123                 errx(1, "can't change Hesiod password");
124                 break;
125         default:
126                 /* specieist! */
127                 errx(1, "can't change little green men's passwords (0x%x)",
128                     pwd->pw_fields);
129         }
130
131 #define pam_check(func) do { \
132         if (pam_err != PAM_SUCCESS) { \
133                 if (pam_err == PAM_AUTH_ERR || pam_err == PAM_PERM_DENIED || \
134                     pam_err == PAM_AUTHTOK_RECOVERY_ERR) \
135                         warnx("sorry"); \
136                 else \
137                         warnx("%s(): %s", func, pam_strerror(pamh, pam_err)); \
138                 goto end; \
139         } \
140 } while (0)
141
142         /* initialize PAM */
143         pam_err = pam_start("passwd", pwd->pw_name, &pamc, &pamh);
144         pam_check("pam_start");
145
146         pam_err = pam_set_item(pamh, PAM_TTY, ttyname(STDERR_FILENO));
147         pam_check("pam_set_item");
148         gethostname(hostname, sizeof hostname);
149         pam_err = pam_set_item(pamh, PAM_RHOST, hostname);
150         pam_check("pam_set_item");
151         pam_err = pam_set_item(pamh, PAM_RUSER, getlogin());
152         pam_check("pam_set_item");
153
154         /* set YP domain and host */
155         pam_err = pam_set_data(pamh, "yp_domain", yp_domain, NULL);
156         pam_check("pam_set_data");
157         pam_err = pam_set_data(pamh, "yp_server", yp_host, NULL);
158         pam_check("pam_set_data");
159
160         /* set new password */
161         pam_err = pam_chauthtok(pamh, 0);
162         pam_check("pam_chauthtok");
163
164  end:
165         pam_end(pamh, pam_err);
166         exit(pam_err == PAM_SUCCESS ? 0 : 1);
167 }