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 <syslog.h> 44 #include <unistd.h> 45 46 #include <security/pam_appl.h> 47 #include <security/openpam.h> 48 49 static pam_handle_t *pamh; 50 static struct pam_conv pamc = { 51 openpam_ttyconv, 52 NULL 53 }; 54 55 static char *yp_domain; 56 static char *yp_host; 57 58 static void 59 usage(void) 60 { 61 fprintf(stderr, "usage: passwd [-ly] [-d domain] [-h host] [user]\n"); 62 exit(1); 63 } 64 65 int 66 main(int argc, char *argv[]) 67 { 68 char hostname[MAXHOSTNAMELEN]; 69 struct passwd *pwd; 70 int o, pam_err; 71 uid_t uid; 72 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(); 88 } 89 90 argc -= optind; 91 argv += optind; 92 93 uid = getuid(); 94 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(); 106 } 107 108 if (uid != 0 && uid != pwd->pw_uid) 109 errx(1, "permission denied"); 110 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 case _PWF_HESIOD: 122 errx(1, "can't change Hesiod password"); 123 break; 124 default: 125 /* specieist! */ 126 errx(1, "can't change little green men's passwords (0x%x)", 127 pwd->pw_fields); 128 } 129 130 #define pam_check(func) do { \ 131 if (pam_err != PAM_SUCCESS) { \ 132 if (pam_err == PAM_AUTH_ERR || pam_err == PAM_PERM_DENIED || \ 133 pam_err == PAM_AUTHTOK_RECOVERY_ERR) \ 134 warnx("sorry"); \ 135 else \ 136 warnx("%s(): %s", func, pam_strerror(pamh, pam_err)); \ 137 goto end; \ 138 } \ 139 } while (0) 140 141 /* initialize PAM */ 142 pam_err = pam_start("passwd", pwd->pw_name, &pamc, &pamh); 143 pam_check("pam_start"); 144 145 pam_err = pam_set_item(pamh, PAM_TTY, ttyname(STDERR_FILENO)); 146 pam_check("pam_set_item"); 147 gethostname(hostname, sizeof hostname); 148 pam_err = pam_set_item(pamh, PAM_RHOST, hostname); 149 pam_check("pam_set_item"); 150 pam_err = pam_set_item(pamh, PAM_RUSER, getlogin()); 151 pam_check("pam_set_item"); 152 153 /* set YP domain and host */ 154 pam_err = pam_set_data(pamh, "yp_domain", yp_domain, NULL); 155 pam_check("pam_set_data"); 156 pam_err = pam_set_data(pamh, "yp_server", yp_host, NULL); 157 pam_check("pam_set_data"); 158 159 /* set new password */ 160 pam_err = pam_chauthtok(pamh, 0); 161 pam_check("pam_chauthtok"); 162 163 end: 164 pam_end(pamh, pam_err); 165 exit(pam_err == PAM_SUCCESS ? 0 : 1); 166 } 167