Changeset 745 for trunk/server/source4/auth/credentials/credentials.c
- Timestamp:
- Nov 27, 2012, 4:43:17 PM (13 years ago)
- Location:
- trunk/server
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/server
- Property svn:mergeinfo changed
/vendor/current merged: 581,587,591,594,597,600,615,618,740
- Property svn:mergeinfo changed
-
trunk/server/source4/auth/credentials/credentials.c
r414 r745 25 25 #include "librpc/gen_ndr/samr.h" /* for struct samrPassword */ 26 26 #include "auth/credentials/credentials.h" 27 #include "auth/credentials/credentials_krb5.h"28 #include "auth/credentials/credentials_proto.h"29 27 #include "libcli/auth/libcli_auth.h" 30 28 #include "lib/events/events.h" 31 29 #include "param/param.h" 30 #include "system/filesys.h" 32 31 33 32 /** … … 38 37 { 39 38 struct cli_credentials *cred = talloc(mem_ctx, struct cli_credentials); 40 if ( !cred) {39 if (cred == NULL) { 41 40 return cred; 42 41 } 43 42 44 cred->netlogon_creds = NULL;45 cred->machine_account_pending = false;46 43 cred->workstation_obtained = CRED_UNINITIALISED; 47 44 cred->username_obtained = CRED_UNINITIALISED; … … 51 48 cred->ccache_obtained = CRED_UNINITIALISED; 52 49 cred->client_gss_creds_obtained = CRED_UNINITIALISED; 50 cred->principal_obtained = CRED_UNINITIALISED; 51 cred->keytab_obtained = CRED_UNINITIALISED; 53 52 cred->server_gss_creds_obtained = CRED_UNINITIALISED; 54 cred->keytab_obtained = CRED_UNINITIALISED;55 cred->principal_obtained = CRED_UNINITIALISED;56 53 57 54 cred->ccache_threshold = CRED_UNINITIALISED; 58 55 cred->client_gss_creds_threshold = CRED_UNINITIALISED; 59 56 57 cred->workstation = NULL; 58 cred->username = NULL; 59 cred->password = NULL; 60 60 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 61 98 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 63 103 cred->machine_account = false; 64 104 65 cred->bind_dn = NULL;66 67 105 cred->tries = 3; 106 68 107 cred->callback_running = false; 69 108 70 109 cli_credentials_set_kerberos_state(cred, CRED_AUTO_USE_KERBEROS); 71 110 cli_credentials_set_gensec_features(cred, 0); 111 cli_credentials_set_krb_forwardable(cred, CRED_AUTO_KRB_FORWARDABLE); 72 112 73 113 return cred; … … 94 134 } 95 135 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 96 142 _PUBLIC_ enum credentials_use_kerberos cli_credentials_get_kerberos_state(struct cli_credentials *creds) 97 143 { 98 144 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; 99 150 } 100 151 … … 185 236 * @note Return value will never be NULL except by programmer error. 186 237 */ 187 _PUBLIC_ const char *cli_credentials_get_principal(struct cli_credentials *cred, TALLOC_CTX *mem_ctx)238 const char *cli_credentials_get_principal_and_obtained(struct cli_credentials *cred, TALLOC_CTX *mem_ctx, enum credentials_obtained *obtained) 188 239 { 189 240 if (cred->machine_account_pending) { … … 201 252 } 202 253 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)) { 204 256 if (cred->domain_obtained > cred->realm_obtained) { 257 *obtained = MIN(cred->domain_obtained, cred->username_obtained); 205 258 return talloc_asprintf(mem_ctx, "%s@%s", 206 259 cli_credentials_get_username(cred), 207 260 cli_credentials_get_domain(cred)); 208 261 } else { 262 *obtained = MIN(cred->domain_obtained, cred->username_obtained); 209 263 return talloc_asprintf(mem_ctx, "%s@%s", 210 264 cli_credentials_get_username(cred), … … 212 266 } 213 267 } 268 *obtained = cred->principal_obtained; 214 269 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); 215 282 } 216 283 … … 614 681 { 615 682 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); 619 686 } 620 687 … … 629 696 { 630 697 char *p; 698 const char *error_string; 631 699 632 700 if (lp_ctx != NULL) { … … 660 728 661 729 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); 663 732 } 664 733 } … … 691 760 { 692 761 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; 693 781 } 694 782 … … 723 811 const char *username; 724 812 813 /* if bind dn is set it's not anonymous */ 814 if (cred->bind_dn) { 815 return false; 816 } 817 725 818 if (cred->machine_account_pending) { 726 819 cli_credentials_set_machine_account(cred, … … 760 853 return (cred->tries > 0); 761 854 } 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.