1/*
2   Samba CIFS implementation
3   ADS convenience functions for GPO
4
5   Copyright (C) 2008 Jelmer Vernooij, jelmer@samba.org
6   Copyright (C) 2008 Wilco Baan Hofman, wilco@baanhofman.nl
7
8   This program is free software; you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 3 of the License, or
11   (at your option) any later version.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with this program.  If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#ifndef __ADS_CONVENIENCE_H__
23#define __ADS_CONVENIENCE_H__
24
25#include "librpc/gen_ndr/security.h"
26
27#define ADS_ERR_OK(status) ((status.error_type == ENUM_ADS_ERROR_NT) ? NT_STATUS_IS_OK(status.err.nt_status):(status.err.rc == 0))
28#define ADS_ERROR(rc) ads_build_ldap_error(rc)
29#define ADS_ERROR_NT(rc) ads_build_nt_error(rc)
30#define ADS_SUCCESS ADS_ERROR(0)
31
32#define ADS_ERROR_HAVE_NO_MEMORY(x) do { \
33        if (!(x)) {\
34                return ADS_ERROR_NT(NT_STATUS_NO_MEMORY);\
35        }\
36} while (0)
37
38#define LDAP_SCOPE_BASE		LDB_SCOPE_BASE
39#define LDAP_SCOPE_SUBTREE	LDB_SCOPE_SUBTREE
40#define LDAP_SCOPE_ONELEVEL	LDB_SCOPE_ONELEVEL
41
42
43
44
45typedef struct {
46	struct libnet_context *netctx;
47	struct ldb_context *ldbctx;
48	char *ldap_server_name;
49
50	/* State information for the smb connection */
51	struct cli_credentials *credentials;
52	struct smbcli_state *cli;
53} ADS_STRUCT;
54
55
56typedef struct security_token NT_USER_TOKEN;
57
58typedef struct ldb_result LDAPMessage;
59typedef void ** ADS_MODLIST;
60
61/* there are 3 possible types of errors the ads subsystem can produce */
62enum ads_error_type { ENUM_ADS_ERROR_LDAP, ENUM_ADS_ERROR_SYSTEM, ENUM_ADS_ERROR_NT};
63
64typedef struct {
65	enum ads_error_type error_type;
66	union err_state{
67		int rc;
68		NTSTATUS nt_status;
69	} err;
70	int minor_status;
71} ADS_STATUS;
72
73
74/* Prototypes from ads_convenience.c */
75ADS_STATUS ads_build_nt_error(NTSTATUS);
76ADS_STATUS ads_build_ldap_error(int);
77
78ADS_STATUS ads_startup (struct libnet_context *netctx, ADS_STRUCT **ads);
79const char *ads_errstr(ADS_STATUS status);
80const char * ads_get_dn(ADS_STRUCT *ads, LDAPMessage *res);
81bool ads_pull_sd(ADS_STRUCT *ads, TALLOC_CTX *ctx, LDAPMessage *res, const char *field, struct security_descriptor **sd);
82const char * ads_pull_string(ADS_STRUCT *ads, TALLOC_CTX *ctx, LDAPMessage *res, const char *field);
83bool ads_pull_uint32(ADS_STRUCT *ads, LDAPMessage *res, const char *field, uint32_t *ret);
84int ads_count_replies(ADS_STRUCT *ads, LDAPMessage *res);
85ADS_STATUS ads_do_search_all_sd_flags (ADS_STRUCT *ads, const char *dn, int scope,
86                                              const char *filter, const char **attrs,
87                                              uint32_t sd_flags, LDAPMessage **res);
88ADS_STATUS ads_search_dn(ADS_STRUCT *ads, LDAPMessage **res,
89                         const char *dn, const char **attrs);
90ADS_STATUS ads_search_retry_dn_sd_flags(ADS_STRUCT *ads, LDAPMessage **res, uint32_t sd_flags,
91                                        const char *dn, const char **attrs);
92ADS_STATUS ads_msgfree(ADS_STRUCT *ads, LDAPMessage *res);
93NTSTATUS ads_ntstatus(ADS_STATUS status);
94ADS_STATUS ads_build_ldap_error(int ldb_error);
95ADS_STATUS ads_build_nt_error(NTSTATUS nt_status);
96bool nt_token_check_sid( const struct dom_sid *sid, const NT_USER_TOKEN *token);
97ADS_MODLIST ads_init_mods(TALLOC_CTX *ctx);
98ADS_STATUS ads_mod_str(TALLOC_CTX *ctx, ADS_MODLIST *mods, const char *name, const char *val);
99ADS_STATUS ads_gen_mod(ADS_STRUCT *ads, const char *mod_dn, ADS_MODLIST mods);
100const char *ads_get_ldap_server_name(ADS_STRUCT *ads);
101
102
103#endif
104