| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | ads (active directory) utility library
|
|---|
| 4 | Copyright (C) Guenther Deschner 2005-2006
|
|---|
| 5 | Copyright (C) Gerald (Jerry) Carter 2006
|
|---|
| 6 |
|
|---|
| 7 | This program is free software; you can redistribute it and/or modify
|
|---|
| 8 | it under the terms of the GNU General Public License as published by
|
|---|
| 9 | the Free Software Foundation; either version 2 of the License, or
|
|---|
| 10 | (at your option) any later version.
|
|---|
| 11 |
|
|---|
| 12 | This program is distributed in the hope that it will be useful,
|
|---|
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 15 | GNU General Public License for more details.
|
|---|
| 16 |
|
|---|
| 17 | You should have received a copy of the GNU General Public License
|
|---|
| 18 | along with this program; if not, write to the Free Software
|
|---|
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include "includes.h"
|
|---|
| 23 |
|
|---|
| 24 | #ifdef HAVE_LDAP
|
|---|
| 25 |
|
|---|
| 26 | ADS_STATUS ads_get_attrnames_by_oids(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx,
|
|---|
| 27 | const char *schema_path,
|
|---|
| 28 | const char **OIDs, size_t num_OIDs,
|
|---|
| 29 | char ***OIDs_out, char ***names, size_t *count)
|
|---|
| 30 | {
|
|---|
| 31 | ADS_STATUS status;
|
|---|
| 32 | LDAPMessage *res = NULL;
|
|---|
| 33 | LDAPMessage *msg;
|
|---|
| 34 | char *expr = NULL;
|
|---|
| 35 | const char *attrs[] = { "lDAPDisplayName", "attributeId", NULL };
|
|---|
| 36 | int i = 0, p = 0;
|
|---|
| 37 |
|
|---|
| 38 | if (!ads || !mem_ctx || !names || !count || !OIDs || !OIDs_out) {
|
|---|
| 39 | return ADS_ERROR(LDAP_PARAM_ERROR);
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | if (num_OIDs == 0 || OIDs[0] == NULL) {
|
|---|
| 43 | return ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | if ((expr = talloc_asprintf(mem_ctx, "(|")) == NULL) {
|
|---|
| 47 | return ADS_ERROR(LDAP_NO_MEMORY);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | for (i=0; i<num_OIDs; i++) {
|
|---|
| 51 |
|
|---|
| 52 | if ((expr = talloc_asprintf_append(expr, "(attributeId=%s)",
|
|---|
| 53 | OIDs[i])) == NULL) {
|
|---|
| 54 | return ADS_ERROR(LDAP_NO_MEMORY);
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | if ((expr = talloc_asprintf_append(expr, ")")) == NULL) {
|
|---|
| 59 | return ADS_ERROR(LDAP_NO_MEMORY);
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | status = ads_do_search_retry(ads, schema_path,
|
|---|
| 63 | LDAP_SCOPE_SUBTREE, expr, attrs, &res);
|
|---|
| 64 | if (!ADS_ERR_OK(status)) {
|
|---|
| 65 | return status;
|
|---|
| 66 | }
|
|---|
| 67 |
|
|---|
| 68 | *count = ads_count_replies(ads, res);
|
|---|
| 69 | if (*count == 0 || !res) {
|
|---|
| 70 | status = ADS_ERROR_NT(NT_STATUS_NONE_MAPPED);
|
|---|
| 71 | goto out;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | if (((*names) = TALLOC_ARRAY(mem_ctx, char *, *count)) == NULL) {
|
|---|
| 75 | status = ADS_ERROR(LDAP_NO_MEMORY);
|
|---|
| 76 | goto out;
|
|---|
| 77 | }
|
|---|
| 78 | if (((*OIDs_out) = TALLOC_ARRAY(mem_ctx, char *, *count)) == NULL) {
|
|---|
| 79 | status = ADS_ERROR(LDAP_NO_MEMORY);
|
|---|
| 80 | goto out;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | for (msg = ads_first_entry(ads, res); msg != NULL;
|
|---|
| 84 | msg = ads_next_entry(ads, msg)) {
|
|---|
| 85 |
|
|---|
| 86 | (*names)[p] = ads_pull_string(ads, mem_ctx, msg,
|
|---|
| 87 | "lDAPDisplayName");
|
|---|
| 88 | (*OIDs_out)[p] = ads_pull_string(ads, mem_ctx, msg,
|
|---|
| 89 | "attributeId");
|
|---|
| 90 | if (((*names)[p] == NULL) || ((*OIDs_out)[p] == NULL)) {
|
|---|
| 91 | status = ADS_ERROR(LDAP_NO_MEMORY);
|
|---|
| 92 | goto out;
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 | p++;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | if (*count < num_OIDs) {
|
|---|
| 99 | status = ADS_ERROR_NT(STATUS_SOME_UNMAPPED);
|
|---|
| 100 | goto out;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | status = ADS_ERROR(LDAP_SUCCESS);
|
|---|
| 104 | out:
|
|---|
| 105 | ads_msgfree(ads, res);
|
|---|
| 106 |
|
|---|
| 107 | return status;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | const char *ads_get_attrname_by_oid(ADS_STRUCT *ads, const char *schema_path, TALLOC_CTX *mem_ctx, const char * OID)
|
|---|
| 111 | {
|
|---|
| 112 | ADS_STATUS rc;
|
|---|
| 113 | int count = 0;
|
|---|
| 114 | LDAPMessage *res = NULL;
|
|---|
| 115 | char *expr = NULL;
|
|---|
| 116 | const char *attrs[] = { "lDAPDisplayName", NULL };
|
|---|
| 117 | char *result;
|
|---|
| 118 |
|
|---|
| 119 | if (ads == NULL || mem_ctx == NULL || OID == NULL) {
|
|---|
| 120 | goto failed;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | expr = talloc_asprintf(mem_ctx, "(attributeId=%s)", OID);
|
|---|
| 124 | if (expr == NULL) {
|
|---|
| 125 | goto failed;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | rc = ads_do_search_retry(ads, schema_path, LDAP_SCOPE_SUBTREE,
|
|---|
| 129 | expr, attrs, &res);
|
|---|
| 130 | if (!ADS_ERR_OK(rc)) {
|
|---|
| 131 | goto failed;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | count = ads_count_replies(ads, res);
|
|---|
| 135 | if (count == 0 || !res) {
|
|---|
| 136 | goto failed;
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | result = ads_pull_string(ads, mem_ctx, res, "lDAPDisplayName");
|
|---|
| 140 | ads_msgfree(ads, res);
|
|---|
| 141 |
|
|---|
| 142 | return result;
|
|---|
| 143 |
|
|---|
| 144 | failed:
|
|---|
| 145 | DEBUG(0,("ads_get_attrname_by_oid: failed to retrieve name for oid: %s\n",
|
|---|
| 146 | OID));
|
|---|
| 147 |
|
|---|
| 148 | ads_msgfree(ads, res);
|
|---|
| 149 | return NULL;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | /*********************************************************************
|
|---|
| 153 | *********************************************************************/
|
|---|
| 154 |
|
|---|
| 155 | static ADS_STATUS ads_schema_path(ADS_STRUCT *ads, TALLOC_CTX *mem_ctx, char **schema_path)
|
|---|
| 156 | {
|
|---|
| 157 | ADS_STATUS status;
|
|---|
| 158 | LDAPMessage *res;
|
|---|
| 159 | const char *schema;
|
|---|
| 160 | const char *attrs[] = { "schemaNamingContext", NULL };
|
|---|
| 161 |
|
|---|
| 162 | status = ads_do_search(ads, "", LDAP_SCOPE_BASE, "(objectclass=*)", attrs, &res);
|
|---|
| 163 | if (!ADS_ERR_OK(status)) {
|
|---|
| 164 | return status;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | if ( (schema = ads_pull_string(ads, mem_ctx, res, "schemaNamingContext")) == NULL ) {
|
|---|
| 168 | ads_msgfree(ads, res);
|
|---|
| 169 | return ADS_ERROR(LDAP_NO_RESULTS_RETURNED);
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | if ( (*schema_path = talloc_strdup(mem_ctx, schema)) == NULL ) {
|
|---|
| 173 | ads_msgfree(ads, res);
|
|---|
| 174 | return ADS_ERROR(LDAP_NO_MEMORY);
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | ads_msgfree(ads, res);
|
|---|
| 178 |
|
|---|
| 179 | return status;
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | /**
|
|---|
| 183 | * Check for "Services for Unix" or rfc2307 Schema and load some attributes into the ADS_STRUCT
|
|---|
| 184 | * @param ads connection to ads server
|
|---|
| 185 | * @param enum mapping type
|
|---|
| 186 | * @return ADS_STATUS status of search (False if one or more attributes couldn't be
|
|---|
| 187 | * found in Active Directory)
|
|---|
| 188 | **/
|
|---|
| 189 | ADS_STATUS ads_check_posix_schema_mapping(TALLOC_CTX *mem_ctx,
|
|---|
| 190 | ADS_STRUCT *ads,
|
|---|
| 191 | enum wb_posix_mapping map_type,
|
|---|
| 192 | struct posix_schema **s )
|
|---|
| 193 | {
|
|---|
| 194 | TALLOC_CTX *ctx = NULL;
|
|---|
| 195 | ADS_STATUS status;
|
|---|
| 196 | char **oids_out, **names_out;
|
|---|
| 197 | size_t num_names;
|
|---|
| 198 | char *schema_path = NULL;
|
|---|
| 199 | int i;
|
|---|
| 200 | struct posix_schema *schema = NULL;
|
|---|
| 201 |
|
|---|
| 202 | const char *oids_sfu[] = { ADS_ATTR_SFU_UIDNUMBER_OID,
|
|---|
| 203 | ADS_ATTR_SFU_GIDNUMBER_OID,
|
|---|
| 204 | ADS_ATTR_SFU_HOMEDIR_OID,
|
|---|
| 205 | ADS_ATTR_SFU_SHELL_OID,
|
|---|
| 206 | ADS_ATTR_SFU_GECOS_OID};
|
|---|
| 207 |
|
|---|
| 208 | const char *oids_rfc2307[] = { ADS_ATTR_RFC2307_UIDNUMBER_OID,
|
|---|
| 209 | ADS_ATTR_RFC2307_GIDNUMBER_OID,
|
|---|
| 210 | ADS_ATTR_RFC2307_HOMEDIR_OID,
|
|---|
| 211 | ADS_ATTR_RFC2307_SHELL_OID,
|
|---|
| 212 | ADS_ATTR_RFC2307_GECOS_OID };
|
|---|
| 213 |
|
|---|
| 214 | DEBUG(10,("ads_check_posix_schema_mapping\n"));
|
|---|
| 215 |
|
|---|
| 216 | if ( (ctx = talloc_init("ads_check_posix_schema_mapping")) == NULL ) {
|
|---|
| 217 | return ADS_ERROR(LDAP_NO_MEMORY);
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | if ( (schema = TALLOC_P(mem_ctx, struct posix_schema)) == NULL ) {
|
|---|
| 221 | TALLOC_FREE( ctx );
|
|---|
| 222 | return ADS_ERROR(LDAP_NO_MEMORY);
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | status = ads_schema_path(ads, ctx, &schema_path);
|
|---|
| 226 | if (!ADS_ERR_OK(status)) {
|
|---|
| 227 | DEBUG(3,("ads_check_posix_mapping: Unable to retrieve schema DN!\n"));
|
|---|
| 228 | goto done;
|
|---|
| 229 | }
|
|---|
| 230 |
|
|---|
| 231 | if (map_type == WB_POSIX_MAP_SFU) {
|
|---|
| 232 | status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_sfu,
|
|---|
| 233 | ARRAY_SIZE(oids_sfu),
|
|---|
| 234 | &oids_out, &names_out, &num_names);
|
|---|
| 235 | } else {
|
|---|
| 236 | status = ads_get_attrnames_by_oids(ads, ctx, schema_path, oids_rfc2307,
|
|---|
| 237 | ARRAY_SIZE(oids_rfc2307),
|
|---|
| 238 | &oids_out, &names_out, &num_names);
|
|---|
| 239 | }
|
|---|
| 240 |
|
|---|
| 241 | if (!ADS_ERR_OK(status)) {
|
|---|
| 242 | DEBUG(3,("ads_check_posix_schema_mapping: failed %s\n",
|
|---|
| 243 | ads_errstr(status)));
|
|---|
| 244 | goto done;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | for (i=0; i<num_names; i++) {
|
|---|
| 248 |
|
|---|
| 249 | DEBUGADD(10,("\tOID %s has name: %s\n", oids_out[i], names_out[i]));
|
|---|
| 250 |
|
|---|
| 251 | if (strequal(ADS_ATTR_RFC2307_UIDNUMBER_OID, oids_out[i]) ||
|
|---|
| 252 | strequal(ADS_ATTR_SFU_UIDNUMBER_OID, oids_out[i])) {
|
|---|
| 253 | schema->posix_uidnumber_attr = talloc_strdup(schema, names_out[i]);
|
|---|
| 254 | continue;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | if (strequal(ADS_ATTR_RFC2307_GIDNUMBER_OID, oids_out[i]) ||
|
|---|
| 258 | strequal(ADS_ATTR_SFU_GIDNUMBER_OID, oids_out[i])) {
|
|---|
| 259 | schema->posix_gidnumber_attr = talloc_strdup(schema, names_out[i]);
|
|---|
| 260 | continue;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | if (strequal(ADS_ATTR_RFC2307_HOMEDIR_OID, oids_out[i]) ||
|
|---|
| 264 | strequal(ADS_ATTR_SFU_HOMEDIR_OID, oids_out[i])) {
|
|---|
| 265 | schema->posix_homedir_attr = talloc_strdup(schema, names_out[i]);
|
|---|
| 266 | continue;
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | if (strequal(ADS_ATTR_RFC2307_SHELL_OID, oids_out[i]) ||
|
|---|
| 270 | strequal(ADS_ATTR_SFU_SHELL_OID, oids_out[i])) {
|
|---|
| 271 | schema->posix_shell_attr = talloc_strdup(schema, names_out[i]);
|
|---|
| 272 | continue;
|
|---|
| 273 | }
|
|---|
| 274 |
|
|---|
| 275 | if (strequal(ADS_ATTR_RFC2307_GECOS_OID, oids_out[i]) ||
|
|---|
| 276 | strequal(ADS_ATTR_SFU_GECOS_OID, oids_out[i])) {
|
|---|
| 277 | schema->posix_gecos_attr = talloc_strdup(schema, names_out[i]);
|
|---|
| 278 | }
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | if (!schema->posix_uidnumber_attr ||
|
|---|
| 282 | !schema->posix_gidnumber_attr ||
|
|---|
| 283 | !schema->posix_homedir_attr ||
|
|---|
| 284 | !schema->posix_shell_attr ||
|
|---|
| 285 | !schema->posix_gecos_attr) {
|
|---|
| 286 | status = ADS_ERROR(LDAP_NO_MEMORY);
|
|---|
| 287 | TALLOC_FREE( schema );
|
|---|
| 288 | goto done;
|
|---|
| 289 | }
|
|---|
| 290 |
|
|---|
| 291 | *s = schema;
|
|---|
| 292 |
|
|---|
| 293 | status = ADS_ERROR(LDAP_SUCCESS);
|
|---|
| 294 |
|
|---|
| 295 | done:
|
|---|
| 296 | if (ctx) {
|
|---|
| 297 | talloc_destroy(ctx);
|
|---|
| 298 | }
|
|---|
| 299 |
|
|---|
| 300 | return status;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | #endif
|
|---|