Move the master copy of ldb (from Samba4) to the top level
[abartlet/samba.git/.git] / source3 / lib / ldb / common / ldb_controls.c
1 /* 
2    ldb database library
3
4    Copyright (C) Simo Sorce  2005
5
6      ** NOTE! The following LGPL license applies to the ldb
7      ** library. This does NOT imply that all of Samba is released
8      ** under the LGPL
9    
10    This library is free software; you can redistribute it and/or
11    modify it under the terms of the GNU Lesser General Public
12    License as published by the Free Software Foundation; either
13    version 3 of the License, or (at your option) any later version.
14
15    This library is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    Lesser General Public License for more details.
19
20    You should have received a copy of the GNU Lesser General Public
21    License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 */
23
24 /*
25  *  Name: ldb_controls.c
26  *
27  *  Component: ldb controls utility functions
28  *
29  *  Description: helper functions for control modules
30  *
31  *  Author: Simo Sorce
32  */
33
34 #include "includes.h"
35 #include "ldb/include/includes.h"
36
37 /* check if a control with the specified "oid" exist and return it */
38 /* returns NULL if not found */
39 struct ldb_control *get_control_from_list(struct ldb_control **controls, const char *oid)
40 {
41         int i;
42
43         /* check if there's a paged request control */
44         if (controls != NULL) {
45                 for (i = 0; controls[i]; i++) {
46                         if (strcmp(oid, controls[i]->oid) == 0) {
47                                 break;
48                         }
49                 }
50
51                 return controls[i];
52         }
53
54         return NULL;
55 }
56
57 /* saves the current controls list into the "saver" and replace the one in req with a new one excluding
58 the "exclude" control */
59 /* returns False on error */
60 int save_controls(struct ldb_control *exclude, struct ldb_request *req, struct ldb_control ***saver)
61 {
62         struct ldb_control **lcs;
63         int i, j;
64
65         *saver = req->controls;
66         for (i = 0; req->controls[i]; i++);
67         if (i == 1) {
68                 req->controls = NULL;
69                 return 1;
70         }
71
72         lcs = talloc_array(req, struct ldb_control *, i);
73         if (!lcs) {
74                 return 0;
75         }
76
77         for (i = 0, j = 0; (*saver)[i]; i++) {
78                 if (exclude == (*saver)[i]) continue;
79                 lcs[j] = (*saver)[i];
80                 j++;
81         }
82         lcs[j] = NULL;
83
84         req->controls = lcs;
85         return 1;
86 }
87
88 /* check if there's any control marked as critical in the list */
89 /* return True if any, False if none */
90 int check_critical_controls(struct ldb_control **controls)
91 {
92         int i;
93
94         if (controls == NULL) {
95                 return 0;
96         }
97
98         for (i = 0; controls[i]; i++) {
99                 if (controls[i]->critical) {
100                         return 1;
101                 }
102         }
103
104         return 0;
105 }