ldb_tdb: factor out the (to be) common init code
[sfrench/samba-autobuild/.git] / nsswitch / pam_winbind.c
index 40f4f7a014f7c1e93f872a3e890757e736af6b29..63fede87c788c2d95e8d05bdcd93eae1a75e9c66 100644 (file)
@@ -174,6 +174,10 @@ static inline void textdomain_init(void)
 
 
 /* some syslogging */
+static void _pam_log_int(const pam_handle_t *pamh,
+                        int err,
+                        const char *format,
+                        va_list args) PRINTF_ATTRIBUTE(3, 0);
 
 #ifdef HAVE_PAM_VSYSLOG
 static void _pam_log_int(const pam_handle_t *pamh,
@@ -189,21 +193,26 @@ static void _pam_log_int(const pam_handle_t *pamh,
                         const char *format,
                         va_list args)
 {
-       char *format2 = NULL;
+       char *base = NULL;
+       va_list args2;
        const char *service;
        int ret;
 
+       va_copy(args2, args);
+
        pam_get_item(pamh, PAM_SERVICE, (const void **) &service);
 
-       ret = asprintf(&format2, "%s(%s): %s", MODULE_NAME, service, format);
+       ret = vasprintf(&base, format, args);
        if (ret == -1) {
                /* what else todo ? */
-               vsyslog(err, format, args);
+               vsyslog(err, format, args2);
+               va_end(args2);
                return;
        }
 
-       vsyslog(err, format2, args);
-       SAFE_FREE(format2);
+       syslog(err, "%s(%s): %s", MODULE_NAME, service, base);
+       SAFE_FREE(base);
+       va_end(args2);
 }
 #endif /* HAVE_PAM_VSYSLOG */
 
@@ -610,7 +619,7 @@ static const struct ntstatus_errors {
        {"NT_STATUS_OK",
                N_("Success")},
        {"NT_STATUS_BACKUP_CONTROLLER",
-               N_("No primary Domain Controler available")},
+               N_("No primary Domain Controller available")},
        {"NT_STATUS_DOMAIN_CONTROLLER_NOT_FOUND",
                N_("No domain controllers found")},
        {"NT_STATUS_NO_LOGON_SERVERS",
@@ -667,11 +676,11 @@ static const char *_get_ntstatus_error_string(const char *nt_status_string)
 
 static int converse(const pam_handle_t *pamh,
                    int nargs,
-                   struct pam_message **message,
+                   const struct pam_message **message,
                    struct pam_response **response)
 {
        int retval;
-       struct pam_conv *conv;
+       const struct pam_conv *conv;
 
        retval = pam_get_item(pamh, PAM_CONV, (const void **) &conv);
        if (retval == PAM_SUCCESS) {
@@ -690,7 +699,8 @@ static int _make_remark(struct pwb_context *ctx,
 {
        int retval = PAM_SUCCESS;
 
-       struct pam_message *pmsg[1], msg[1];
+       const struct pam_message *pmsg[1];
+       struct pam_message msg[1];
        struct pam_response *resp;
 
        if (ctx->flags & WINBIND_SILENT) {
@@ -710,6 +720,11 @@ static int _make_remark(struct pwb_context *ctx,
        return retval;
 }
 
+static int _make_remark_v(struct pwb_context *ctx,
+                         int type,
+                         const char *format,
+                         va_list args) PRINTF_ATTRIBUTE(3, 0);
+
 static int _make_remark_v(struct pwb_context *ctx,
                          int type,
                          const char *format,
@@ -843,7 +858,8 @@ static int wbc_auth_error_to_pam_error(struct pwb_context *ctx,
 #if defined(HAVE_PAM_RADIO_TYPE)
 static bool _pam_winbind_change_pwd(struct pwb_context *ctx)
 {
-       struct pam_message msg, *pmsg;
+       struct pam_message msg;
+       const struct pam_message *pmsg;
        struct pam_response *resp = NULL;
        int ret;
        bool retval = false;
@@ -1002,7 +1018,6 @@ static bool _pam_send_password_expiry_message(struct pwb_context *ctx,
 
 static void _pam_warn_password_expiry(struct pwb_context *ctx,
                                      const struct wbcAuthUserInfo *info,
-                                     const struct wbcUserPasswordPolicyInfo *policy,
                                      int warn_pwd_expire,
                                      bool *already_expired,
                                      bool *change_pwd)
@@ -1010,7 +1025,7 @@ static void _pam_warn_password_expiry(struct pwb_context *ctx,
        time_t now = time(NULL);
        time_t next_change = 0;
 
-       if (!info || !policy) {
+       if (info == NULL) {
                return;
        }
 
@@ -1042,23 +1057,6 @@ static void _pam_warn_password_expiry(struct pwb_context *ctx,
                return;
        }
 
-       /* now check for the global password policy */
-       /* good catch from Ralf Haferkamp: an expiry of "never" is translated
-        * to -1 */
-       if ((policy->expire == (int64_t)-1) ||
-           (policy->expire == 0)) {
-               return;
-       }
-
-       next_change = info->pass_last_set_time + policy->expire;
-
-       if (_pam_send_password_expiry_message(ctx, next_change, now,
-                                             warn_pwd_expire,
-                                             already_expired,
-                                             change_pwd)) {
-               return;
-       }
-
        /* no warning sent */
 }
 
@@ -1694,23 +1692,17 @@ static int winbind_auth_request(struct pwb_context *ctx,
                                const int warn_pwd_expire,
                                struct wbcAuthErrorInfo **p_error,
                                struct wbcLogonUserInfo **p_info,
-                               struct wbcUserPasswordPolicyInfo **p_policy,
                                time_t *pwd_last_set,
                                char **user_ret)
 {
        wbcErr wbc_status;
-
        struct wbcLogonUserParams logon;
        char membership_of[1024];
        uid_t user_uid = -1;
-       uint32_t flags = WBFLAG_PAM_INFO3_TEXT |
-                        WBFLAG_PAM_GET_PWD_POLICY;
-
+       uint32_t flags = WBFLAG_PAM_INFO3_TEXT;
        struct wbcLogonUserInfo *info = NULL;
        struct wbcAuthUserInfo *user_info = NULL;
        struct wbcAuthErrorInfo *error = NULL;
-       struct wbcUserPasswordPolicyInfo *policy = NULL;
-
        int ret = PAM_AUTH_ERR;
        int i;
        const char *codes[] = {
@@ -1843,7 +1835,7 @@ static int winbind_auth_request(struct pwb_context *ctx,
                                     &logon,
                                     &info,
                                     &error,
-                                    &policy);
+                                    NULL);
        ret = wbc_auth_error_to_pam_error(ctx, error, wbc_status,
                                          user, "wbcLogonUser");
        wbcFreeMemory(logon.blobs);
@@ -1861,10 +1853,6 @@ static int winbind_auth_request(struct pwb_context *ctx,
                *p_info = info;
        }
 
-       if (p_policy && policy) {
-               *p_policy = policy;
-       }
-
        if (p_error && error) {
                /* We want to process the error in the caller. */
                *p_error = error;
@@ -1879,13 +1867,13 @@ static int winbind_auth_request(struct pwb_context *ctx,
                }
        }
 
-       if ((ret == PAM_SUCCESS) && user_info && policy && info) {
+       if ((ret == PAM_SUCCESS) && user_info && info) {
 
                bool already_expired = false;
                bool change_pwd = false;
 
                /* warn a user if the password is about to expire soon */
-               _pam_warn_password_expiry(ctx, user_info, policy,
+               _pam_warn_password_expiry(ctx, user_info,
                                          warn_pwd_expire,
                                          &already_expired,
                                          &change_pwd);
@@ -1893,15 +1881,15 @@ static int winbind_auth_request(struct pwb_context *ctx,
                if (already_expired == true) {
 
                        SMB_TIME_T last_set = user_info->pass_last_set_time;
+                       SMB_TIME_T must_set = user_info->pass_must_change_time;
 
                        _pam_log_debug(ctx, LOG_DEBUG,
                                       "Password has expired "
                                       "(Password was last set: %lld, "
-                                      "the policy says it should expire here "
+                                      "it must be changed here "
                                       "%lld (now it's: %ld))\n",
                                       (long long int)last_set,
-                                      (long long int)last_set +
-                                      policy->expire,
+                                      (long long int)must_set,
                                       (long)time(NULL));
 
                        return PAM_AUTHTOK_EXPIRED;
@@ -1940,9 +1928,6 @@ static int winbind_auth_request(struct pwb_context *ctx,
        if (info && !p_info) {
                wbcFreeMemory(info);
        }
-       if (policy && !p_policy) {
-               wbcFreeMemory(policy);
-       }
 
        return ret;
 }
@@ -2194,7 +2179,8 @@ static int _winbind_read_password(struct pwb_context *ctx,
         */
 
        {
-               struct pam_message msg[3], *pmsg[3];
+               struct pam_message msg[3];
+               const struct pam_message *pmsg[3];
                struct pam_response *resp;
                int i, replies;
 
@@ -2476,10 +2462,14 @@ static char* winbind_upn_to_username(struct pwb_context *ctx,
        if (!name) {
                return NULL;
        }
-       if ((p = strchr(name, '@')) != NULL) {
-               *p = 0;
-               domain = p + 1;
+
+       p = strchr(name, '@');
+       if (p == NULL) {
+               TALLOC_FREE(name);
+               return NULL;
        }
+       *p = '\0';
+       domain = p + 1;
 
        /* Convert the UPN to a SID */
 
@@ -2734,8 +2724,7 @@ int pam_sm_authenticate(pam_handle_t *pamh, int flags,
        /* Now use the username to look up password */
        retval = winbind_auth_request(ctx, real_username, password,
                                      member, cctype, warn_pwd_expire,
-                                     NULL, NULL, NULL,
-                                     NULL, &username_ret);
+                                     NULL, NULL, NULL, &username_ret);
 
        if (retval == PAM_NEW_AUTHTOK_REQD ||
            retval == PAM_AUTHTOK_EXPIRED) {
@@ -2897,7 +2886,8 @@ int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags,
                        ret = atoi(tmp);
                        switch (ret) {
                        case PAM_AUTHTOK_EXPIRED:
-                               /* fall through, since new token is required in this case */
+                               /* Since new token is required in this case */
+                               FALL_THROUGH;
                        case PAM_NEW_AUTHTOK_REQD:
                                _pam_log(ctx, LOG_WARNING,
                                         "pam_sm_acct_mgmt success but %s is set",
@@ -3018,7 +3008,7 @@ static bool _pam_require_krb5_auth_after_chauthtok(struct pwb_context *ctx,
         * --- BoYang
         * */
 
-       char *new_authtok_reqd_during_auth = NULL;
+       const char *new_authtok_reqd_during_auth = NULL;
        struct passwd *pwd = NULL;
 
        pam_get_data(ctx->pamh, PAM_WINBIND_NEW_AUTHTOK_REQD_DURING_AUTH,
@@ -3145,7 +3135,7 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
 
                ret = winbind_auth_request(ctx, user, pass_old,
                                           NULL, NULL, 0,
-                                          &error, NULL, NULL,
+                                          &error, NULL,
                                           &pwdlastset_prelim, NULL);
 
                if (ret != PAM_ACCT_EXPIRED &&
@@ -3253,7 +3243,6 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
                        const char *cctype = NULL;
                        int warn_pwd_expire;
                        struct wbcLogonUserInfo *info = NULL;
-                       struct wbcUserPasswordPolicyInfo *policy = NULL;
 
                        member = get_member_from_config(ctx);
                        cctype = get_krb5_cc_type_from_config(ctx);
@@ -3269,7 +3258,7 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
 
                        ret = winbind_auth_request(ctx, user, pass_new,
                                                   member, cctype, 0,
-                                                  &error, &info, &policy,
+                                                  &error, &info,
                                                   NULL, &username_ret);
                        pass_old = pass_new = NULL;
 
@@ -3283,7 +3272,7 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
 
                                /* warn a user if the password is about to
                                 * expire soon */
-                               _pam_warn_password_expiry(ctx, user_info, policy,
+                               _pam_warn_password_expiry(ctx, user_info,
                                                          warn_pwd_expire,
                                                          NULL, NULL);
 
@@ -3309,7 +3298,6 @@ int pam_sm_chauthtok(pam_handle_t * pamh, int flags,
                                wbcFreeMemory(info->blobs);
                        }
                        wbcFreeMemory(info);
-                       wbcFreeMemory(policy);
 
                        goto out;
                }