| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | #include <afsconfig.h> |
| 11 | #include <afs/param.h> |
| 12 | |
| 13 | #include <roken.h> |
| 14 | |
| 15 | #include <security/pam_appl.h> |
| 16 | #include <security/pam_modules.h> |
| 17 | |
| 18 | #include <afs/kautils.h> |
| 19 | |
| 20 | #include "afs_message.h" |
| 21 | #include "afs_util.h" |
| 22 | |
| 23 | |
| 24 | |
| 25 | #define RET(x){ retcode = (x); goto out; } { retcode = (x); goto out; } |
| 26 | |
| 27 | #if defined(AFS_KERBEROS_ENV) |
| 28 | extern char *ktc_tkt_string(); |
| 29 | #endif |
| 30 | |
| 31 | extern int |
| 32 | pam_sm_setcred(pam_handle_t * pamh, int flags, int argc, const char **argv) |
| 33 | { |
| 34 | int retcode = PAM_SUCCESS; |
| 35 | int errcode = PAM_SUCCESS; |
| 36 | int origmask; |
| 37 | int logmask = LOG_UPTO(LOG_INFO)((1 << ((6)+1)) - 1); |
| 38 | int nowarn = 0; |
| 39 | int use_first_pass = 1; |
| 40 | int try_first_pass = 0; |
| 41 | int got_authtok = 0; |
| 42 | int ignore_uid = 0; |
| 43 | int no_unlog = 0; |
| 44 | uid_t ignore_uid_id = 0; |
| 45 | int refresh_token = 0; |
| 46 | int set_expires = 0; |
| 47 | int use_klog = 0; |
| 48 | int i; |
| 49 | PAM_CONST struct pam_conv *pam_convp = NULL((void *)0); |
| 50 | char my_password_buf[256]; |
| 51 | char *cell_ptr = NULL((void *)0); |
| 52 | char sbuffer[100]; |
| 53 | char *torch_password = NULL((void *)0); |
| 54 | int auth_ok = 0; |
| 55 | char *lh; |
| 56 | PAM_CONST char *user = NULL((void *)0); |
| 57 | const char *password = NULL((void *)0); |
| 58 | int password_expires = -1; |
| 59 | char *reason = NULL((void *)0); |
| 60 | struct passwd unix_pwd, *upwd = NULL((void *)0); |
| 61 | char upwd_buf[2048]; |
| 62 | |
| 63 | #ifndef AFS_SUN5_ENV |
| 64 | openlog(pam_afs_ident, LOG_CONS0x02, LOG_AUTH(4<<3)); |
| 65 | #endif |
| 66 | origmask = setlogmask(logmask); |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | for (i = 0; i < argc; i++) { |
| 72 | if (strcasecmp(argv[i], "debug") == 0) { |
| 73 | logmask |= LOG_MASK(LOG_DEBUG)(1 << (7)); |
| 74 | (void)setlogmask(logmask); |
| 75 | } else if (strcasecmp(argv[i], "nowarn") == 0) { |
| 76 | nowarn = 1; |
| 77 | } else if (strcasecmp(argv[i], "use_first_pass") == 0) { |
| 78 | use_first_pass = 1; |
| 79 | } else if (strcasecmp(argv[i], "try_first_pass") == 0) { |
| 80 | try_first_pass = 1; |
| 81 | } else if (strcasecmp(argv[i], "ignore_root") == 0) { |
| 82 | ignore_uid = 1; |
| 83 | ignore_uid_id = 0; |
| 84 | } else if (strcasecmp(argv[i], "ignore_uid") == 0) { |
| 85 | i++; |
| 86 | if (i == argc) { |
| 87 | pam_afs_syslog(LOG_ERR3, PAMAFS_IGNOREUID39, |
| 88 | "ignore_uid missing argument"); |
| 89 | ignore_uid = 0; |
| 90 | } else { |
| 91 | ignore_uid = 1; |
| 92 | ignore_uid_id = (uid_t) strtol(argv[i], (char **)NULL((void *)0), 10); |
| 93 | if ((0 > ignore_uid_id) || (ignore_uid_id > IGNORE_MAX1000)) { |
| 94 | ignore_uid = 0; |
| 95 | pam_afs_syslog(LOG_ERR3, PAMAFS_IGNOREUID39, argv[i]); |
| 96 | } |
| 97 | } |
| 98 | } else if (strcasecmp(argv[i], "cell") == 0) { |
| 99 | i++; |
| 100 | if (i == argc) { |
| 101 | pam_afs_syslog(LOG_ERR3, PAMAFS_OTHERCELL47, |
| 102 | "cell missing argument"); |
| 103 | } else { |
| 104 | cell_ptr = (char *)argv[i]; |
| 105 | pam_afs_syslog(LOG_INFO6, PAMAFS_OTHERCELL47, cell_ptr); |
| 106 | } |
| 107 | } else if (strcasecmp(argv[i], "no_unlog") == 0) { |
| 108 | no_unlog = 1; |
| 109 | } else if (strcasecmp(argv[i], "refresh_token") == 0) { |
| 110 | refresh_token = 1; |
| 111 | } else if (strcasecmp(argv[i], "set_token") == 0) { |
| 112 | ; |
| 113 | } else if (strcasecmp(argv[i], "dont_fork") == 0) { |
| 114 | ; |
| 115 | } else if (strcasecmp(argv[i], "use_klog") == 0) { |
| 116 | use_klog = 1; |
| 117 | } else if (strcasecmp(argv[i], "setenv_password_expires") == 0) { |
| 118 | set_expires = 1; |
| 119 | } else { |
| 120 | pam_afs_syslog(LOG_ERR3, PAMAFS_UNKNOWNOPT1, argv[i]); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | if (use_first_pass) |
| 125 | try_first_pass = 0; |
| 126 | |
| 127 | if (logmask && LOG_MASK(LOG_DEBUG)(1 << (7))) |
| 128 | pam_afs_syslog(LOG_DEBUG7, PAMAFS_OPTIONS17, nowarn, use_first_pass, |
| 129 | try_first_pass, ignore_uid, ignore_uid_id, 8, 8, 8, 8); |
| 130 | |
| 131 | errcode = pam_get_item(pamh, PAM_CONV, (PAM_CONST void **)&pam_convp); |
| 132 | if (errcode != PAM_SUCCESS) { |
| 133 | if (logmask && LOG_MASK(LOG_DEBUG)(1 << (7))) |
| 134 | pam_afs_syslog(LOG_DEBUG7, PAMAFS_NO_USER_INT18); |
| 135 | pam_convp = NULL((void *)0); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | if ((errcode = |
| 140 | pam_get_user(pamh, (PAM_CONST char **)&user, |
| 141 | "AFS username:")) != PAM_SUCCESS) { |
| 142 | pam_afs_syslog(LOG_ERR3, PAMAFS_NOUSER2, errcode); |
| 143 | RET(PAM_USER_UNKNOWN){ retcode = (PAM_USER_UNKNOWN); goto out; }; |
| 144 | } |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | #if defined(AFS_HPUX_ENV) || defined(AFS_DARWIN100_ENV) || defined(AFS_SUN5_ENV) |
| 154 | #if defined(AFS_HPUX110_ENV) || defined(AFS_DARWIN100_ENV) || defined(AFS_SUN5_ENV) |
| 155 | i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf), &upwd); |
| 156 | #else /* AFS_HPUX110_ENV */ |
| 157 | i = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf)); |
| 158 | if (i == 0) |
| 159 | upwd = &unix_pwd; |
| 160 | #endif /* AFS_HPUX110_ENV */ |
| 161 | if (ignore_uid && i == 0 && upwd && upwd->pw_uid <= ignore_uid_id) { |
| 162 | pam_afs_syslog(LOG_INFO6, PAMAFS_IGNORINGROOT26, user); |
| 163 | RET(PAM_AUTH_ERR){ retcode = (PAM_AUTH_ERR); goto out; }; |
| 164 | } |
| 165 | #else |
| 166 | #if defined(AFS_LINUX20_ENV) || defined(AFS_FBSD_ENV1) || defined(AFS_DFBSD_ENV) || defined(AFS_NBSD_ENV) |
| 167 | upwd = getpwnam(user); |
| 168 | #else |
| 169 | upwd = getpwnam_r(user, &unix_pwd, upwd_buf, sizeof(upwd_buf)); |
| 170 | #endif |
| 171 | if (ignore_uid && upwd != NULL((void *)0) && upwd->pw_uid <= ignore_uid_id) { |
| 172 | pam_afs_syslog(LOG_INFO6, PAMAFS_IGNORINGROOT26, user); |
| 173 | RET(PAM_AUTH_ERR){ retcode = (PAM_AUTH_ERR); goto out; }; |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | if (flags & PAM_DELETE_CRED) { |
| 178 | if (logmask && LOG_MASK(LOG_DEBUG)(1 << (7))) |
| 179 | pam_afs_syslog(LOG_DEBUG7, PAMAFS_DELCRED19, user); |
| 180 | |
| 181 | RET(PAM_SUCCESS){ retcode = (PAM_SUCCESS); goto out; }; |
| 182 | } else if (flags & PAM_REINITIALIZE_CRED) { |
| 183 | |
| 184 | if (logmask && LOG_MASK(LOG_DEBUG)(1 << (7))) |
| 185 | pam_afs_syslog(LOG_DEBUG7, PAMAFS_REINITCRED31, user); |
| 186 | RET(PAM_SUCCESS){ retcode = (PAM_SUCCESS); goto out; }; |
| 187 | |
| 188 | } else { |
| 189 | |
| 190 | if (logmask && LOG_MASK(LOG_DEBUG)(1 << (7))) |
| 191 | pam_afs_syslog(LOG_DEBUG7, PAMAFS_ESTABCRED22, user); |
| 192 | |
| 193 | errcode = pam_get_data(pamh, pam_afs_lh, (const void **)&password); |
| 194 | if (errcode != PAM_SUCCESS || password == NULL((void *)0)) { |
| 195 | if (use_first_pass) { |
| 196 | pam_afs_syslog(LOG_ERR3, PAMAFS_PASSWD_REQ4, user); |
| 197 | RET(PAM_AUTH_ERR){ retcode = (PAM_AUTH_ERR); goto out; }; |
| 198 | } |
| 199 | password = NULL((void *)0); |
| 200 | if (logmask && LOG_MASK(LOG_DEBUG)(1 << (7))) |
| 201 | pam_afs_syslog(LOG_DEBUG7, PAMAFS_NOFIRSTPASS8, user); |
| 202 | } else if (password[0] == '\0') { |
| 203 | |
| 204 | got_authtok = 1; |
| Value stored to 'got_authtok' is never read |
| 205 | |
| 206 | password = NULL((void *)0); |
| 207 | if (use_first_pass) { |
| 208 | pam_afs_syslog(LOG_ERR3, PAMAFS_PASSWD_REQ4, user); |
| 209 | RET(PAM_NEW_AUTHTOK_REQD){ retcode = (PAM_NEW_AUTHTOK_REQD); goto out; }; |
| 210 | } |
| 211 | if (logmask && LOG_MASK(LOG_DEBUG)(1 << (7))) |
| 212 | pam_afs_syslog(LOG_DEBUG7, PAMAFS_NILPASSWORD21, user); |
| 213 | } else { |
| 214 | if (logmask && LOG_MASK(LOG_DEBUG)(1 << (7))) |
| 215 | pam_afs_syslog(LOG_DEBUG7, PAMAFS_GOTPASS7, user); |
| 216 | got_authtok = 1; |
| 217 | } |
| 218 | if (!(use_first_pass || try_first_pass)) { |
| 219 | password = NULL((void *)0); |
| 220 | } |
| 221 | |
| 222 | try_auth: |
| 223 | if (password == NULL((void *)0)) { |
| 224 | char *prompt_password; |
| 225 | |
| 226 | if (use_first_pass) |
| 227 | RET(PAM_AUTH_ERR){ retcode = (PAM_AUTH_ERR); goto out; }; |
| 228 | if (try_first_pass) |
| 229 | try_first_pass = 0; |
| 230 | |
| 231 | if (pam_convp == NULL((void *)0) || pam_convp->conv == NULL((void *)0)) { |
| 232 | pam_afs_syslog(LOG_ERR3, PAMAFS_CANNOT_PROMPT6); |
| 233 | RET(PAM_AUTH_ERR){ retcode = (PAM_AUTH_ERR); goto out; }; |
| 234 | } |
| 235 | |
| 236 | errcode = |
| 237 | pam_afs_prompt(pam_convp, &prompt_password, 0, PAMAFS_PWD_PROMPT5); |
| 238 | if (errcode != PAM_SUCCESS || prompt_password == NULL((void *)0)) { |
| 239 | pam_afs_syslog(LOG_ERR3, PAMAFS_GETPASS_FAILED9); |
| 240 | RET(PAM_AUTH_ERR){ retcode = (PAM_AUTH_ERR); goto out; }; |
| 241 | } |
| 242 | if (prompt_password[0] == '\0') { |
| 243 | if (logmask && LOG_MASK(LOG_DEBUG)(1 << (7))) |
| 244 | pam_afs_syslog(LOG_DEBUG7, PAMAFS_NILPASSWORD21); |
| 245 | RET(PAM_NEW_AUTHTOK_REQD){ retcode = (PAM_NEW_AUTHTOK_REQD); goto out; }; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | |
| 250 | |
| 251 | |
| 252 | |
| 253 | |
| 254 | |
| 255 | strncpy(my_password_buf, prompt_password, sizeof(my_password_buf)); |
| 256 | my_password_buf[sizeof(my_password_buf) - 1] = '\0'; |
| 257 | memset(prompt_password, 0, strlen(prompt_password)); |
| 258 | free(prompt_password); |
| 259 | password = torch_password = my_password_buf; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | if ((!refresh_token) && (getPAG() == -1)) { |
| 266 | if (logmask && LOG_MASK(LOG_DEBUG)(1 << (7))) |
| 267 | syslog(LOG_DEBUG7, "New PAG created in pam_setcred()"); |
| 268 | setpag(); |
| 269 | #ifdef AFS_KERBEROS_ENV |
| 270 | ktc_newpag(); |
| 271 | #endif |
| 272 | } |
| 273 | |
| 274 | if (flags & PAM_REFRESH_CRED) { |
| 275 | if (use_klog) { |
| 276 | auth_ok = !do_klog(user, password, "00:00:01", cell_ptr); |
| 277 | ktc_ForgetAllTokens(); |
| 278 | } else { |
| 279 | if (ka_VerifyUserPassword(KA_USERAUTH_VERSION1, (char *)user, |
| 280 | NULL((void *)0), |
| 281 | cell_ptr, |
| 282 | (char*)password, |
| 283 | 0, |
| 284 | &reason |
| 285 | )) { |
| 286 | pam_afs_syslog(LOG_ERR3, PAMAFS_LOGIN_FAILED13, user, |
| 287 | reason); |
| 288 | } else { |
| 289 | auth_ok = 1; |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if (flags & PAM_ESTABLISH_CRED) { |
| 295 | if (use_klog) |
| 296 | auth_ok = !do_klog(user, password, NULL((void *)0), cell_ptr); |
| 297 | else { |
| 298 | if (ka_UserAuthenticateGeneral(KA_USERAUTH_VERSION1, (char *)user, |
| 299 | NULL((void *)0), |
| 300 | cell_ptr, |
| 301 | (char*)password, |
| 302 | 0, |
| 303 | &password_expires, 0, |
| 304 | &reason |
| 305 | )) { |
| 306 | pam_afs_syslog(LOG_ERR3, PAMAFS_LOGIN_FAILED13, user, |
| 307 | reason); |
| 308 | } else { |
| 309 | auth_ok = 1; |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | if (!auth_ok && try_first_pass) { |
| 315 | password = NULL((void *)0); |
| 316 | goto try_auth; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | if (auth_ok) { |
| 327 | if (set_expires && !use_klog && (password_expires >= 0)) { |
| 328 | strcpy(sbuffer, "PASSWORD_EXPIRES="); |
| 329 | strcat(sbuffer, cv2string(&sbuffer[100], password_expires)); |
| 330 | errcode = pam_putenv(pamh, sbuffer); |
| 331 | if (errcode != PAM_SUCCESS) |
| 332 | pam_afs_syslog(LOG_ERR3, PAMAFS_PASSEXPFAIL32, user); |
| 333 | } |
| 334 | #if defined(AFS_KERBEROS_ENV) |
| 335 | if (upwd) { |
| 336 | if (chown(ktc_tkt_string(), upwd->pw_uid, upwd->pw_gid) < 0) |
| 337 | pam_afs_syslog(LOG_ERR3, PAMAFS_CHOWNKRB33, user); |
| 338 | sprintf(sbuffer, "KRBTKFILE=%s", ktc_tkt_string()); |
| 339 | errcode = pam_putenv(pamh, sbuffer); |
| 340 | if (errcode != PAM_SUCCESS) |
| 341 | pam_afs_syslog(LOG_ERR3, PAMAFS_KRBFAIL34, user); |
| 342 | } |
| 343 | #endif |
| 344 | |
| 345 | RET(PAM_SUCCESS){ retcode = (PAM_SUCCESS); goto out; }; |
| 346 | } else { |
| 347 | RET(PAM_CRED_ERR){ retcode = (PAM_CRED_ERR); goto out; }; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | out: |
| 352 | if (password && torch_password) |
| 353 | memset(torch_password, 0, strlen(torch_password)); |
| 354 | (void)setlogmask(origmask); |
| 355 | #ifndef AFS_SUN5_ENV |
| 356 | closelog(); |
| 357 | #endif |
| 358 | return retcode; |
| 359 | } |