⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 modify.c

📁 开放源码的ldap系统
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *	 Copyright 1999, Dmitry Kovalev <mit@openldap.org>, All rights reserved. * *	 Redistribution and use in source and binary forms are permitted only *	 as authorized by the OpenLDAP Public License.	A copy of this *	 license is available at http://www.OpenLDAP.org/license.html or *	 in file LICENSE in the top-level directory of the distribution. */#include "portable.h"#ifdef SLAPD_SQL#include <stdio.h>#include <sys/types.h>#include <string.h>#include "slap.h"#include "back-sql.h"#include "sql-wrap.h"#include "schema-map.h"#include "entry-id.h"#include "util.h"int backsql_modify(BackendDB *be,Connection *conn,Operation *op,	const char *dn,const char *ndn,Modifications *modlist){ backsql_info *bi=(backsql_info*)be->be_private; SQLHDBC dbh; SQLHSTMT sth; RETCODE rc; backsql_oc_map_rec *oc=NULL; backsql_entryID e_id,*res; Modification *c_mod; Modifications *ml; backsql_at_map_rec *at=NULL; struct berval *at_val; int i; int pno,po;//first parameter no, parameter order int prc; //procedure return code Debug(LDAP_DEBUG_TRACE,"==>backsql_modify(): changing entry '%s'\n",ndn,0,0); dbh=backsql_get_db_conn(be,conn); if (!dbh) {  Debug(LDAP_DEBUG_TRACE,"backsql_modify(): could not get connection handle - exiting\n",0,0,0);  send_ldap_result(conn,op,LDAP_OTHER,"","SQL-backend error",NULL,NULL);  return 1; } res=backsql_dn2id(bi,&e_id,dbh,(char*)ndn); if (res==NULL) {  Debug(LDAP_DEBUG_TRACE,"backsql_modify(): could not lookup entry id\n",0,0,0);  send_ldap_result(conn,op,LDAP_NO_SUCH_OBJECT,"",NULL,NULL,NULL);  return 1; } oc=backsql_oc_with_id(bi,e_id.oc_id); if (oc==NULL) {  Debug(LDAP_DEBUG_TRACE,"backsql_modify(): cannot determine objectclass of entry -- aborting\n",0,0,0);  send_ldap_result(conn,op,LDAP_OTHER,"","SQL-backend error",NULL,NULL);  return 1; } SQLAllocStmt(dbh, &sth); Debug(LDAP_DEBUG_TRACE,"backsql_modify(): traversing modifications list\n",0,0,0); for(ml=modlist;ml!=NULL;ml=ml->sml_next) {  c_mod=&ml->sml_mod;  Debug(LDAP_DEBUG_TRACE,"backsql_modify(): attribute '%s'\n",c_mod->sm_desc->ad_cname->bv_val,0,0);  at=backsql_at_with_name(oc,c_mod->sm_desc->ad_cname->bv_val);  if (at==NULL)  {   Debug(LDAP_DEBUG_TRACE,"backsql_modify(): attribute provided is not registered in this objectclass ('%s')\n",c_mod->sm_desc->ad_cname->bv_val,0,0);   continue;  }    switch(c_mod->sm_op)  {   case LDAP_MOD_REPLACE:			{			 SQLHSTMT asth;			 BACKSQL_ROW_NTS row;			 			 Debug(LDAP_DEBUG_TRACE,"backsql_modify(): replacing values for attribute '%s'\n",at->name,0,0);             if (at->add_proc==NULL)			 {			  Debug(LDAP_DEBUG_TRACE,"backsql_modify(): add procedure is not defined for this attribute ('%s') - unable to perform replacements\n",at->name,0,0);			  break;			 }del_all:			 			 if ((rc=backsql_Prepare(dbh,&asth,at->query,0)) != SQL_SUCCESS)			 {			  Debug(LDAP_DEBUG_TRACE,"backsql_modify(): error preparing query\n",0,0,0);			  backsql_PrintErrors(bi->db_env,dbh,asth,rc);			  break;			 }			 if (backsql_BindParamID(asth,1,&e_id.keyval) != SQL_SUCCESS)			 {			  Debug(LDAP_DEBUG_TRACE,"backsql_modify(): error binding key value parameter\n",0,0,0);			  backsql_PrintErrors(bi->db_env,dbh,asth,rc);			  SQLFreeStmt(asth,SQL_DROP);			  break;			 }			 if ((rc=SQLExecute(asth)) != SQL_SUCCESS && rc!= SQL_SUCCESS_WITH_INFO)			 {			  Debug(LDAP_DEBUG_TRACE,"backsql_modify(): error executing attribute query\n",0,0,0);			  backsql_PrintErrors(bi->db_env,dbh,asth,rc);			  SQLFreeStmt(asth,SQL_DROP);			  break;			 }			 backsql_BindRowAsStrings(asth,&row);			 while ((rc=SQLFetch(asth)) == SQL_SUCCESS || rc==SQL_SUCCESS_WITH_INFO)			 {			  for (i=0;i<row.ncols;i++)			  {			   if (at->expect_return & BACKSQL_DEL)			   {			    pno=1;			    SQLBindParameter(sth,1,SQL_PARAM_OUTPUT,SQL_C_ULONG,SQL_INTEGER,0,0,&prc,0,0);			   }			   else			    pno=0;			   po=(at->param_order & BACKSQL_DEL)>0;			   SQLBindParameter(sth,(SQLUSMALLINT)(pno+1+po),SQL_PARAM_INPUT,SQL_C_ULONG,SQL_INTEGER,0,0,&e_id.keyval,0,0);			   //check for syntax needed here - maybe need binary bind?			   SQLBindParameter(sth,(SQLUSMALLINT)(pno+2-po),SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,0,0,row.cols[i],strlen(row.cols[i]),0);			 			   Debug(LDAP_DEBUG_TRACE,"backsql_modify(): executing '%s'\n",at->delete_proc,0,0);			   rc=SQLExecDirect(sth,at->delete_proc,SQL_NTS);			   if (rc!=SQL_SUCCESS)				{			     Debug(LDAP_DEBUG_TRACE,"backsql_modify(): delete_proc execution failed\n",0,0,0);			     backsql_PrintErrors(bi->db_env,dbh,sth,rc);				}			  }			 }			 backsql_FreeRow(&row);             SQLFreeStmt(asth,SQL_DROP);			}			//PASSTHROUGH - to add new attributes -- do NOT add break  case LDAP_MOD_ADD:			if (at->add_proc==NULL)			{			 Debug(LDAP_DEBUG_TRACE,"backsql_modify(): add procedure is not defined for this attribute ('%s')\n",at->name,0,0);			 break;			}			if (c_mod->sm_bvalues==NULL)			{			 Debug(LDAP_DEBUG_TRACE,"backsql_modify(): no values given to add for attribute '%s'\n",at->name,0,0);			 break;			}			Debug(LDAP_DEBUG_TRACE,"backsql_modify(): adding new values for attribute '%s'\n",at->name,0,0);			for(i=0,at_val=c_mod->sm_bvalues[0];at_val!=NULL;i++,at_val=c_mod->sm_bvalues[i])			{			 if (at->expect_return & BACKSQL_ADD)			 {			  pno=1;			  SQLBindParameter(sth,1,SQL_PARAM_OUTPUT,SQL_C_ULONG,SQL_INTEGER,0,0,&prc,0,0);			 }			 else			  pno=0;			 po=(at->param_order & BACKSQL_ADD)>0;			 SQLBindParameter(sth,(SQLUSMALLINT)(pno+1+po),SQL_PARAM_INPUT,SQL_C_ULONG,SQL_INTEGER,0,0,&e_id.keyval,0,0);			 //check for syntax needed here - maybe need binary bind?			 SQLBindParameter(sth,(SQLUSMALLINT)(pno+2-po),SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,0,0,at_val->bv_val,at_val->bv_len,0);			 			 Debug(LDAP_DEBUG_TRACE,"backsql_modify(): executing '%s'\n",at->add_proc,0,0);			 rc=SQLExecDirect(sth,at->add_proc,SQL_NTS);			 if (rc!=SQL_SUCCESS)			 {			  Debug(LDAP_DEBUG_TRACE,"backsql_modify(): add_proc execution failed\n",0,0,0);			  backsql_PrintErrors(bi->db_env,dbh,sth,rc);			 }			}			break;  case LDAP_MOD_DELETE:			if (at->delete_proc==NULL)			{			 Debug(LDAP_DEBUG_TRACE,"backsql_modify(): delete procedure is not defined for this attribute ('%s')\n",at->name,0,0);			 break;			}			if (c_mod->sm_bvalues==NULL)			{			 Debug(LDAP_DEBUG_TRACE,"backsql_modify(): no values given to delete for attribute '%s' -- deleting all values\n",at->name,0,0);			 goto del_all;			}            Debug(LDAP_DEBUG_TRACE,"backsql_modify(): deleting values for attribute '%s'\n",at->name,0,0);			for(i=0,at_val=c_mod->sm_bvalues[0];at_val!=NULL;i++,at_val=c_mod->sm_bvalues[i])			{			 if (at->expect_return & BACKSQL_DEL)			  {			   pno=1;			   SQLBindParameter(sth,1,SQL_PARAM_OUTPUT,SQL_C_ULONG,SQL_INTEGER,0,0,&prc,0,0);			  }			 else			  pno=0;			 po=(at->param_order & BACKSQL_DEL)>0;			 SQLBindParameter(sth,(SQLUSMALLINT)(pno+1+po),SQL_PARAM_INPUT,SQL_C_ULONG,SQL_INTEGER,0,0,&e_id.keyval,0,0);			 //check for syntax needed here - maybe need binary bind?			 SQLBindParameter(sth,(SQLUSMALLINT)(pno+2-po),SQL_PARAM_INPUT,SQL_C_CHAR,SQL_CHAR,0,0,at_val->bv_val,at_val->bv_len,0);			   			 Debug(LDAP_DEBUG_TRACE,"backsql_modify(): executing '%s'\n",at->delete_proc,0,0);			 rc=SQLExecDirect(sth,at->delete_proc,SQL_NTS);			 if (rc!=SQL_SUCCESS)			 {			  Debug(LDAP_DEBUG_TRACE,"backsql_modify(): delete_proc execution failed\n",0,0,0);			  backsql_PrintErrors(bi->db_env,dbh,sth,rc);			 }			}			break;  }  SQLFreeStmt(sth,SQL_RESET_PARAMS); } SQLFreeStmt(sth,SQL_DROP); send_ldap_result(conn,op,LDAP_SUCCESS,"",NULL,NULL,NULL); Debug(LDAP_DEBUG_TRACE,"<==backsql_modify()\n",0,0,0); return 0;}int backsql_modrdn(BackendDB *be,Connection *conn,Operation *op,	const char *dn,const char *ndn,const char *newrdn,int deleteoldrdn,const char *newSuperior){ backsql_info *bi=(backsql_info*)be->be_private; SQLHDBC dbh; SQLHSTMT sth; RETCODE rc; backsql_oc_map_rec *oc=NULL; backsql_entryID e_id,pe_id,new_pid,*res; backsql_at_map_rec *at=NULL; char *p_dn=NULL,*new_pdn=NULL, *new_dn;   Debug(LDAP_DEBUG_TRACE,"==>backsql_modrdn() renaming entry '%s', newrdn='%s', newSuperior='%s'\n",dn,newrdn,newSuperior); dbh=backsql_get_db_conn(be,conn); if (!dbh) {  Debug(LDAP_DEBUG_TRACE,"backsql_modrdn(): could not get connection handle - exiting\n",0,0,0);  send_ldap_result(conn,op,LDAP_OTHER,"","SQL-backend error",NULL,NULL);  return 1; } res=backsql_dn2id(bi,&e_id,dbh,(char*)ndn); if (res==NULL) {  Debug(LDAP_DEBUG_TRACE,"backsql_modrdn(): could not lookup entry id\n",0,0,0);  send_ldap_result(conn,op,LDAP_NO_SUCH_OBJECT,"",NULL,NULL,NULL);  return 1; } Debug(LDAP_DEBUG_TRACE,"backsql_modrdn(): entry id is %d\n",e_id.id,0,0); p_dn=dn_parent(be,ndn); if (newSuperior)  new_pdn=dn_validate(ch_strdup(newSuperior)); else   new_pdn=p_dn; SQLAllocStmt(dbh, &sth); if (newSuperior && !strcasecmp(p_dn,new_pdn)) {  Debug(LDAP_DEBUG_TRACE,"backsql_modrdn(): newSuperior is equal to old parent - aborting\n",0,0,0);  send_ldap_result(conn,op,LDAP_OTHER,"",NULL,NULL,NULL);  goto modrdn_return; } if (newSuperior && !strcasecmp(ndn,new_pdn)) {  Debug(LDAP_DEBUG_TRACE,"backsql_modrdn(): newSuperior is equal to entry being moved - aborting\n",0,0,0);  send_ldap_result(conn,op,LDAP_OTHER,"",NULL,NULL,NULL);  goto modrdn_return; } build_new_dn( &new_dn, dn, new_pdn, newrdn );  if (!dn_validate(new_dn)) {  Debug(LDAP_DEBUG_TRACE,"backsql_modrdn(): new dn is invalid ('%s') - aborting\n",new_dn,0,0);  send_ldap_result(conn,op,LDAP_OTHER,"",NULL,NULL,NULL);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -