File: lulu.c

package info (click to toggle)
ldapjdk 4.21.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 9,768 kB
  • sloc: ansic: 44,727; java: 39,519; xml: 7,420; sh: 4,237; perl: 3,774; makefile: 1,681; cpp: 979
file content (267 lines) | stat: -rw-r--r-- 7,199 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*  -*- Mode: C; eval: (c-set-style "GNU") -*-
 ******************************************************************************
 * $Id: lulu.c,v 1.4 2004/04/25 21:07:10 gerv%gerv.net Exp $
 *
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Netscape DS Plugins. 
 *
 * The Initial Developer of the Original Code is Leif Hedstrom and Netscape  
 * Communications Corp. 
 * Portions created by the Initial Developer are Copyright (C) 2001
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
 
/*****************************************************************************
 * DESCRIPTION:
 *   Lots of Useful Little Utilities
 *
 *****************************************************************************/
 
#include <stdio.h>
#include "lulu.h"


/******************************************************************************
 *  Free the list of attributes, in case something went south...
 */
PUBLIC int
free_attributes(Plugin_Attrs *attrs)
{
  Plugin_Attrs *cur;

  if (!attrs)
    return 0;

  while (attrs)
    {
      cur = attrs;
      attrs = cur->next;
      slapi_ch_free((void **)&cur);
    }

  return 1;
}


/******************************************************************************
 *  Create a list of attributes, from the string argument. This is a comma
 *  separated list of attributes, without white spaces! For instance:
 *
 *    parseAttributes("cn,uid,mail");
 */
PUBLIC Plugin_Attrs *
parse_attributes(char *str)
{
  char *tmp;
  Plugin_Attrs *cur, *ret;

  if (!str)
    return (Plugin_Attrs *)NULL;

  tmp = str;
  while (*tmp)
    {
      *tmp = tolower((int)*tmp);
      tmp++;
    }
  if (!(ret = (Plugin_Attrs *)slapi_ch_malloc(sizeof(Plugin_Attrs))))
    return (Plugin_Attrs *)NULL;

  cur = ret;
  tmp = strtok(str, ",");
  while (tmp)
    {
      if (!cur)
	{
	  free_attributes(ret);
	  return (Plugin_Attrs *)NULL;
	}

      cur->type = tmp;
      cur->first = *tmp;
      cur->len = strlen(tmp);

      if ((tmp = strtok(NULL, ",")))
	cur->next = (Plugin_Attrs *)
	  slapi_ch_malloc(sizeof(Plugin_Attrs));
      else
	cur->next = (Plugin_Attrs *)NULL;

      cur = cur->next;
    }

  return ret;
}


/******************************************************************************
 *  Check if a particular attribute type is in a Plugin Attribute list. This
 *  can be used to decide if a plugin/filter should be applied for instance.
 */
PUBLIC INLINE int
list_has_attribute(Plugin_Attrs *attrs, char *type)
{
  int len;

  if (!attrs || !type)
    return 0;

  len = strlen(type);
  while (attrs)
    {
      if ((attrs->first == *type) &&
	  (attrs->len == len) && (!strcmp(attrs->type, type)))
	return 1;

      attrs = attrs->next;
    }

  return 0;
}


/******************************************************************************
 *  Send a constraint violation error back to the client, with a more
 *  descriptive error message.
 */
PUBLIC int
send_constraint_err(Slapi_PBlock *pb, char *str)
{
  slapi_send_ldap_result(pb, LDAP_CONSTRAINT_VIOLATION, NULLCP, str, 0,
			 (BerVal **)NULL);

  return 1;
}


/******************************************************************************
 *  Send a constraint violation error back to the client, with a more
 *  descriptive error message.
 */
PUBLIC int
send_operations_err(Slapi_PBlock *pb, char *str)
{
  slapi_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULLCP, str, 0,
			 (BerVal **)NULL);

  return 1;
}


/******************************************************************************
 *  Get the ADD entry, and if it fails we send an error, and return FALSE. As
 *  a side effect the "entry" argument (a handle) is set to the structure.
 */
PUBLIC INLINE int
get_add_entry(Slapi_PBlock *pb, Slapi_Entry **entry, char *name)
{
  if (!entry)
    return 0;

  if (slapi_pblock_get(pb, SLAPI_ADD_ENTRY, entry) || !*entry)
    {
      slapi_log_error(LOG_FACILITY, name, ERR_NOENTRY);
      slapi_send_ldap_result(pb, LDAP_NO_MEMORY, NULLCP, ERR_NOENTRY, 0,
			     (BerVal **)NULL);
      
      return 0;
    }

  return 1;
}


/******************************************************************************
 *  Get the MODS for the current operation, and if it fails we send and error,
 *  and return FALSE. As a side effect, we set the "mods" argument (a handle)
 *  to the modifications structure.
 */
PUBLIC INLINE int
get_modify_mods(Slapi_PBlock *pb, LDAPMod ***mods, char *name)
{
  if (!mods)
    return 0;

  if (slapi_pblock_get(pb, SLAPI_MODIFY_MODS, mods) || !*mods)
    {
      slapi_log_error(LOG_FACILITY, name, ERR_NOMODS);
      slapi_send_ldap_result(pb, LDAP_NO_MEMORY, NULLCP, ERR_NOMODS, 0,
			     (BerVal **)NULL);

      return 0;
    }

  return 1;
}


/******************************************************************************
 *  Get the "command line arguments", and return TRUE if successful.
 */
PUBLIC int
get_slapi_args(Slapi_PBlock *pb, int *argc, char ***argv, int exp, char *name)
{
  if (slapi_pblock_get(pb, SLAPI_PLUGIN_ARGC, argc) ||
      slapi_pblock_get(pb, SLAPI_PLUGIN_ARGV, argv) ||
      (*argc < exp) || !*argv)
    {
      slapi_log_error(LOG_FACILITY, name, ERR_ARGS);

      return 0;
    }

  return 1;
}


/******************************************************************************
 *  Set the DN and authtype return values, making sure the pblock_set() did
 *  actually work.
 */
PUBLIC int
set_simple_auth_info(Slapi_PBlock *pb, char *dn, char *auth, char *name)
{
  char *setDN;

  if (!dn)
    return LDAP_OPERATIONS_ERROR;

  setDN = slapi_ch_strdup(dn);
  if (!setDN ||
      slapi_pblock_set(pb, SLAPI_CONN_DN, setDN) ||
      slapi_pblock_set(pb, SLAPI_CONN_AUTHTYPE, auth))
    {
      slapi_log_error(LOG_FACILITY, name, ERR_NOSET);

      return LDAP_OPERATIONS_ERROR;
    }

  return LDAP_SUCCESS;
}