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

📄 ldb_ldif.c

📁 samba最新软件
💻 C
📖 第 1 页 / 共 2 页
字号:
/*    ldb database library   Copyright (C) Andrew Tridgell  2004     ** NOTE! The following LGPL license applies to the ldb     ** library. This does NOT imply that all of Samba is released     ** under the LGPL      This library is free software; you can redistribute it and/or   modify it under the terms of the GNU Lesser General Public   License as published by the Free Software Foundation; either   version 3 of the License, or (at your option) any later version.   This library is distributed in the hope that it will be useful,   but WITHOUT ANY WARRANTY; without even the implied warranty of   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU   Lesser General Public License for more details.   You should have received a copy of the GNU Lesser General Public   License along with this library; if not, see <http://www.gnu.org/licenses/>.*//* *  Name: ldb * *  Component: ldif routines * *  Description: ldif pack/unpack routines * *  Author: Andrew Tridgell *//*  see RFC2849 for the LDIF format definition*/#include "ldb_includes.h"#include "system/locale.h"/*  */static int ldb_read_data_file(void *mem_ctx, struct ldb_val *value){	struct stat statbuf;	char *buf;	int count, size, bytes;	int ret;	int f;	const char *fname = (const char *)value->data;	if (strncmp(fname, "file://", 7) != 0) {		return LDB_ERR_INVALID_ATTRIBUTE_SYNTAX;	}	fname += 7;	f = open(fname, O_RDONLY);	if (f == -1) {		return -1;	}	if (fstat(f, &statbuf) != 0) {		ret = -1;		goto done;	}	if (statbuf.st_size == 0) {		ret = -1;		goto done;	}	value->data = (uint8_t *)talloc_size(mem_ctx, statbuf.st_size + 1);	if (value->data == NULL) {		ret = -1;		goto done;	}	value->data[statbuf.st_size] = 0;	count = 0;	size = statbuf.st_size;	buf = (char *)value->data;	while (count < statbuf.st_size) {		bytes = read(f, buf, size);		if (bytes == -1) {			talloc_free(value->data);			ret = -1;			goto done;		}		count += bytes;		buf += bytes;		size -= bytes;	}	value->length = statbuf.st_size;	ret = statbuf.st_size;done:	close(f);	return ret;}/*  this base64 decoder was taken from jitterbug (written by tridge).  we might need to replace it with a new version*/int ldb_base64_decode(char *s){	const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";	int bit_offset=0, byte_offset, idx, i, n;	uint8_t *d = (uint8_t *)s;	char *p=NULL;	n=i=0;	while (*s && (p=strchr(b64,*s))) {		idx = (int)(p - b64);		byte_offset = (i*6)/8;		bit_offset = (i*6)%8;		d[byte_offset] &= ~((1<<(8-bit_offset))-1);		if (bit_offset < 3) {			d[byte_offset] |= (idx << (2-bit_offset));			n = byte_offset+1;		} else {			d[byte_offset] |= (idx >> (bit_offset-2));			d[byte_offset+1] = 0;			d[byte_offset+1] |= (idx << (8-(bit_offset-2))) & 0xFF;			n = byte_offset+2;		}		s++; i++;	}	if (bit_offset >= 3) {		n--;	}	if (*s && !p) {		/* the only termination allowed */		if (*s != '=') {			return -1;		}	}	/* null terminate */	d[n] = 0;	return n;}/*  encode as base64  caller frees*/char *ldb_base64_encode(void *mem_ctx, const char *buf, int len){	const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";	int bit_offset, byte_offset, idx, i;	const uint8_t *d = (const uint8_t *)buf;	int bytes = (len*8 + 5)/6, pad_bytes = (bytes % 4) ? 4 - (bytes % 4) : 0;	char *out;	out = talloc_array(mem_ctx, char, bytes+pad_bytes+1);	if (!out) return NULL;	for (i=0;i<bytes;i++) {		byte_offset = (i*6)/8;		bit_offset = (i*6)%8;		if (bit_offset < 3) {			idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F;		} else {			idx = (d[byte_offset] << (bit_offset-2)) & 0x3F;			if (byte_offset+1 < len) {				idx |= (d[byte_offset+1] >> (8-(bit_offset-2)));			}		}		out[i] = b64[idx];	}	for (;i<bytes+pad_bytes;i++)		out[i] = '=';	out[i] = 0;	return out;}/*  see if a buffer should be base64 encoded*/int ldb_should_b64_encode(const struct ldb_val *val){	unsigned int i;	uint8_t *p = val->data;	if (val->length == 0) {		return 0;	}	if (p[0] == ' ' || p[0] == ':') {		return 1;	}	for (i=0; i<val->length; i++) {		if (!isprint(p[i]) || p[i] == '\n') {			return 1;		}	}	return 0;}/* this macro is used to handle the return checking on fprintf_fn() */#define CHECK_RET do { if (ret < 0) return ret; total += ret; } while (0)/*  write a line folded string onto a file*/static int fold_string(int (*fprintf_fn)(void *, const char *, ...), void *private_data,			const char *buf, size_t length, int start_pos){	unsigned int i;	int total=0, ret;	for (i=0;i<length;i++) {		ret = fprintf_fn(private_data, "%c", buf[i]);		CHECK_RET;		if (i != (length-1) && (i + start_pos) % 77 == 0) {			ret = fprintf_fn(private_data, "\n ");			CHECK_RET;		}	}	return total;}#undef CHECK_RET/*  encode as base64 to a file*/static int base64_encode_f(struct ldb_context *ldb,			   int (*fprintf_fn)(void *, const char *, ...), 			   void *private_data,			   const char *buf, int len, int start_pos){	char *b = ldb_base64_encode(ldb, buf, len);	int ret;	if (!b) {		return -1;	}	ret = fold_string(fprintf_fn, private_data, b, strlen(b), start_pos);	talloc_free(b);	return ret;}static const struct {	const char *name;	enum ldb_changetype changetype;} ldb_changetypes[] = {	{"add",    LDB_CHANGETYPE_ADD},	{"delete", LDB_CHANGETYPE_DELETE},	{"modify", LDB_CHANGETYPE_MODIFY},	{NULL, 0}};/* this macro is used to handle the return checking on fprintf_fn() */#define CHECK_RET do { if (ret < 0) { talloc_free(mem_ctx); return ret; } total += ret; } while (0)/*  write to ldif, using a caller supplied write method*/int ldb_ldif_write(struct ldb_context *ldb,		   int (*fprintf_fn)(void *, const char *, ...), 		   void *private_data,		   const struct ldb_ldif *ldif){	TALLOC_CTX *mem_ctx;	unsigned int i, j;	int total=0, ret;	const struct ldb_message *msg;	mem_ctx = talloc_named_const(NULL, 0, "ldb_ldif_write");	msg = ldif->msg;	ret = fprintf_fn(private_data, "dn: %s\n", ldb_dn_get_linearized(msg->dn));	CHECK_RET;	if (ldif->changetype != LDB_CHANGETYPE_NONE) {		for (i=0;ldb_changetypes[i].name;i++) {			if (ldb_changetypes[i].changetype == ldif->changetype) {				break;			}		}		if (!ldb_changetypes[i].name) {			ldb_debug(ldb, LDB_DEBUG_ERROR, "Error: Invalid ldif changetype %d\n",				  ldif->changetype);			talloc_free(mem_ctx);			return -1;		}		ret = fprintf_fn(private_data, "changetype: %s\n", ldb_changetypes[i].name);		CHECK_RET;	}	for (i=0;i<msg->num_elements;i++) {		const struct ldb_schema_attribute *a;		a = ldb_schema_attribute_by_name(ldb, msg->elements[i].name);		if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {			switch (msg->elements[i].flags & LDB_FLAG_MOD_MASK) {			case LDB_FLAG_MOD_ADD:				fprintf_fn(private_data, "add: %s\n", 					   msg->elements[i].name);				break;			case LDB_FLAG_MOD_DELETE:				fprintf_fn(private_data, "delete: %s\n", 					   msg->elements[i].name);				break;			case LDB_FLAG_MOD_REPLACE:				fprintf_fn(private_data, "replace: %s\n", 					   msg->elements[i].name);				break;			}		}		for (j=0;j<msg->elements[i].num_values;j++) {			struct ldb_val v;			ret = a->syntax->ldif_write_fn(ldb, mem_ctx, &msg->elements[i].values[j], &v);			CHECK_RET;			if (ldb_should_b64_encode(&v)) {				ret = fprintf_fn(private_data, "%s:: ", 						 msg->elements[i].name);				CHECK_RET;				ret = base64_encode_f(ldb, fprintf_fn, private_data, 						      (char *)v.data, v.length,						      strlen(msg->elements[i].name)+3);				CHECK_RET;				ret = fprintf_fn(private_data, "\n");				CHECK_RET;			} else {				ret = fprintf_fn(private_data, "%s: ", msg->elements[i].name);				CHECK_RET;				ret = fold_string(fprintf_fn, private_data,						  (char *)v.data, v.length,						  strlen(msg->elements[i].name)+2);				CHECK_RET;				ret = fprintf_fn(private_data, "\n");				CHECK_RET;			}			if (v.data != msg->elements[i].values[j].data) {				talloc_free(v.data);			}		}		if (ldif->changetype == LDB_CHANGETYPE_MODIFY) {			fprintf_fn(private_data, "-\n");		}	}	ret = fprintf_fn(private_data,"\n");	CHECK_RET;	return total;}#undef CHECK_RET/*  pull a ldif chunk, which is defined as a piece of data ending in \n\n or EOF  this routine removes any RFC2849 continuations and comments  caller frees*/static char *next_chunk(struct ldb_context *ldb, 			int (*fgetc_fn)(void *), void *private_data){	size_t alloc_size=0, chunk_size = 0;	char *chunk = NULL;

⌨️ 快捷键说明

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