pjs/directory/ds-plugins/lulu.c

268 строки
7.0 KiB
C
Исходник Обычный вид История

2000-01-12 09:15:45 +03:00
/* -*- Mode: C; eval: (c-set-style "GNU") -*-
2000-01-12 09:27:00 +03:00
******************************************************************************
* $Id: lulu.c,v 1.4 2004-04-25 21:07:10 gerv%gerv.net Exp $
2000-01-12 09:15:45 +03:00
*
* ***** 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
2000-01-12 09:15:45 +03:00
* 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.
2000-01-12 09:15:45 +03:00
*
* 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.
2000-01-12 09:15:45 +03:00
*
* 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 ***** */
/*****************************************************************************
2000-01-12 09:15:45 +03:00
* DESCRIPTION:
* Lots of Useful Little Utilities
*
2000-01-12 09:27:00 +03:00
*****************************************************************************/
2000-01-12 09:15:45 +03:00
#include <stdio.h>
#include "lulu.h"
2000-01-12 09:27:00 +03:00
/******************************************************************************
2000-01-12 09:15:45 +03:00
* Free the list of attributes, in case something went south...
*/
2000-01-15 01:25:29 +03:00
PUBLIC int
free_attributes(Plugin_Attrs *attrs)
2000-01-12 09:15:45 +03:00
{
2000-01-15 01:25:29 +03:00
Plugin_Attrs *cur;
2000-01-12 09:15:45 +03:00
if (!attrs)
2000-01-15 01:25:29 +03:00
return 0;
2000-01-12 09:15:45 +03:00
while (attrs)
{
cur = attrs;
attrs = cur->next;
slapi_ch_free((void **)&cur);
}
2000-01-15 01:25:29 +03:00
return 1;
2000-01-12 09:15:45 +03:00
}
2000-01-12 09:27:00 +03:00
/******************************************************************************
2000-01-12 09:15:45 +03:00
* 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");
*/
2000-01-15 01:25:29 +03:00
PUBLIC Plugin_Attrs *
parse_attributes(char *str)
2000-01-12 09:15:45 +03:00
{
2000-01-15 01:25:29 +03:00
char *tmp;
Plugin_Attrs *cur, *ret;
2000-01-12 09:15:45 +03:00
2000-01-15 01:25:29 +03:00
if (!str)
return (Plugin_Attrs *)NULL;
2000-01-12 09:15:45 +03:00
2000-01-15 01:25:29 +03:00
tmp = str;
2000-01-12 09:15:45 +03:00
while (*tmp)
{
*tmp = tolower((int)*tmp);
tmp++;
}
2000-01-15 01:25:29 +03:00
if (!(ret = (Plugin_Attrs *)slapi_ch_malloc(sizeof(Plugin_Attrs))))
return (Plugin_Attrs *)NULL;
2000-01-12 09:15:45 +03:00
cur = ret;
2000-01-15 01:25:29 +03:00
tmp = strtok(str, ",");
2000-01-12 09:15:45 +03:00
while (tmp)
{
if (!cur)
{
2000-01-15 01:25:29 +03:00
free_attributes(ret);
return (Plugin_Attrs *)NULL;
2000-01-12 09:15:45 +03:00
}
cur->type = tmp;
cur->first = *tmp;
cur->len = strlen(tmp);
2000-01-15 01:25:29 +03:00
if ((tmp = strtok(NULL, ",")))
cur->next = (Plugin_Attrs *)
slapi_ch_malloc(sizeof(Plugin_Attrs));
2000-01-12 09:15:45 +03:00
else
2000-01-15 01:25:29 +03:00
cur->next = (Plugin_Attrs *)NULL;
2000-01-12 09:15:45 +03:00
cur = cur->next;
}
return ret;
}
2000-01-12 09:27:00 +03:00
/******************************************************************************
2000-01-12 09:15:45 +03:00
* 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.
*/
2000-01-15 01:25:29 +03:00
PUBLIC INLINE int
list_has_attribute(Plugin_Attrs *attrs, char *type)
2000-01-12 09:15:45 +03:00
{
int len;
if (!attrs || !type)
2000-01-15 01:25:29 +03:00
return 0;
2000-01-12 09:15:45 +03:00
len = strlen(type);
while (attrs)
{
if ((attrs->first == *type) &&
2000-01-15 01:25:29 +03:00
(attrs->len == len) && (!strcmp(attrs->type, type)))
return 1;
2000-01-12 09:15:45 +03:00
attrs = attrs->next;
}
2000-01-15 01:25:29 +03:00
return 0;
2000-01-12 09:15:45 +03:00
}
2000-01-12 09:27:00 +03:00
/******************************************************************************
2000-01-12 09:15:45 +03:00
* Send a constraint violation error back to the client, with a more
* descriptive error message.
*/
2000-01-15 01:25:29 +03:00
PUBLIC int
send_constraint_err(Slapi_PBlock *pb, char *str)
2000-01-12 09:15:45 +03:00
{
slapi_send_ldap_result(pb, LDAP_CONSTRAINT_VIOLATION, NULLCP, str, 0,
(BerVal **)NULL);
2000-01-15 01:25:29 +03:00
return 1;
2000-01-12 09:15:45 +03:00
}
2000-01-12 09:27:00 +03:00
/******************************************************************************
2000-01-12 09:15:45 +03:00
* Send a constraint violation error back to the client, with a more
* descriptive error message.
*/
2000-01-15 01:25:29 +03:00
PUBLIC int
send_operations_err(Slapi_PBlock *pb, char *str)
2000-01-12 09:15:45 +03:00
{
slapi_send_ldap_result(pb, LDAP_OPERATIONS_ERROR, NULLCP, str, 0,
(BerVal **)NULL);
2000-01-15 01:25:29 +03:00
return 1;
2000-01-12 09:15:45 +03:00
}
2000-01-12 09:27:00 +03:00
/******************************************************************************
2000-01-12 09:15:45 +03:00
* 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.
*/
2000-01-15 01:25:29 +03:00
PUBLIC INLINE int
get_add_entry(Slapi_PBlock *pb, Slapi_Entry **entry, char *name)
2000-01-12 09:15:45 +03:00
{
if (!entry)
2000-01-15 01:25:29 +03:00
return 0;
2000-01-12 09:15:45 +03:00
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);
2000-01-15 01:25:29 +03:00
return 0;
2000-01-12 09:15:45 +03:00
}
2000-01-15 01:25:29 +03:00
return 1;
2000-01-12 09:15:45 +03:00
}
2000-01-12 09:27:00 +03:00
/******************************************************************************
2000-01-12 09:15:45 +03:00
* 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.
*/
2000-01-15 01:25:29 +03:00
PUBLIC INLINE int
get_modify_mods(Slapi_PBlock *pb, LDAPMod ***mods, char *name)
2000-01-12 09:15:45 +03:00
{
if (!mods)
2000-01-15 01:25:29 +03:00
return 0;
2000-01-12 09:15:45 +03:00
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);
2000-01-15 01:25:29 +03:00
return 0;
2000-01-12 09:15:45 +03:00
}
2000-01-15 01:25:29 +03:00
return 1;
2000-01-12 09:15:45 +03:00
}
2000-01-12 09:27:00 +03:00
/******************************************************************************
2000-01-12 09:15:45 +03:00
* Get the "command line arguments", and return TRUE if successful.
*/
2000-01-15 01:25:29 +03:00
PUBLIC int
get_slapi_args(Slapi_PBlock *pb, int *argc, char ***argv, int exp, char *name)
2000-01-12 09:15:45 +03:00
{
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);
2000-01-15 01:25:29 +03:00
return 0;
2000-01-12 09:15:45 +03:00
}
2000-01-15 01:25:29 +03:00
return 1;
2000-01-12 09:15:45 +03:00
}
2000-01-12 09:27:00 +03:00
/******************************************************************************
2000-01-12 09:15:45 +03:00
* Set the DN and authtype return values, making sure the pblock_set() did
* actually work.
*/
PUBLIC int
2000-01-15 01:25:29 +03:00
set_simple_auth_info(Slapi_PBlock *pb, char *dn, char *auth, char *name)
2000-01-12 09:15:45 +03:00
{
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;
}