r23581: Move regkey_open_onelevel from reg_frontend to reg_api,
[ira/wip.git] / source3 / registry / reg_api.c
index e163759dabcf6e371cde00abe2c25a266e36bf03..4fbf54e75376bc65df97c1a219645ca2f7aaddd1 100644 (file)
@@ -57,6 +57,116 @@ static WERROR fill_subkey_cache(struct registry_key *key)
        return WERR_OK;
 }
 
+static int regkey_destructor(REGISTRY_KEY *key)
+{
+       return regdb_close();
+}
+
+static WERROR regkey_open_onelevel(TALLOC_CTX *mem_ctx, 
+                                  struct registry_key *parent,
+                                  const char *name,
+                                  const struct nt_user_token *token,
+                                  uint32 access_desired,
+                                  struct registry_key **pregkey)
+{
+       WERROR          result = WERR_OK;
+       struct registry_key *regkey;
+       REGISTRY_KEY *key;
+       REGSUBKEY_CTR   *subkeys = NULL;
+
+       DEBUG(7,("regkey_open_onelevel: name = [%s]\n", name));
+
+       SMB_ASSERT(strchr(name, '\\') == NULL);
+
+       if (!(regkey = TALLOC_ZERO_P(mem_ctx, struct registry_key)) ||
+           !(regkey->token = dup_nt_token(regkey, token)) ||
+           !(regkey->key = TALLOC_ZERO_P(regkey, REGISTRY_KEY))) {
+               result = WERR_NOMEM;
+               goto done;
+       }
+
+       if ( !(W_ERROR_IS_OK(result = regdb_open())) ) {
+               goto done;
+       }
+
+       key = regkey->key;
+       talloc_set_destructor(key, regkey_destructor);
+               
+       /* initialization */
+       
+       key->type = REG_KEY_GENERIC;
+
+       if (name[0] == '\0') {
+               /*
+                * Open a copy of the parent key
+                */
+               if (!parent) {
+                       result = WERR_BADFILE;
+                       goto done;
+               }
+               key->name = talloc_strdup(key, parent->key->name);
+       }
+       else {
+               /*
+                * Normal subkey open
+                */
+               key->name = talloc_asprintf(key, "%s%s%s",
+                                           parent ? parent->key->name : "",
+                                           parent ? "\\": "",
+                                           name);
+       }
+
+       if (key->name == NULL) {
+               result = WERR_NOMEM;
+               goto done;
+       }
+
+       /* Tag this as a Performance Counter Key */
+
+       if( StrnCaseCmp(key->name, KEY_HKPD, strlen(KEY_HKPD)) == 0 )
+               key->type = REG_KEY_HKPD;
+       
+       /* Look up the table of registry I/O operations */
+
+       if ( !(key->hook = reghook_cache_find( key->name )) ) {
+               DEBUG(0,("reg_open_onelevel: Failed to assigned a "
+                        "REGISTRY_HOOK to [%s]\n", key->name ));
+               result = WERR_BADFILE;
+               goto done;
+       }
+       
+       /* check if the path really exists; failed is indicated by -1 */
+       /* if the subkey count failed, bail out */
+
+       if ( !(subkeys = TALLOC_ZERO_P( key, REGSUBKEY_CTR )) ) {
+               result = WERR_NOMEM;
+               goto done;
+       }
+
+       if ( fetch_reg_keys( key, subkeys ) == -1 )  {
+               result = WERR_BADFILE;
+               goto done;
+       }
+       
+       TALLOC_FREE( subkeys );
+
+       if ( !regkey_access_check( key, access_desired, &key->access_granted,
+                                  token ) ) {
+               result = WERR_ACCESS_DENIED;
+               goto done;
+       }
+
+       *pregkey = regkey;
+       result = WERR_OK;
+       
+done:
+       if ( !W_ERROR_IS_OK(result) ) {
+               TALLOC_FREE(regkey);
+       }
+
+       return result;
+}
+
 WERROR reg_openhive(TALLOC_CTX *mem_ctx, const char *hive,
                    uint32 desired_access,
                    const struct nt_user_token *token,
@@ -567,5 +677,3 @@ WERROR reg_open_path(TALLOC_CTX *mem_ctx, const char *orig_path,
        *pkey = key;
        return WERR_OK;
 }
-
-/* END */