eb3071404979e3b3d1be6ac160f07a3bb71b41f4
[bbaumbach/samba-autobuild/.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, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23 */
24
25 /*
26  *  Name: ldb_controls.c
27  *
28  *  Component: ldb controls utility functions
29  *
30  *  Description: helper functions for control modules
31  *
32  *  Author: Simo Sorce
33  */
34
35 #include "includes.h"
36 #include "ldb/include/includes.h"
37
38 /* check if a control with the specified "oid" exist and return it */
39 /* returns NULL if not found */
40 struct ldb_control *get_control_from_list(struct ldb_control **controls, const char *oid)
41 {
42         int i;
43
44         /* check if there's a paged request control */
45         if (controls != NULL) {
46                 for (i = 0; controls[i]; i++) {
47                         if (strcmp(oid, controls[i]->oid) == 0) {
48                                 break;
49                         }
50                 }
51
52                 return controls[i];
53         }
54
55         return NULL;
56 }
57
58 /* saves the current controls list into the "saver" and replace the one in req with a new one excluding
59 the "exclude" control */
60 /* returns False on error */
61 int save_controls(struct ldb_control *exclude, struct ldb_request *req, struct ldb_control ***saver)
62 {
63         struct ldb_control **lcs;
64         int i, j;
65
66         *saver = req->controls;
67         for (i = 0; req->controls[i]; i++);
68         if (i == 1) {
69                 req->controls = NULL;
70                 return 1;
71         }
72
73         lcs = talloc_array(req, struct ldb_control *, i);
74         if (!lcs) {
75                 return 0;
76         }
77
78         for (i = 0, j = 0; (*saver)[i]; i++) {
79                 if (exclude == (*saver)[i]) continue;
80                 lcs[j] = (*saver)[i];
81                 j++;
82         }
83         lcs[j] = NULL;
84
85         req->controls = lcs;
86         return 1;
87 }
88
89 /* check if there's any control marked as critical in the list */
90 /* return True if any, False if none */
91 int check_critical_controls(struct ldb_control **controls)
92 {
93         int i;
94
95         if (controls == NULL) {
96                 return 0;
97         }
98
99         for (i = 0; controls[i]; i++) {
100                 if (controls[i]->critical) {
101                         return 1;
102                 }
103         }
104
105         return 0;
106 }