Ignore:
Timestamp:
Nov 27, 2012, 4:43:17 PM (13 years ago)
Author:
Silvan Scherrer
Message:

Samba Server: updated trunk to 3.6.0

Location:
trunk/server
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/server

  • trunk/server/source4/auth/credentials/credentials.c

    r414 r745  
    2525#include "librpc/gen_ndr/samr.h" /* for struct samrPassword */
    2626#include "auth/credentials/credentials.h"
    27 #include "auth/credentials/credentials_krb5.h"
    28 #include "auth/credentials/credentials_proto.h"
    2927#include "libcli/auth/libcli_auth.h"
    3028#include "lib/events/events.h"
    3129#include "param/param.h"
     30#include "system/filesys.h"
    3231
    3332/**
     
    3837{
    3938        struct cli_credentials *cred = talloc(mem_ctx, struct cli_credentials);
    40         if (!cred) {
     39        if (cred == NULL) {
    4140                return cred;
    4241        }
    4342
    44         cred->netlogon_creds = NULL;
    45         cred->machine_account_pending = false;
    4643        cred->workstation_obtained = CRED_UNINITIALISED;
    4744        cred->username_obtained = CRED_UNINITIALISED;
     
    5148        cred->ccache_obtained = CRED_UNINITIALISED;
    5249        cred->client_gss_creds_obtained = CRED_UNINITIALISED;
     50        cred->principal_obtained = CRED_UNINITIALISED;
     51        cred->keytab_obtained = CRED_UNINITIALISED;
    5352        cred->server_gss_creds_obtained = CRED_UNINITIALISED;
    54         cred->keytab_obtained = CRED_UNINITIALISED;
    55         cred->principal_obtained = CRED_UNINITIALISED;
    5653
    5754        cred->ccache_threshold = CRED_UNINITIALISED;
    5855        cred->client_gss_creds_threshold = CRED_UNINITIALISED;
    5956
     57        cred->workstation = NULL;
     58        cred->username = NULL;
     59        cred->password = NULL;
    6060        cred->old_password = NULL;
     61        cred->domain = NULL;
     62        cred->realm = NULL;
     63        cred->principal = NULL;
     64        cred->salt_principal = NULL;
     65        cred->impersonate_principal = NULL;
     66        cred->target_service = NULL;
     67
     68        cred->bind_dn = NULL;
     69
     70        cred->nt_hash = NULL;
     71
     72        cred->lm_response.data = NULL;
     73        cred->lm_response.length = 0;
     74        cred->nt_response.data = NULL;
     75        cred->nt_response.length = 0;
     76
     77        cred->ccache = NULL;
     78        cred->client_gss_creds = NULL;
     79        cred->keytab = NULL;
     80        cred->server_gss_creds = NULL;
     81
     82        cred->workstation_cb = NULL;
     83        cred->password_cb = NULL;
     84        cred->username_cb = NULL;
     85        cred->domain_cb = NULL;
     86        cred->realm_cb = NULL;
     87        cred->principal_cb = NULL;
     88
     89        cred->priv_data = NULL;
     90
     91        cred->netlogon_creds = NULL;
     92        cred->secure_channel_type = SEC_CHAN_NULL;
     93
     94        cred->kvno = 0;
     95
     96        cred->password_last_changed_time = 0;
     97
    6198        cred->smb_krb5_context = NULL;
    62         cred->salt_principal = NULL;
     99
     100        cred->machine_account_pending = false;
     101        cred->machine_account_pending_lp_ctx = NULL;
     102
    63103        cred->machine_account = false;
    64104
    65         cred->bind_dn = NULL;
    66 
    67105        cred->tries = 3;
     106
    68107        cred->callback_running = false;
    69108
    70109        cli_credentials_set_kerberos_state(cred, CRED_AUTO_USE_KERBEROS);
    71110        cli_credentials_set_gensec_features(cred, 0);
     111        cli_credentials_set_krb_forwardable(cred, CRED_AUTO_KRB_FORWARDABLE);
    72112
    73113        return cred;
     
    94134}
    95135
     136_PUBLIC_ void cli_credentials_set_krb_forwardable(struct cli_credentials *creds,
     137                                                  enum credentials_krb_forwardable krb_forwardable)
     138{
     139        creds->krb_forwardable = krb_forwardable;
     140}
     141
    96142_PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds)
    97143{
    98144        return creds->use_kerberos;
     145}
     146
     147_PUBLIC_ enum credentials_krb_forwardable cli_credentials_get_krb_forwardable(struct cli_credentials *creds)
     148{
     149        return creds->krb_forwardable;
    99150}
    100151
     
    185236 * @note Return value will never be NULL except by programmer error.
    186237 */
    187 _PUBLIC_ const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
     238const char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained)
    188239{
    189240        if (cred->machine_account_pending) {
     
    201252        }
    202253
    203         if (cred->principal_obtained < cred->username_obtained) {
     254        if (cred->principal_obtained < cred->username_obtained
     255            || cred->principal_obtained < MAX(cred->domain_obtained, cred->realm_obtained)) {
    204256                if (cred->domain_obtained > cred->realm_obtained) {
     257                        *obtained = MIN(cred->domain_obtained, cred->username_obtained);
    205258                        return talloc_asprintf(mem_ctx, "%s@%s",
    206259                                               cli_credentials_get_username(cred),
    207260                                               cli_credentials_get_domain(cred));
    208261                } else {
     262                        *obtained = MIN(cred->domain_obtained, cred->username_obtained);
    209263                        return talloc_asprintf(mem_ctx, "%s@%s",
    210264                                               cli_credentials_get_username(cred),
     
    212266                }
    213267        }
     268        *obtained = cred->principal_obtained;
    214269        return talloc_reference(mem_ctx, cred->principal);
     270}
     271
     272/**
     273 * Obtain the client principal for this credentials context.
     274 * @param cred credentials context
     275 * @retval The username set on this context.
     276 * @note Return value will never be NULL except by programmer error.
     277 */
     278_PUBLIC_ const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)
     279{
     280        enum credentials_obtained obtained;
     281        return cli_credentials_get_principal_and_obtained(cred, mem_ctx, &obtained);
    215282}
    216283
     
    614681{
    615682        cli_credentials_set_username(cred, "", CRED_UNINITIALISED);
    616         cli_credentials_set_domain(cred, lp_workgroup(lp_ctx), CRED_UNINITIALISED);
    617         cli_credentials_set_workstation(cred, lp_netbios_name(lp_ctx), CRED_UNINITIALISED);
    618         cli_credentials_set_realm(cred, lp_realm(lp_ctx), CRED_UNINITIALISED);
     683        cli_credentials_set_domain(cred, lpcfg_workgroup(lp_ctx), CRED_UNINITIALISED);
     684        cli_credentials_set_workstation(cred, lpcfg_netbios_name(lp_ctx), CRED_UNINITIALISED);
     685        cli_credentials_set_realm(cred, lpcfg_realm(lp_ctx), CRED_UNINITIALISED);
    619686}
    620687
     
    629696{
    630697        char *p;
     698        const char *error_string;
    631699
    632700        if (lp_ctx != NULL) {
     
    660728       
    661729        if (cli_credentials_get_kerberos_state(cred) != CRED_DONT_USE_KERBEROS) {
    662                 cli_credentials_set_ccache(cred, event_context_find(cred), lp_ctx, NULL, CRED_GUESS_FILE);
     730                cli_credentials_set_ccache(cred, lp_ctx, NULL, CRED_GUESS_FILE,
     731                                           &error_string);
    663732        }
    664733}
     
    691760{
    692761        cred->secure_channel_type = secure_channel_type;
     762}
     763
     764/**
     765 * Return NETLOGON secure chanel type
     766 */
     767
     768_PUBLIC_ time_t cli_credentials_get_password_last_changed_time(struct cli_credentials *cred)
     769{
     770        return cred->password_last_changed_time;
     771}
     772
     773/**
     774 * Set NETLOGON secure channel type
     775 */
     776
     777_PUBLIC_ void cli_credentials_set_password_last_changed_time(struct cli_credentials *cred,
     778                                                             time_t last_changed_time)
     779{
     780        cred->password_last_changed_time = last_changed_time;
    693781}
    694782
     
    723811        const char *username;
    724812       
     813        /* if bind dn is set it's not anonymous */
     814        if (cred->bind_dn) {
     815                return false;
     816        }
     817
    725818        if (cred->machine_account_pending) {
    726819                cli_credentials_set_machine_account(cred,
     
    760853        return (cred->tries > 0);
    761854}
     855
     856_PUBLIC_ void cli_credentials_get_ntlm_username_domain(struct cli_credentials *cred, TALLOC_CTX *mem_ctx,
     857                                              const char **username,
     858                                              const char **domain)
     859{
     860        if (cred->principal_obtained > cred->username_obtained) {
     861                *domain = talloc_strdup(mem_ctx, "");
     862                *username = cli_credentials_get_principal(cred, mem_ctx);
     863        } else {
     864                *domain = cli_credentials_get_domain(cred);
     865                *username = cli_credentials_get_username(cred);
     866        }
     867}
     868
     869/**
     870 * Read a named file, and parse it for username, domain, realm and password
     871 *
     872 * @param credentials Credentials structure on which to set the password
     873 * @param file a named file to read the details from
     874 * @param obtained This enum describes how 'specified' this password is
     875 */
     876
     877_PUBLIC_ bool cli_credentials_parse_file(struct cli_credentials *cred, const char *file, enum credentials_obtained obtained)
     878{
     879        uint16_t len = 0;
     880        char *ptr, *val, *param;
     881        char **lines;
     882        int i, numlines;
     883
     884        lines = file_lines_load(file, &numlines, 0, NULL);
     885
     886        if (lines == NULL)
     887        {
     888                /* fail if we can't open the credentials file */
     889                d_printf("ERROR: Unable to open credentials file!\n");
     890                return false;
     891        }
     892
     893        for (i = 0; i < numlines; i++) {
     894                len = strlen(lines[i]);
     895
     896                if (len == 0)
     897                        continue;
     898
     899                /* break up the line into parameter & value.
     900                 * will need to eat a little whitespace possibly */
     901                param = lines[i];
     902                if (!(ptr = strchr_m (lines[i], '=')))
     903                        continue;
     904
     905                val = ptr+1;
     906                *ptr = '\0';
     907
     908                /* eat leading white space */
     909                while ((*val!='\0') && ((*val==' ') || (*val=='\t')))
     910                        val++;
     911
     912                if (strwicmp("password", param) == 0) {
     913                        cli_credentials_set_password(cred, val, obtained);
     914                } else if (strwicmp("username", param) == 0) {
     915                        cli_credentials_set_username(cred, val, obtained);
     916                } else if (strwicmp("domain", param) == 0) {
     917                        cli_credentials_set_domain(cred, val, obtained);
     918                } else if (strwicmp("realm", param) == 0) {
     919                        cli_credentials_set_realm(cred, val, obtained);
     920                }
     921                memset(lines[i], 0, len);
     922        }
     923
     924        talloc_free(lines);
     925
     926        return true;
     927}
     928
     929/**
     930 * Read a named file, and parse it for a password
     931 *
     932 * @param credentials Credentials structure on which to set the password
     933 * @param file a named file to read the password from
     934 * @param obtained This enum describes how 'specified' this password is
     935 */
     936
     937_PUBLIC_ bool cli_credentials_parse_password_file(struct cli_credentials *credentials, const char *file, enum credentials_obtained obtained)
     938{
     939        int fd = open(file, O_RDONLY, 0);
     940        bool ret;
     941
     942        if (fd < 0) {
     943                fprintf(stderr, "Error opening password file %s: %s\n",
     944                                file, strerror(errno));
     945                return false;
     946        }
     947
     948        ret = cli_credentials_parse_password_fd(credentials, fd, obtained);
     949
     950        close(fd);
     951       
     952        return ret;
     953}
     954
     955
     956/**
     957 * Read a file descriptor, and parse it for a password (eg from a file or stdin)
     958 *
     959 * @param credentials Credentials structure on which to set the password
     960 * @param fd open file descriptor to read the password from
     961 * @param obtained This enum describes how 'specified' this password is
     962 */
     963
     964_PUBLIC_ bool cli_credentials_parse_password_fd(struct cli_credentials *credentials,
     965                                       int fd, enum credentials_obtained obtained)
     966{
     967        char *p;
     968        char pass[128];
     969
     970        for(p = pass, *p = '\0'; /* ensure that pass is null-terminated */
     971                p && p - pass < sizeof(pass);) {
     972                switch (read(fd, p, 1)) {
     973                case 1:
     974                        if (*p != '\n' && *p != '\0') {
     975                                *++p = '\0'; /* advance p, and null-terminate pass */
     976                                break;
     977                        }
     978                        /* fall through */
     979                case 0:
     980                        if (p - pass) {
     981                                *p = '\0'; /* null-terminate it, just in case... */
     982                                p = NULL; /* then force the loop condition to become false */
     983                                break;
     984                        } else {
     985                                fprintf(stderr, "Error reading password from file descriptor %d: %s\n", fd, "empty password\n");
     986                                return false;
     987                        }
     988
     989                default:
     990                        fprintf(stderr, "Error reading password from file descriptor %d: %s\n",
     991                                        fd, strerror(errno));
     992                        return false;
     993                }
     994        }
     995
     996        cli_credentials_set_password(credentials, pass, obtained);
     997        return true;
     998}
     999
     1000
Note: See TracChangeset for help on using the changeset viewer.