lib: Move "iov_buf.[ch]" to lib/util
[obnox/samba/samba-obnox.git] / source3 / lib / lsa.c
1 /*
2  * Helper functions related to the LSA server
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /***************************************************************************
19  init_lsa_ref_domain_list - adds a domain if it's not already in, returns index.
20 ***************************************************************************/
21
22 #include "includes.h"
23 #include "libcli/security/dom_sid.h"
24 #include "librpc/gen_ndr/lsa.h"
25 #include "lsa.h"
26
27 int init_lsa_ref_domain_list(TALLOC_CTX *mem_ctx,
28                              struct lsa_RefDomainList *ref,
29                              const char *dom_name,
30                              struct dom_sid *dom_sid)
31 {
32         int num = 0;
33
34         if (dom_name != NULL) {
35                 for (num = 0; num < ref->count; num++) {
36                         if (dom_sid_equal(dom_sid, ref->domains[num].sid)) {
37                                 return num;
38                         }
39                 }
40         } else {
41                 num = ref->count;
42         }
43
44         if (num >= LSA_REF_DOMAIN_LIST_MULTIPLIER) {
45                 /* index not found, already at maximum domain limit */
46                 return -1;
47         }
48
49         ref->count = num + 1;
50         ref->max_size = LSA_REF_DOMAIN_LIST_MULTIPLIER;
51
52         ref->domains = talloc_realloc(mem_ctx, ref->domains,
53                                             struct lsa_DomainInfo, ref->count);
54         if (!ref->domains) {
55                 return -1;
56         }
57
58         ZERO_STRUCT(ref->domains[num]);
59
60         ref->domains[num].name.string = dom_name;
61         ref->domains[num].sid = dom_sid_dup(mem_ctx, dom_sid);
62         if (!ref->domains[num].sid) {
63                 return -1;
64         }
65
66         return num;
67 }