Exemple #1
1
// This function taken from openvpn-2.2.1 and tidied up a bit.
static int setenv_x509(X509_NAME *x509, const char *type)
{
	int i, n;
	int fn_nid;
	ASN1_OBJECT *fn;
	ASN1_STRING *val;
	X509_NAME_ENTRY *ent;
	const char *objbuf;
	uint8_t *buf;
	char *name_expand;
	size_t name_expand_size;

	n=X509_NAME_entry_count (x509);
	for(i=0; i<n; ++i)
	{
		if(!(ent=X509_NAME_get_entry (x509, i))
		  || !(fn=X509_NAME_ENTRY_get_object(ent))
		  || !(val=X509_NAME_ENTRY_get_data(ent))
		  || (fn_nid=OBJ_obj2nid(fn))==NID_undef
		  || !(objbuf=OBJ_nid2sn(fn_nid)))
			continue;
		buf=(uint8_t *)1; /* bug in OpenSSL 0.9.6b ASN1_STRING_to_UTF8 requires this workaround */
		if(ASN1_STRING_to_UTF8(&buf, val)<=0) continue;
		name_expand_size=64+strlen(objbuf);
		if(!(name_expand=(char *)malloc_w(name_expand_size, __func__)))
			return -1;
		snprintf(name_expand, name_expand_size,
			"X509_%s_%s", type, objbuf);
		sanitise(name_expand);
		sanitise((char*)buf);
		setenv(name_expand, (char*)buf, 1);
		free (name_expand);
		OPENSSL_free (buf);
	}
	return 0;
}
Exemple #2
0
LPSTR *GetSupportedFaxDrivers(UINT *uCount)
{
    HKEY  hKey = 0;
    DWORD dwType;
    DWORD cbBufSize;
    LPSTR lpSupFaxDrvBuf;
    LPSTR *alpSupFaxDrvList;

    *uCount = 0;

    // Open the registry key.
    if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                     "Software\\Microsoft\\Windows NT\\CurrentVersion\\WOW\\WowFax\\SupportedFaxDrivers",
                     0, KEY_READ, &hKey ) != ERROR_SUCCESS) {
        goto GSFD_error;
    }

    // Query value for size of buffer and allocate.
    if (RegQueryValueEx(hKey, "DriverNames", 0, &dwType, NULL, &cbBufSize) != ERROR_SUCCESS) {
        goto GSFD_error;
    }
    if ((dwType != REG_MULTI_SZ) ||
        ((lpSupFaxDrvBuf = (LPSTR) malloc_w(cbBufSize)) == NULL)) {
        goto GSFD_error;
    }

    if (RegQueryValueEx(hKey, "DriverNames", 0, &dwType, lpSupFaxDrvBuf, &cbBufSize) != ERROR_SUCCESS) {
        goto GSFD_error;
    }

    // Get the number of elements in the list
    if (*uCount = BuildStrList(lpSupFaxDrvBuf, NULL)) {
        // Build an array of pointers to the start of the strings in the list.
        alpSupFaxDrvList = (LPSTR *) malloc_w(*uCount * sizeof(LPSTR));
        RtlZeroMemory(alpSupFaxDrvList, *uCount * sizeof(LPSTR));
        if (alpSupFaxDrvList) {
            // Fill the array with string starting points.
            BuildStrList(lpSupFaxDrvBuf, alpSupFaxDrvList);
        }
    }
    goto GSFD_exit;

GSFD_error:
    LOGDEBUG(0,("WOW32!GetSupportedFaxDrivers failed!\n"));

GSFD_exit:
    if (hKey) {
        RegCloseKey(hKey);
    }
    return alpSupFaxDrvList;
}
Exemple #3
0
static
#endif
char *get_next_xattr_str(struct asfd *asfd, char **data, size_t *l,
	struct cntr *cntr, ssize_t *s, const char *path)
{
	char *ret=NULL;

	if((sscanf(*data, "%08X", (unsigned int *)s))!=1)
	{
		logw(asfd, cntr, "sscanf of xattr '%s' %zd failed for %s\n",
			*data, *l, path);
		return NULL;
	}
	*data+=8;
	*l-=8;
	if(!(ret=(char *)malloc_w((*s)+1, __func__)))
		return NULL;
	memcpy(ret, *data, *s);
	ret[*s]='\0';

	*data+=*s;
	*l-=*s;

	return ret;
}
Exemple #4
0
// Return 0 for OK, -1 for error, 1 for finished reading the file.
static int get_next_dindex(uint64_t **dnew, struct sbuf *sb, struct fzp *fzp)
{
	static struct blk blk;
	static struct iobuf rbuf;

	memset(&rbuf, 0, sizeof(rbuf));

	switch(iobuf_fill_from_fzp(&rbuf, fzp))
	{
		case -1: goto error;
		case 1: return 1; // Reached the end.
	}
	if(rbuf.cmd==CMD_SAVE_PATH)
	{
		if(blk_set_from_iobuf_savepath(&blk, &rbuf))
			goto error;
		*dnew=(uint64_t *)malloc_w(sizeof(uint64_t), __func__);
		**dnew=blk.savepath;
		iobuf_free_content(&rbuf);
		return 0;
	}
	else
		iobuf_log_unexpected(&sb->path, __func__);

error:
	iobuf_free_content(&rbuf);
	return -1;
}
Exemple #5
0
ULONG FASTCALL WWS32getprotobyname(PVDMFRAME pFrame)
{
    ULONG ul;
    register PGETPROTOBYNAME16 parg16;
    PPROTOENT protoent32;
    PPROTOENT16 protoent16;
    PSZ name32 = NULL;
    PBYTE name16;
    DWORD bytesRequired;

    if ( !WWS32IsThreadInitialized ) {
        SetLastError( WSANOTINITIALISED );
        RETURN((ULONG)NULL);
    }

    GETARGPTR( pFrame, sizeof(GETPROTOBYNAME16), parg16 );
    GETVDMPTR( parg16->Name, 32, name16 );

    if(name16) {
        name32 = malloc_w(strlen(name16)+1);
        strcpy(name32, name16);
    }

    protoent32 = (PPROTOENT) (*wsockapis[WOW_GETPROTOBYNAME].lpfn)( name32 );

    // Note: 16-bit callbacks resulting from above function
    //       call may have caused 16-bit memory movement
    FREEVDMPTR(name16);
    FREEARGPTR(parg16);

    if(name32) {
        free_w( name32 );
    }

    if ( protoent32 != NULL ) {

        GETVDMPTR( WWS32vProtoent, MAXGETHOSTSTRUCT, protoent16 );
        bytesRequired = CopyProtoent32To16(
                            protoent16,
                            WWS32vProtoent,
                            MAXGETHOSTSTRUCT,
                            protoent32
                            );
        ASSERT( bytesRequired < MAXGETHOSTSTRUCT );

        FLUSHVDMPTR( WWS32vProtoent, (USHORT) bytesRequired, protoent16 );
        FREEVDMPTR( protoent16 );
        ul = WWS32vProtoent;

    } else {

        ul = 0;
    }

    FREEVDMPTR(name16);
    FREEARGPTR(parg16);

    RETURN(ul);

} // WWS32getprotobyname
Exemple #6
0
char *iso8859_7_to_utf8(const char *iso) {

	unsigned char *uiso, *utf;
	unsigned i = 0, y = 0;

	uiso = (unsigned char *) iso;
	utf = malloc_w(strlen(iso) * 2);

	while (uiso[i] != '\0') {
		if (uiso[i] > 0xa0) {
			if (uiso[i] < 0xf0) {
				utf[y] = 0xce;
				utf[y + 1] = uiso[i] - 48;
			} else {
				utf[y] = 0xcf;
				utf[y + 1] = uiso[i] - 112;
			}
			y += 2;
		} else {
			utf[y] = uiso[i];
			y++;
		}
		i++;
	}
	utf[y] = '\0';
	return (char *) utf;
}
Exemple #7
0
PSZ StrDup(PSZ szStr)
{
    PSZ  pszTmp;

    pszTmp = malloc_w(strlen(szStr)+1);
    return(strcpy(pszTmp, szStr));
}
Exemple #8
0
BOOL W32AddDibInfo (
    HDC hdcMem,
    HANDLE hfile,
    HANDLE hsec,
    ULONG  nalignment,
    PVOID  newdib,
    PVOID  newIntelDib,
    HBITMAP hbm,
    ULONG dibsize,
    USHORT originaldibflags,
    USHORT originaldibsel
    )
{
    PDIBINFO pdi;
    
    if ((pdi = malloc_w (sizeof (DIBINFO))) == NULL)
        return FALSE;

    pdi->di_hdc     = hdcMem;
    pdi->di_hfile   = hfile;
    pdi->di_hsec    = hsec;
    pdi->di_nalignment    = nalignment;
    pdi->di_newdib  = newdib;
    pdi->di_newIntelDib = newIntelDib;
    pdi->di_hbm     = hbm;
    pdi->di_dibsize = dibsize;
    pdi->di_originaldibsel = originaldibsel;
    pdi->di_originaldibflags = originaldibflags;
    pdi->di_next    = pDibInfoHead;
    pdi->di_lockcount = 1;
    pDibInfoHead    = pdi;

    return TRUE;
}
Exemple #9
0
int extract_params(char *msg, char **argv[]) {

	char *savedptr;
	int argc = 0, size = STARTING_PARAMS;

	*argv = NULL;
	if (!msg)
		return 0;

	// Allocate enough starting space for most bot commands
	*argv = malloc_w(size * sizeof(char *));

	// split parameters separated by space or tab
	(*argv)[argc] = strtok_r(msg, " \t", &savedptr);
	while ((*argv)[argc]) {
		if (argc == size - 1) { // Double the array if it gets full
			*argv = realloc_w(*argv, size * 2 * sizeof(char *));
			size *= 2;
		}
		(*argv)[++argc] = strtok_r(NULL, " \t", &savedptr);
	}
	if (!argc) {
		free(*argv);
		*argv = NULL;
	}
	return argc;
}
Exemple #10
0
int do_get_entries_in_directory(DIR *directory, struct dirent ***nl,
	int *count, int (*compar)(const void *, const void *))
{
	int status;
	int allocated=0;
	struct dirent **ntmp=NULL;
	struct dirent *entry=NULL;
	struct dirent *result=NULL;

	// This here is doing a funky kind of scandir/alphasort
	// that can also run on Windows.
	while(1)
	{
		char *p;
		if(!(entry=(struct dirent *)malloc_w(
		  sizeof(struct dirent)+fs_name_max+100, __func__)))
			goto error;
		status=readdir_r(directory, entry, &result);
		if(status || !result)
		{
			free_v((void **)&entry);
			break;
		}

		p=entry->d_name;
		ASSERT(fs_name_max+1 > (int)sizeof(struct dirent)+strlen(p));

		if(!p
		  || !strcmp(p, ".")
		  || !strcmp(p, ".."))
		{
			free_v((void **)&entry);
			continue;
		}

		if(*count==allocated)
		{
			if(!allocated) allocated=10;
			else allocated*=2;

			if(!(ntmp=(struct dirent **)
			  realloc_w(*nl, allocated*sizeof(**nl), __func__)))
				goto error;
			*nl=ntmp;
		}
		(*nl)[(*count)++]=entry;
	}
	if(*nl && compar) qsort(*nl, *count, sizeof(**nl), compar);
	return 0;
error:
	free_v((void **)&entry);
	if(*nl)
	{
		int i;
		for(i=0; i<*count; i++)
			free_v((void **)&((*nl)[i]));
		free_v((void **)nl);
	}
	return -1;
}
Exemple #11
0
static int add_file(struct mystruct *s, struct file *f)
{
	struct file *newfile;
	if(!(newfile=(struct file *)malloc_w(sizeof(struct file), __func__)))
		return -1;
	memcpy(newfile, f, sizeof(struct file));
	newfile->next=s->files;
	s->files=newfile;
	return 0;
}
Exemple #12
0
int blks_generate_init(void)
{
	rconf_init(&rconf);
	if(!(win=win_alloc(&rconf))
	  || !(gbuf=(char *)malloc_w(rconf.blk_max, __func__)))
		return -1;
	gbuf_end=gbuf;
	gcp=gbuf;
	return 0;
}
Exemple #13
0
static int get_files_in_directory(DIR *directory, struct dirent ***nl, int *count)
{
	int status;
	int allocated=0;
	struct dirent **ntmp=NULL;
	struct dirent *entry=NULL;
	struct dirent *result=NULL;

	/* Graham says: this here is doing a funky kind of scandir/alphasort
	   that can also run on Windows.
	   TODO: split into a scandir function
	*/
	while(1)
	{
		char *p;
		if(!(entry=(struct dirent *)malloc_w(
			sizeof(struct dirent)+fs_name_max+100, __func__)))
				return -1;
		status=readdir_r(directory, entry, &result);
		if(status || !result)
		{
			free_v((void **)&entry);
			break;
		}

		p=entry->d_name;
		ASSERT(fs_name_max+1 > (int)sizeof(struct dirent)+strlen(p));

		/* Skip `.', `..', and excluded file names.  */
		if(!p || !strcmp(p, ".") || !strcmp(p, ".."))
		{
			free_v((void **)&entry);
			continue;
		}

		if(*count==allocated)
		{
			if(!allocated) allocated=10;
			else allocated*=2;

			if(!(ntmp=(struct dirent **)
			  realloc_w(*nl, allocated*sizeof(**nl), __func__)))
			{
				free_v((void **)&entry);
				return -1;
			}
			*nl=ntmp;
		}
		(*nl)[(*count)++]=entry;
	}
	if(*nl) qsort(*nl, *count, sizeof(**nl),
		(int (*)(const void *, const void *))myalphasort);
	return 0;
}
Exemple #14
0
bool openssl_crypto_init(void) {

	openssl_mtx = malloc_w(CRYPTO_num_locks() * sizeof(*openssl_mtx));

	for (int i = 0; i < CRYPTO_num_locks(); i++)
		if (pthread_mutex_init(&openssl_mtx[i], NULL))
			return false;

	CRYPTO_set_locking_callback(openssl_lock_cb);
	return true;
}
Exemple #15
0
int linkhash_add(char *fname, struct stat *statp, struct f_link **bucket)
{
	struct f_link *new_fl;
	if(!(new_fl=(struct f_link *)malloc_w(sizeof(struct f_link), __func__))
	  || !(new_fl->name=strdup_w(fname, __func__)))
		return -1;
	new_fl->ino=statp->st_ino;
	new_fl->dev=statp->st_dev;
	new_fl->next=*bucket;
	*bucket=new_fl;
	return 0;
}
Exemple #16
0
struct hash_weak *hash_weak_add(uint64_t weakint)
{
	struct hash_weak *newweak;
	if(!(newweak=(struct hash_weak *)
		malloc_w(sizeof(struct hash_weak), __func__)))
			return NULL;
	newweak->weak=weakint;
//logp("addweak: %016lX\n", weakint);
	newweak->strong=NULL;
	HASH_ADD_INT(hash_table, weak, newweak);
	return newweak;
}
Exemple #17
0
static int add_key(off_t st_size, struct file *f)
{
	struct mystruct *s;

	if(!(s=(struct mystruct *)malloc_w(sizeof(struct mystruct), __func__)))
		return -1;
	s->st_size = st_size;
	s->files=NULL;
	if(add_file(s, f)) return -1;
	HASH_ADD_INT(myfiles, st_size, s);
	return 0;
}
Exemple #18
0
static char *prepend(const char *oldpath, const char *newpath, const char *sep)
{
	int len=0;
	char *path=NULL;
	len+=strlen(oldpath);
	len+=strlen(newpath);
	len+=2;
	if(!(path=(char *)malloc_w(len, __func__)))
		return NULL;
	snprintf(path, len, "%s%s%s", oldpath, *oldpath?sep:"", newpath);
	return path;
}
Exemple #19
0
static struct hash_strong *hash_strong_add(struct hash_weak *hash_weak,
	struct blk *blk)
{
	struct hash_strong *newstrong;
	if(!(newstrong=(struct hash_strong *)
		malloc_w(sizeof(struct hash_strong), __func__)))
			return NULL;
	memcpy(newstrong->savepath, blk->savepath, SAVE_PATH_LEN);
	memcpy(newstrong->md5sum, blk->md5sum, MD5_DIGEST_LENGTH);
	newstrong->next=hash_weak->strong;
	return newstrong;
}
Exemple #20
0
int submit(char *username, char *password) {
	int i=0;
	char *path;
	char *valid_username;
	char *valid_password;

	valid_username = (char*)malloc_w(sizeof(char)*81);

	if (max_connects == num_connects && max_connects != 0) {
		if (!quiet)
			printf("max connects reached at %i\n", num_connects);
		fprintf(log, "max connects reached at %i\n", num_connects);
		exit(EXIT_SUCCESS);
	}
	num_connects++;
	
	if (!submit_dummy_list) {
		path = search_path(submit_dummy_file, pathlist);
		submit_dummy_list = textlist(path);
	}	

	if (!quiet && (debug_level == 0))	{
		printf("trying %s->%s\r", username, password);
		fflush(stdout);
	} else if (debug_level > 0)
			printf("trying %s->%s\n", username, password);

	while (submit_dummy_list[i] != NULL) {
		strncpy(valid_username, submit_dummy_list[i], sizeof(char)*81);
		valid_password = extract(valid_username, ':');
		if (!valid_password) {
			fprintf(stderr, "submit_dummy: list element invalid\n");
			exit(EXIT_FAILURE);
		}
		if (!strcmp(valid_username, username) && !strcmp(valid_password, password)) {
			free(valid_username);
			if (!quiet && (debug_level == 0)) {
				printf("%s\r", BLANKSPACE);
				fflush(stdout);
			}
			return(EXIT_SUCCESS);
		}
		i++;
	}

	if (!quiet && (debug_level == 0)) {
		printf("%s\r", BLANKSPACE);
		fflush(stdout);
	}
	free(valid_username);
	return(EXIT_FAILURE);
}
Exemple #21
0
static int hooks_alloc(struct hooks **hnew, char **path, char **fingerprints)
{
	if(!*path || !*fingerprints) return 0;

	if(!(*hnew=(struct hooks *)malloc_w(sizeof(struct hooks), __func__)))
		return -1;
	
	(*hnew)->path=*path;
	(*hnew)->fingerprints=*fingerprints;
	*fingerprints=NULL;
	*path=NULL;
	return 0;
}
Exemple #22
0
VOID CacheBlockInit(PBLKCACHE pc, DWORD dwCacheSize)
{
    PBLKHEADER pCache = (PBLKHEADER)malloc_w(dwCacheSize);

    RtlZeroMemory(pc, sizeof(*pc));

    if (NULL != pCache) {
        pc->pCache = (LPBYTE)pCache;
        pc->pCacheFree = pCache;
        pc->dwCacheSize= dwCacheSize;
        pCache->dwSize = dwCacheSize;
        pCache->pNext  = NULL;
    }
}
Exemple #23
0
regex_t *regex_compile(const char *str)
{
	regex_t *regex=NULL;
	if((regex=(regex_t *)malloc_w(sizeof(regex_t), __func__))
	  && !regcomp(regex, str, REG_EXTENDED
#ifdef HAVE_WIN32
// Give Windows another helping hand and make the regular expressions
// case insensitive.
		| REG_ICASE
#endif
	)) return regex;

	regex_free(&regex);
	return NULL;
}
Exemple #24
0
static char *get_crl_path(const char *ca_name)
{
	int flen=0;
	char *fname=NULL;
	char *crl_path=NULL;
	flen+=strlen(ca_name);
	flen+=strlen("CA_");
	flen+=strlen(".crl")+1;
	if(!(fname=(char *)malloc_w(flen, __func__)))
		goto end;
	snprintf(fname, flen, "CA_%s.crl", ca_name);
	crl_path=prepend_s(gca_dir, fname);
end:
	free_w(&fname);
	return crl_path;
}
Exemple #25
0
ULONG FASTCALL WWS32gethostname(PVDMFRAME pFrame)
{
    ULONG ul;
    register PGETHOSTNAME16 parg16;
    PCHAR name32 = NULL;
    PCHAR name16;
    INT   NameLength;
    VPSZ  vpszName;

    if ( !WWS32IsThreadInitialized ) {
        SetLastError( WSANOTINITIALISED );
        RETURN((ULONG)NULL);
    }

    GETARGPTR( pFrame, sizeof(GETHOSTNAME16), parg16 );

    vpszName = FETCHDWORD(parg16->Name);
    NameLength = INT32(parg16->NameLength);

    if(vpszName) {
        name32 = malloc_w(NameLength);
    }

    ul = GETWORD16( (*wsockapis[WOW_GETHOSTNAME].lpfn)( name32, NameLength ) );

    // Note: 16-bit callbacks resulting from above function
    //       call may have caused 16-bit memory movement
    FREEVDMPTR(name16);
    FREEARGPTR(parg16);

    GETVDMPTR( vpszName, NameLength, name16 );
    if(name16 && name32) {
        strcpy(name16, name32);
    }
    FLUSHVDMPTR( vpszName, NameLength, name16 );

    FREEVDMPTR( name16 );
    FREEARGPTR(parg16);

    RETURN(ul);

} // WWS32gethostname
Exemple #26
0
static void add_to_ioevent(struct ioevent_list *ioevent_list,
	int *i, int ret, enum cmd cmd, void *data, int dlen)
{
	struct ioevent *ioevent;
	ioevent_list_grow(ioevent_list);
	ioevent=ioevent_list->ioevent;
	ioevent[*i].ret=ret;
	ioevent[*i].no_op=0;
	ioevent[*i].iobuf.cmd=cmd;
	ioevent[*i].iobuf.len=dlen;
	ioevent[*i].iobuf.buf=NULL;
	if(dlen)
	{
		fail_unless((ioevent[*i].iobuf.buf=
			(char *)malloc_w(dlen+1, __func__))!=NULL);
		fail_unless(memcpy(ioevent[*i].iobuf.buf,
			data, dlen+1)!=NULL);
	}
	(*i)++;
}
Exemple #27
0
static char *xstrsub(const char *src, int begin, int len)
{
	int l;
	int ind;
	char *ret;
	size_t s_full;

	s_full=strlen(src);
	if(len==-1) l=(int)s_full;
	else l=len;

	if(!(ret=(char *)malloc_w((xmin(s_full, l)+1)*sizeof(char), __func__)))
		return NULL;
	ind=begin<0?xmax((int) s_full+begin, 0):xmin(s_full, begin);

	strncpy(ret, src+ind, xmin(s_full, l));
	ret[xmin(s_full, l)] = '\0';

	return ret;
}
Exemple #28
0
static int append_for_champ_chooser(struct asfd *chfd,
	struct blist *blist, int sigs_end)
{
	static int finished_sending=0;
	static struct iobuf *wbuf=NULL;
	if(!wbuf)
	{
		if(!(wbuf=iobuf_alloc())
		  || !(wbuf->buf=(char *)malloc_w(128, __func__)))
			return -1;
		wbuf->cmd=CMD_SIG;
	}
	while(blist->blk_for_champ_chooser)
	{
		// FIX THIS: This should not need to be done quite like this.
		// Make weak/strong into uint64 and uint8_t array, then
		// send them unconverted.
		wbuf->len=snprintf(wbuf->buf, 128,
#ifdef HAVE_WIN32
			"%016I64X%s",
#else
			"%016lX%s",
#endif
			blist->blk_for_champ_chooser->fingerprint,
			bytes_to_md5str(blist->blk_for_champ_chooser->md5sum));

		if(chfd->append_all_to_write_buffer(chfd, wbuf))
			return 0; // Try again later.
		blist->blk_for_champ_chooser=blist->blk_for_champ_chooser->next;
	}
	if(sigs_end && !finished_sending && !blist->blk_for_champ_chooser)
	{
		wbuf->cmd=CMD_GEN;
		wbuf->len=snprintf(wbuf->buf, 128, "%s", "sigs_end");
		if(chfd->append_all_to_write_buffer(chfd, wbuf))
			return 0; // Try again later.
		finished_sending++;
	}
	return 0;
}
Exemple #29
0
static int do_iobuf_fill_from_fzp(struct iobuf *iobuf, struct fzp *fzp,
	int extra_bytes)
{
	static unsigned int s;
	static char lead[5]="";

	switch(fzp_read_ensure(fzp, lead, sizeof(lead), __func__))
	{
		case 0: break; // OK.
		case 1: return 1; // Finished OK.
		default:
		{
			logp("Error reading lead in %s\n", __func__);
			return -1; // Error.
		}
	}
	if((sscanf(lead, "%c%04X", (char *)&iobuf->cmd, &s))!=2)
	{
		logp("sscanf failed reading manifest: %s\n", lead);
		return -1;
	}
	iobuf->len=(size_t)s;
	if(!(iobuf->buf=(char *)malloc_w(
		iobuf->len+extra_bytes+1, __func__)))
			return -1;
	switch(fzp_read_ensure(fzp,
		iobuf->buf, iobuf->len+extra_bytes, __func__))
	{
		case 0: break; // OK.
		case 1: return 1; // Finished OK.
		default:
			logp("Error attempting to read after %s in %s (%c:%u)\n", lead, __func__, iobuf->cmd, s);
			return -1;
	}
	iobuf->buf[iobuf->len]='\0';
	return 0;
}
Exemple #30
0
static int do_recursive_delete(const char *d, const char *file,
	uint8_t delfiles, int32_t name_max)
{
	int ret=RECDEL_ERROR;
	DIR *dirp=NULL;
	struct dirent *entry=NULL;
	struct dirent *result;
	struct stat statp;
	char *directory=NULL;
	char *fullpath=NULL;

	if(!file)
	{
		if(!(directory=prepend_s(d, ""))) goto end;
	}
	else if(!(directory=prepend_s(d, file)))
	{
		log_out_of_memory(__func__);
		goto end;
	}

	if(lstat(directory, &statp))
	{
		// path does not exist.
		ret=RECDEL_OK;
		goto end;
	}

	if(!(dirp=opendir(directory)))
	{
		logp("opendir %s: %s\n", directory, strerror(errno));
		goto end;
	}

	if(!(entry=(struct dirent *)
		malloc_w(sizeof(struct dirent)+name_max+100, __func__)))
			goto end;

	while(1)
	{
		if(readdir_r(dirp, entry, &result) || !result)
		{
			// Got to the end of the directory.
			ret=RECDEL_OK;
			break;
		}

		if(!entry->d_ino
		  || !strcmp(entry->d_name, ".")
		  || !strcmp(entry->d_name, ".."))
			continue;
		free_w(&fullpath);
		if(!(fullpath=prepend_s(directory, entry->d_name)))
			goto end;

		if(is_dir(fullpath, entry)>0)
		{
			int r;
			if((r=do_recursive_delete(directory, entry->d_name,
				delfiles, name_max))==RECDEL_ERROR)
					goto end;
			// do not overwrite ret with OK if it previously
			// had ENTRIES_REMAINING
			if(r==RECDEL_ENTRIES_REMAINING) ret=r;
		}
		else if(delfiles)
		{
			if(unlink(fullpath))
			{
				logp("unlink %s: %s\n",
					fullpath, strerror(errno));
				ret=RECDEL_ENTRIES_REMAINING;
			}
		}
		else
		{
			ret=RECDEL_ENTRIES_REMAINING;
		}
	}

	if(ret==RECDEL_OK && rmdir(directory))
	{
		logp("rmdir %s: %s\n", directory, strerror(errno));
		ret=RECDEL_ERROR;
	}
end:
	if(dirp) closedir(dirp);
	free_w(&fullpath);
	free_w(&directory);
	free_v((void **)&entry);
	return ret;
}