Fix UTIME_OMIT handling
[dragonfly.git] / usr.bin / passwd / passwd.c
blobc951923d140c3facc82661bd39510a27869d8079
1 /*-
2 * Copyright (c) 2002 Networks Associates Technologies, Inc.
3 * All rights reserved.
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.
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.
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.
34 * $FreeBSD: src/usr.bin/passwd/passwd.c,v 1.23 2003/04/18 21:27:09 nectar Exp $
37 #include <sys/param.h>
39 #include <err.h>
40 #include <pwd.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <syslog.h>
44 #include <unistd.h>
46 #include <security/pam_appl.h>
47 #include <security/openpam.h>
49 static pam_handle_t *pamh;
50 static struct pam_conv pamc = {
51 openpam_ttyconv,
52 NULL
55 static char *yp_domain;
56 static char *yp_host;
58 static void
59 usage(void)
61 fprintf(stderr, "usage: passwd [-ly] [-d domain] [-h host] [user]\n");
62 exit(1);
65 int
66 main(int argc, char **argv)
68 char hostname[MAXHOSTNAMELEN];
69 struct passwd *pwd = NULL;
70 int o, pam_err;
71 uid_t uid;
73 while ((o = getopt(argc, argv, "d:h:loy")) != -1)
74 switch (o) {
75 case 'd':
76 yp_domain = optarg;
77 break;
78 case 'h':
79 yp_host = optarg;
80 break;
81 case 'l':
82 case 'o':
83 case 'y':
84 /* compatibility */
85 break;
86 default:
87 usage();
90 argc -= optind;
91 argv += optind;
93 uid = getuid();
95 switch (argc) {
96 case 0:
97 if ((pwd = getpwuid(uid)) == NULL)
98 errx(1, "who are you?");
99 break;
100 case 1:
101 if ((pwd = getpwnam(*argv)) == NULL)
102 errx(1, "%s: no such user", *argv);
103 break;
104 default:
105 usage();
108 if (uid != 0 && uid != pwd->pw_uid)
109 errx(1, "permission denied");
111 /* check where the user's from */
112 switch (pwd->pw_fields & _PWF_SOURCE) {
113 case _PWF_FILES:
114 fprintf(stderr, "Changing local password for %s\n",
115 pwd->pw_name);
116 break;
117 case _PWF_NIS:
118 fprintf(stderr, "Changing NIS password for %s\n",
119 pwd->pw_name);
120 break;
121 default:
122 fprintf(stderr, "Changing password for %s\n",
123 pwd->pw_name);
126 #define pam_check(func) do { \
127 if (pam_err != PAM_SUCCESS) { \
128 if (pam_err == PAM_AUTH_ERR || pam_err == PAM_PERM_DENIED || \
129 pam_err == PAM_AUTHTOK_RECOVERY_ERR) \
130 warnx("sorry"); \
131 else \
132 warnx("%s(): %s", func, pam_strerror(pamh, pam_err)); \
133 goto end; \
135 } while (0)
137 /* initialize PAM */
138 pam_err = pam_start("passwd", pwd->pw_name, &pamc, &pamh);
139 pam_check("pam_start");
141 pam_err = pam_set_item(pamh, PAM_TTY, ttyname(STDERR_FILENO));
142 pam_check("pam_set_item");
143 gethostname(hostname, sizeof hostname);
144 pam_err = pam_set_item(pamh, PAM_RHOST, hostname);
145 pam_check("pam_set_item");
146 pam_err = pam_set_item(pamh, PAM_RUSER, getlogin());
147 pam_check("pam_set_item");
149 /* set YP domain and host */
150 pam_err = pam_set_data(pamh, "yp_domain", yp_domain, NULL);
151 pam_check("pam_set_data");
152 pam_err = pam_set_data(pamh, "yp_server", yp_host, NULL);
153 pam_check("pam_set_data");
155 /* set new password */
156 pam_err = pam_chauthtok(pamh, 0);
157 pam_check("pam_chauthtok");
159 end:
160 pam_end(pamh, pam_err);
161 exit(pam_err == PAM_SUCCESS ? 0 : 1);