示例#1
0
kim_error kim_identity_change_password_common (kim_identity    in_identity,
                                               kim_boolean     in_old_password_expired,
                                               kim_ui_context *in_ui_context,
                                               kim_string     *out_new_password)
{
    kim_error err = KIM_NO_ERROR;
    kim_boolean done = 0;

    if (!err && !in_identity  ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_ui_context) { err = check_error (KIM_NULL_PARAMETER_ERR); }

    while (!err && !done) {
        char *old_password = NULL;
        char *new_password = NULL;
        char *verify_password = NULL;
        kim_error rejected_err = KIM_NO_ERROR;
        kim_string rejected_message = NULL;
        kim_string rejected_description = NULL;
        kim_boolean was_prompted = 0;   /* ignore because we always prompt */

        err = kim_ui_change_password (in_ui_context,
                                      in_identity,
                                      in_old_password_expired,
                                      &old_password,
                                      &new_password,
                                      &verify_password);

        if (!err) {
            kim_comparison comparison;

            err = kim_string_compare (new_password,
                                      verify_password,
                                      &comparison);
            if (!err && !kim_comparison_is_equal_to (comparison)) {
                err = check_error (KIM_PASSWORD_MISMATCH_ERR);
            }
        }

        if (!err) {
            kim_credential credential = NULL;

            if (in_ui_context->type == kim_ui_type_cli && in_ui_context->tcontext) {
                /* command line has already gotten the credentials for us */
                credential = (kim_credential) in_ui_context->tcontext;
            } else {
                err = kim_credential_create_for_change_password (&credential,
                                                                 in_identity,
                                                                 old_password,
                                                                 in_ui_context,
                                                                 &was_prompted);
            }

            if (!err) {
                err = kim_identity_change_password_with_credential (in_identity,
                                                                    credential,
                                                                    new_password,
                                                                    in_ui_context,
                                                                    &rejected_err,
                                                                    &rejected_message,
                                                                    &rejected_description);
            }

            kim_credential_free (&credential);
            if (in_ui_context->type == kim_ui_type_cli) {
                in_ui_context->tcontext = NULL; /* just freed our creds */
            }
        }

        if (!err && rejected_err) {
            /* Password rejected, report it to the user */
            err = kim_ui_handle_error (in_ui_context, in_identity,
                                       rejected_err,
                                       rejected_message,
                                       rejected_description);

        } else if (err && err != KIM_USER_CANCELED_ERR &&
                          err != KIM_DUPLICATE_UI_REQUEST_ERR) {
            /* New creds failed, report error to user.
             * Overwrite error so we loop and let the user try again.
             * The user always gets prompted so we always loop. */
            err = kim_ui_handle_kim_error (in_ui_context, in_identity,
                                           kim_ui_error_type_change_password,
                                           err);

        } else {
            /* password change succeeded or the user gave up */
            done = 1;

            if (!err && out_new_password) {
                err = kim_string_copy (out_new_password, new_password);
            }

            if (!err) {
                kim_error terr = KIM_NO_ERROR;
                kim_string saved_password = NULL;

                terr = kim_os_identity_get_saved_password (in_identity,
                                                           &saved_password);
                if (!terr) {
                    /* We changed the password and the user had their
                     * old password saved.  Update it. */
                    terr = kim_os_identity_set_saved_password (in_identity,
                                                               new_password);
                }

                kim_string_free (&saved_password);
            }

            if (err == KIM_DUPLICATE_UI_REQUEST_ERR) { err = KIM_NO_ERROR; }
        }

        kim_string_free (&rejected_message);
        kim_string_free (&rejected_description);

        kim_ui_free_string (in_ui_context, &old_password);
        kim_ui_free_string (in_ui_context, &new_password);
        kim_ui_free_string (in_ui_context, &verify_password);
    }

    return check_error (err);
}
示例#2
0
krb5_error_code kim_ui_prompter (krb5_context  in_krb5_context,
                                 void         *in_context,
                                 const char   *in_name,
                                 const char   *in_banner,
                                 int           in_num_prompts,
                                 krb5_prompt   in_prompts[])
{
    kim_error err = KIM_NO_ERROR;
    krb5_prompt_type *types = NULL;
    kim_ui_context *context = (kim_ui_context *) in_context;
    int i;

    if (!err && !in_krb5_context) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_context     ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    if (!err && !in_prompts     ) { err = check_error (KIM_NULL_PARAMETER_ERR); }

    if (!err) {
        types = krb5_get_prompt_types (in_krb5_context);
        if (!types) { err = check_error (KIM_NULL_PARAMETER_ERR); }
    }

    for (i = 0; !err && i < in_num_prompts; i++) {
        char *reply = NULL;
        kim_prompt_type type = kim_ui_ptype2ktype (types[i]);
        kim_boolean got_saved_password = 0;

        if (type == kim_prompt_type_password) {
            /* Check for saved password on OSes that support it */
            kim_error terr = KIM_NO_ERROR;

            terr = kim_os_identity_get_saved_password (context->identity,
                                                       (kim_string *) &reply);
            if (!terr && reply) { got_saved_password = 1; }
        }

        if (!got_saved_password) {
            kim_boolean save_reply = FALSE;
            kim_boolean allow_save_password = kim_os_identity_allow_save_password ();

            context->prompt_count++;

            err = kim_ui_init_lazy (in_context);

            if (!err) {
                if (context->type == kim_ui_type_gui_plugin) {
                    err = kim_ui_plugin_auth_prompt (context,
                                                     context->identity,
                                                     type,
                                                     allow_save_password,
                                                     in_prompts[i].hidden,
                                                     in_name,
                                                     in_banner,
                                                     in_prompts[i].prompt,
                                                     &reply,
                                                     &save_reply);

#ifdef KIM_BUILTIN_UI
                } else if (context->type == kim_ui_type_gui_builtin) {
                    err = kim_os_ui_gui_auth_prompt (context,
                                                     context->identity,
                                                     type,
                                                     allow_save_password,
                                                     in_prompts[i].hidden,
                                                     in_name,
                                                     in_banner,
                                                     in_prompts[i].prompt,
                                                     &reply,
                                                     &save_reply);

                } else if (context->type == kim_ui_type_cli) {
                    err = kim_ui_cli_auth_prompt (context,
                                                  context->identity,
                                                  type,
                                                  allow_save_password,
                                                  in_prompts[i].hidden,
                                                  in_name,
                                                  in_banner,
                                                  in_prompts[i].prompt,
                                                  &reply,
                                                  &save_reply);
#endif /* KIM_BUILTIN_UI */

                } else {
                    err = check_error (KIM_NO_UI_ERR);
                }
            }

            if (!err && type == kim_prompt_type_password) {
                kim_string_free (&context->password_to_save);

                if (allow_save_password && save_reply) {
                    err = kim_string_copy (&context->password_to_save, reply);
                }
            }
        }

        if (!err) {
            uint32_t reply_len = strlen (reply);

            if ((reply_len + 1) > in_prompts[i].reply->length) {
                kim_debug_printf ("%s(): reply %d is too long (is %d, should be %d)\n",
                                  __FUNCTION__, i,
                                  reply_len, in_prompts[i].reply->length);
                reply_len = in_prompts[i].reply->length;
            }

            memmove (in_prompts[i].reply->data, reply, reply_len + 1);
            in_prompts[i].reply->length = reply_len;
        }

        /* Clean up reply buffer.  Saved passwords are allocated by KIM. */
        if (reply) {
           if (got_saved_password) {
               memset (reply, '\0', strlen (reply));
               kim_string_free ((kim_string *) &reply);
             } else {
                kim_ui_free_string (context, &reply);
            }
        }
    }

    return check_error (err);
}