Beispiel #1
0
int libwrap_init() {
#ifdef USE_PTHREAD
    int i, j, rfd, result;
    char servname[SERVNAME_LEN];
    static int initialized=0;
    SERVICE_OPTIONS *opt;

    if(initialized) /* during startup or previous configuration file reload */
        return 0;
    for(opt=service_options.next; opt; opt=opt->next)
        if(opt->option.libwrap) /* libwrap is enabled for this service */
            break;
    if(!opt) /* disabled for all sections or inetd mode (no sections) */
        return 0;

    num_processes=LIBWRAP_CLIENTS;
    ipc_socket=str_alloc(2*num_processes*sizeof(int));
    busy=str_alloc(num_processes*sizeof(int));
    for(i=0; i<num_processes; ++i) { /* spawn a child */
        if(s_socketpair(AF_UNIX, SOCK_STREAM, 0, ipc_socket+2*i, 0, "libwrap_init"))
            return 1;
        switch(fork()) {
        case -1:    /* error */
            ioerror("fork");
            return 1;
        case  0:    /* child */
            drop_privileges(0); /* libwrap processes are not chrooted */
            close(0); /* stdin */
            close(1); /* stdout */
            if(!global_options.option.foreground) /* for logging in read_fd */
                close(2); /* stderr */
            for(j=0; j<=i; ++j) /* close parent-side sockets created so far */
                close(ipc_socket[2*j]);
            while(1) { /* main libwrap child loop */
                if(read_fd(ipc_socket[2*i+1], servname, SERVNAME_LEN, &rfd)<=0)
                    _exit(0);
                result=check(servname, rfd);
                write(ipc_socket[2*i+1], (u8 *)&result, sizeof result);
                if(rfd>=0)
                    close(rfd);
            }
        default:    /* parent */
            close(ipc_socket[2*i+1]); /* child-side socket */
        }
    }
    initialized=1;
#endif /* USE_PTHREAD */
    return 0;
}
NOEXPORT void win_log(LPSTR line) {
    struct LIST *curr;
    int len;
    static int log_len=0;
    LPTSTR txt;

    txt=str2tstr(line);
    len=_tcslen(txt);
    /* this list is shared between threads */
    curr=str_alloc(sizeof(struct LIST)+len*sizeof(TCHAR));
    curr->len=len;
    _tcscpy(curr->txt, txt);
    str_free(txt);
    curr->next=NULL;

    if(tail)
        tail->next=curr;
    tail=curr;
    if(!head)
        head=tail;
    log_len++;
    while(log_len>LOG_LINES) {
        curr=head;
        head=head->next;
        /* this list is shared between threads */
        str_free(curr);
        log_len--;
    }

    new_logs=1;
}
Beispiel #3
0
static SOCKADDR_LIST *dynamic_remote_addr(CLI *c) {
#ifdef SO_ORIGINAL_DST
    socklen_t addrlen=sizeof(SOCKADDR_UNION);
#endif /* SO_ORIGINAL_DST */

    /* check if the address was already set by a dynamic protocol
     * implemented protocols: CONNECT
     * protocols to be implemented: SOCKS4 */
    if(c->connect_addr.num)
        return &c->connect_addr;

#ifdef SO_ORIGINAL_DST
    if(c->opt->option.transparent_dst) {
        c->connect_addr.num=1;
        c->connect_addr.addr=str_alloc(sizeof(SOCKADDR_UNION));
        if(getsockopt(c->local_rfd.fd, SOL_IP, SO_ORIGINAL_DST,
                c->connect_addr.addr, &addrlen)) {
            sockerror("setsockopt SO_ORIGINAL_DST");
            longjmp(c->err, 1);
        }
        return &c->connect_addr;
    }
#endif /* SO_ORIGINAL_DST */

    if(c->opt->option.delayed_lookup) {
        if(!name2addrlist(&c->connect_addr,
                c->opt->connect_name, DEFAULT_LOOPBACK)) {
            s_log(LOG_ERR, "No host resolved");
            longjmp(c->err, 1);
        }
        return &c->connect_addr;
    }

    return &c->opt->connect_addr; /* use pre-resolved (static) addresses */
}
Beispiel #4
0
DISK_FILE *file_open(char *name, FILE_MODE mode) {
    DISK_FILE *df;
    LPTSTR tstr;
    HANDLE fh;
    DWORD desired_access, creation_disposition;

    /* open file */
    switch(mode) {
    case FILE_MODE_READ:
        desired_access=FILE_READ_DATA;
        creation_disposition=OPEN_EXISTING;
        break;
    case FILE_MODE_APPEND:
        desired_access=FILE_APPEND_DATA;
        creation_disposition=OPEN_ALWAYS;
        break;
    case FILE_MODE_OVERWRITE:
        desired_access=FILE_WRITE_DATA;
        creation_disposition=CREATE_ALWAYS;
        break;
    default: /* invalid mode */
        return NULL;
    }
    tstr=str2tstr(name);
    fh=CreateFile(tstr, desired_access, FILE_SHARE_READ, NULL,
        creation_disposition, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL);
    str_free(tstr); /* str_free() overwrites GetLastError() value */
    if(fh==INVALID_HANDLE_VALUE)
        return NULL;

    /* setup df structure */
    df=str_alloc(sizeof df);
    df->fh=fh;
    return df;
}
Beispiel #5
0
DISK_FILE *file_open(char *name, int wr) {
    DISK_FILE *df;
    int fd, flags;

    /* open file */
    if(wr)
        flags=O_CREAT|O_WRONLY|O_APPEND;
    else
        flags=O_RDONLY;
#ifdef O_NONBLOCK
    flags|=O_NONBLOCK;
#elif defined O_NDELAY
    flags|=O_NDELAY;
#endif
#ifdef O_CLOEXEC
    flags|=O_CLOEXEC;
#endif /* O_CLOEXEC */
    fd=open(name, flags, 0640);
    if(fd<0) {
        ioerror(name);
        return NULL;
    }

    /* setup df structure */
    df=str_alloc(sizeof df);
    df->fd=fd;
    return df;
}
Beispiel #6
0
static void log_time(const int level, const char *txt, ASN1_TIME *t) {
#ifndef HAVE_LIB_CYASSL
    char *cp;
    BIO *bio;
    int n;

    if(!t)
        return;
    bio=BIO_new(BIO_s_mem());
    if(!bio)
        return;
    ASN1_TIME_print(bio, t);
    n=BIO_pending(bio);
    cp=str_alloc(n+1);
    n=BIO_read(bio, cp, n);
    if(n<0) {
        BIO_free(bio);
        str_free(cp);
        return;
    }
    cp[n]='\0';
    BIO_free(bio);
    s_log(level, "%s: %s", txt, cp);
    str_free(cp);
#else
    s_log(level, "%s: N/A", txt);
#endif
}
Beispiel #7
0
DISK_FILE *file_fdopen(int fd) {
    DISK_FILE *df;

    df=str_alloc(sizeof(DISK_FILE));
    df->fd=fd;
    return df;
}
Beispiel #8
0
char *ssl_getline(CLI *c) { /* get newline-terminated string */
    char *line;
    size_t ptr=0, allocated=32;

    line=str_alloc(allocated);
    for(;;) {
        if(ptr>65536) { /* >64KB --> DoS protection */
            s_log(LOG_ERR, "ssl_getline: Line too long");
            str_free(line);
            longjmp(c->err, 1);
        }
        if(allocated<ptr+1) {
            allocated*=2;
            line=str_realloc(line, allocated);
        }
        s_ssl_read(c, line+ptr, 1);
        if(line[ptr]=='\r')
            continue;
        if(line[ptr]=='\n')
            break;
        if(line[ptr]=='\0')
            break;
        ++ptr;
    }
    line[ptr]='\0';
    s_log(LOG_DEBUG, " <- %s", line);
    return line;
}
Beispiel #9
0
gtf_row_t* gtf_row_alloc()
{
    gtf_row_t* row = malloc_or_die(sizeof(gtf_row_t));

    row->seqname = str_alloc();
    row->source  = str_alloc();
    row->feature = str_alloc();
    row->start      = -1;
    row->end        = -1;
    row->score      = 0.0;
    row->strand     = strand_na;
    row->frame      = -1;
    row->attributes = str_map_create();

    return row;
}
Beispiel #10
0
static CONTEXT *new_context(void) {
    static int next_id=1;
    CONTEXT *context;

    /* allocate and fill the CONTEXT structure */
    context=str_alloc(sizeof(CONTEXT));
    if(!context) {
        s_log(LOG_ERR, "Unable to allocate CONTEXT structure");
        return NULL;
    }
    str_detach(context);
    context->id=next_id++;
    context->fds=NULL;
    context->ready=0;

    /* append to the tail of the ready queue */
    context->next=NULL;
    if(ready_tail)
        ready_tail->next=context;
    ready_tail=context;
    if(!ready_head)
        ready_head=context;

    return context;
}
Beispiel #11
0
    /* log the line to the UI (GUI, stderr, etc.) */
    if(log_mode==LOG_MODE_ERROR ||
            (log_mode==LOG_MODE_INFO && level<LOG_DEBUG) ||
#if defined(USE_WIN32) || defined(USE_JNI)
            level<=opt->log_level
#else
            (level<=opt->log_level &&
            global_options.option.log_stderr)
#endif
            )
        ui_new_log(line);

    str_free(line);
}

#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat"
#pragma GCC diagnostic ignored "-Wformat-extra-args"
#endif /* __GNUC__ */
char *log_id(CLI *c) {
    const char table[62]=
        "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    unsigned char rnd[22];
    char *uniq;
    size_t i;
    unsigned long tid;

    switch(c->opt->log_id) {
    case LOG_ID_SEQUENTIAL:
        return str_printf("%llu", c->seq);
    case LOG_ID_UNIQUE:
        if(RAND_bytes(rnd, sizeof rnd)<=0) /* log2(62^22)=130.99 */
            return str_dup("error");
        for(i=0; i<sizeof rnd; ++i) {
            rnd[i]&=63;
            while(rnd[i]>=62) {
                if(RAND_bytes(rnd+i, 1)<=0)
                    return str_dup("error");
                rnd[i]&=63;
            }
        }
        uniq=str_alloc(sizeof rnd+1);
        for(i=0; i<sizeof rnd; ++i)
            uniq[i]=table[rnd[i]];
        uniq[sizeof rnd]='\0';
        return uniq;
    case LOG_ID_THREAD:
        tid=stunnel_thread_id();
        if(!tid) /* currently USE_FORK */
            tid=stunnel_process_id();
        return str_printf("%lu", tid);
    }
    return str_dup("error");
}
Beispiel #12
0
DISK_FILE *file_open(char *name, FILE_MODE mode) {
    DISK_FILE *df;
    int fd, flags;

    /* open file */
    switch(mode) {
    case FILE_MODE_READ:
        flags=O_RDONLY;
        break;
    case FILE_MODE_APPEND:
        flags=O_CREAT|O_WRONLY|O_APPEND;
        break;
    case FILE_MODE_OVERWRITE:
        flags=O_CREAT|O_WRONLY|O_TRUNC;
        break;
    default: /* invalid mode */
        return NULL;
    }
#ifdef O_NONBLOCK
    flags|=O_NONBLOCK;
#elif defined O_NDELAY
    flags|=O_NDELAY;
#endif
#ifdef O_CLOEXEC
    flags|=O_CLOEXEC;
#endif /* O_CLOEXEC */
    fd=open(name, flags, 0640);
    if(fd<0)
        return NULL;

    /* setup df structure */
    df=str_alloc(sizeof df);
    df->fd=fd;
    return df;
}
Beispiel #13
0
int file_putline(DISK_FILE *df, char *line) {
    int len;
    char *buff;
#ifdef USE_WIN32
    DWORD num;
#else /* USE_WIN32 */
    int num;
#endif /* USE_WIN32 */

    len=strlen(line);
    buff=str_alloc(len+2); /* +2 for CR+LF */
    strcpy(buff, line);
#ifdef USE_WIN32
    buff[len++]='\r'; /* CR */
#endif /* USE_WIN32 */
    buff[len++]='\n'; /* LF */
#ifdef USE_WIN32
    WriteFile(df->fh, buff, len, &num, NULL);
#else /* USE_WIN32 */
    /* no file -> write to stderr */
    num=write(df ? df->fd : 2, buff, len);
#endif /* USE_WIN32 */
    str_free(buff);
    return num;
}
Beispiel #14
0
NOEXPORT void sslerror_log(unsigned long err, char *txt) {
    char *error_string;

    error_string=str_alloc(256);
    ERR_error_string_n(err, error_string, 256);
    s_log(LOG_ERR, "%s: %lX: %s", txt, err, error_string);
    str_free(error_string);
}
Beispiel #15
0
char *fd_getline(CLI *c, int fd) {
    char *line, *tmpline;
    int ptr=0, allocated=32;

    line=str_alloc(allocated);
    for(;;) {
        s_poll_init(c->fds);
        s_poll_add(c->fds, fd, 1, 0); /* read */
        switch(s_poll_wait(c->fds, c->opt->timeout_busy, 0)) {
        case -1:
            sockerror("fd_getline: s_poll_wait");
            str_free(line);
            longjmp(c->err, 1); /* error */
        case 0:
            s_log(LOG_INFO, "fd_getline: s_poll_wait:"
                " TIMEOUTbusy exceeded: sending reset");
            str_free(line);
            longjmp(c->err, 1); /* timeout */
        case 1:
            break; /* OK */
        default:
            s_log(LOG_ERR, "fd_getline: s_poll_wait: Unknown result");
            str_free(line);
            longjmp(c->err, 1); /* error */
        }
        if(allocated<ptr+1) {
            allocated*=2;
            line=str_realloc(line, allocated);
        }
        switch(readsocket(fd, line+ptr, 1)) {
        case -1: /* error */
            sockerror("fd_getline: readsocket");
            str_free(line);
            longjmp(c->err, 1);
        case 0: /* EOF */
            s_log(LOG_ERR, "fd_getline: Unexpected socket close");
            str_free(line);
            longjmp(c->err, 1);
        }
        if(line[ptr]=='\r')
            continue;
        if(line[ptr]=='\n')
            break;
        if(line[ptr]=='\0')
            break;
        if(++ptr>65536) { /* >64KB --> DoS protection */
            s_log(LOG_ERR, "fd_getline: Line too long");
            str_free(line);
            longjmp(c->err, 1);
        }
    }
    line[ptr]='\0';
    tmpline=str_dup(line);
    safestring(tmpline);
    s_log(LOG_DEBUG, " <- %s", tmpline);
    str_free(tmpline);
    return line;
}
Beispiel #16
0
strp str_dup(const char *s1)
{
  strp s;
  if (IS_RESID(s1))
    return (strp)s1;
  s = str_alloc(strlen(s1) + 1);
  strcpy(s, s1);
  return s;
}
Beispiel #17
0
void RF_destroy(RF_class *RF, WORD compact_threshold) {
	OD_link *link, *next;
	WORD lost_percent;
	ULONG lost_space, entry, nentries;
	RF_class *new_class, *old;
	BYTE *RF_filename, *temp_fn;

	if (RF->touched) {
		lseek(RF->file, 0L, SEEK_SET);
		r_write(RF->file, &RF->hdr, sizeof(RF_file_hdr));
	}

	link = RF->root;
	while (link != NULL) {
		if (link->touched) {
			lseek(RF->file, link->origin, SEEK_SET);
			r_write(RF->file, &link->blk, sizeof(OD_block));
		}

		next = link->next;
		mem_free(link);
		link = next;
	}

	close(RF->file);

	lost_space = RF->hdr.lost_space;
	lost_percent = (WORD) ((lost_space * 100L) / RF->hdr.file_size);
	RF_filename = str_alloc(RF->filename);

	mem_free(RF->filename);
	mem_free(RF);

	if (lost_space && (lost_percent >= compact_threshold)) {
		rename(RF_filename, temp_fn = temp_filename(NULL));
		old = RF_construct(temp_fn, 0);

		new_class = RF_construct(RF_filename, 1);

		new_class->hdr.modify_time = old->hdr.modify_time;
		new_class->hdr.create_time = old->hdr.create_time;
		new_class->touched = 1;

		nentries = RF_entry_count(old);
		for (entry = 0; entry < nentries; entry++) {
			RF_new_entry(new_class, &old->file, RF_header(old, entry),
					RTYP_HOUSECLEAN);
			RF_set_flags(new_class, entry, RF_flags(old, entry));
		}

		RF_destroy(new_class, 0);
		RF_destroy(old, 100);
		remove_tempfile(temp_fn);
	}

	mem_free(RF_filename);
}
Beispiel #18
0
BYTE *str(ULONG value) {
	BYTE temp[64];

	//ultoa(value,temp,10);
	sprintf(temp, "%d", value);
	//snprintf(target_string, size_of_target_string_in_bytes, "%d", source_int);

	return (str_alloc(temp));
}
int main(void)
{
	char * str[COL_COUNT];

	str_alloc(str);
	out_put(str);
	str_free(str);

	return 0;
}
Beispiel #20
0
unsigned addrlist_dup(SOCKADDR_LIST *dst, const SOCKADDR_LIST *src) {
    memcpy(dst, src, sizeof(SOCKADDR_LIST));
    if(src->num) { /* already resolved */
        dst->addr=str_alloc(src->num*sizeof(SOCKADDR_UNION));
        memcpy(dst->addr, src->addr, src->num*sizeof(SOCKADDR_UNION));
    } else { /* delayed resolver */
        addrlist_resolve(dst);
    }
    return dst->num;
}
Beispiel #21
0
void libwrap_init(int num) {
#ifdef USE_PTHREAD
    int i, j, rfd, result;
    char servname[SERVNAME_LEN];

    num_processes=num;
    if(!num_processes) /* no extra processes to spawn */
        return;
    ipc_socket=str_alloc(2*num_processes*sizeof(int));
    busy=str_alloc(num_processes*sizeof(int));
    if(!ipc_socket || !busy) {
        s_log(LOG_ERR, "Memory allocation failed");
        die(1);
    }
    for(i=0; i<num_processes; ++i) { /* spawn a child */
        if(s_socketpair(AF_UNIX, SOCK_STREAM, 0, ipc_socket+2*i, 0, "libwrap_init"))
            die(1);
        switch(fork()) {
        case -1:    /* error */
            ioerror("fork");
            die(1);
        case  0:    /* child */
            drop_privileges(); /* libwrap processes are not chrooted */
            close(0); /* stdin */
            close(1); /* stdout */
            if(!global_options.option.foreground) /* for logging in read_fd */
                close(2); /* stderr */
            for(j=0; j<=i; ++j) /* close parent-side sockets created so far */
                close(ipc_socket[2*j]);
            while(1) { /* main libwrap child loop */
                if(read_fd(ipc_socket[2*i+1], servname, SERVNAME_LEN, &rfd)<=0)
                    _exit(0);
                result=check(servname, rfd);
                write(ipc_socket[2*i+1], (u8 *)&result, sizeof result);
                if(rfd>=0)
                    close(rfd);
            }
        default:    /* parent */
            close(ipc_socket[2*i+1]); /* child-side socket */
        }
    }
#endif /* USE_PTHREAD */
}
Beispiel #22
0
LPSTR tstr2str(const LPTSTR in) {
    LPSTR out;
    int len;

#ifdef UNICODE
    len=WideCharToMultiByte(CP_ACP, 0, in, -1, NULL, 0, NULL, NULL);
    if(!len)
        return NULL;
    out=str_alloc(len+1);
    len=WideCharToMultiByte(CP_ACP, 0, in, -1, out, len, NULL, NULL);
    if(!len)
        return NULL;
#else
    len=strlen(in);
    out=str_alloc(len+1);
    strcpy(out, in);
#endif
    return out;
}
Beispiel #23
0
gtf_file_t* gtf_file_alloc(FILE* file)
{
    gtf_file_t* f = malloc_or_die(sizeof(gtf_file_t));
    f->file   = file;
    f->k      = 0;
    f->state  = STATE_FIELD;
    f->buf    = malloc_or_die(gtf_file_buf_size);
    f->buf[0] = '\0';
    f->quote  = '\0';
    f->c      = f->buf;

    f->field1 = str_alloc();
    f->field2 = str_alloc();

    f->line = 1;
    f->col  = 1;

    return f;
}
Beispiel #24
0
LPTSTR str2tstr(const LPSTR in) {
    LPTSTR out;
    int len;

#ifdef UNICODE
    len=MultiByteToWideChar(CP_ACP, 0, in, -1, NULL, 0);
    if(!len)
        return NULL;
    out=str_alloc((len+1)*sizeof(WCHAR));
    len=MultiByteToWideChar(CP_ACP, 0, in, -1, out, len);
    if(!len)
        return NULL;
#else
    len=strlen(in);
    out=str_alloc(len+1);
    strcpy(out, in);
#endif
    return out;
}
Beispiel #25
0
char *s_ntop(SOCKADDR_UNION *addr, socklen_t addrlen) {
    int err;
    char *host, *port, *retval;

    if(addrlen==sizeof(u_short)) /* see UNIX(7) manual for details */
        return str_dup("unnamed socket");
    host=str_alloc(256);
    port=str_alloc(256); /* needs to be long enough for AF_UNIX path */
    err=getnameinfo(&addr->sa, addrlen,
        host, 256, port, 256, NI_NUMERICHOST|NI_NUMERICSERV);
    if(err) {
        s_log(LOG_ERR, "getnameinfo: %s", s_gai_strerror(err));
        retval=str_dup("unresolvable address");
    } else
        retval=str_printf("%s:%s", host, port);
    str_free(host);
    str_free(port);
    return retval;
}
Beispiel #26
0
/* allocate local data structure for the new thread */
CLI *alloc_client_session(SERVICE_OPTIONS *opt, int rfd, int wfd) {
    CLI *c;

    c=str_alloc(sizeof(CLI));
    str_detach(c);
    c->opt=opt;
    c->local_rfd.fd=rfd;
    c->local_wfd.fd=wfd;
    return c;
}
Beispiel #27
0
/* sipmly swappes the content of the two buffers */
void swap_buffers(char *fst, char *snd) {
	char *tmp;

	if (fst == snd)
		return;
	tmp = str_alloc(strlen(fst)+1);
	strcpy(tmp, fst);
	strcpy(fst, snd);
	strcpy(snd, tmp);
	free(tmp);
}
Beispiel #28
0
/* checks if the strings contains special double marks and then
 * replace all occurences of this strings in the message */
void replace_strings(char *mes, char *strings) {
	char *pos, *atr, *val, *repl, *end;
	char sep;

	pos=atr=val=repl = NULL;
#ifdef DEBUG
	printf("replace_strings entered\nstrings: '%s'\n", strings);
#endif
	if ((isalnum(*strings) != 0) && 
		(isalnum(*(strings + strlen(strings) - 1)) != 0)) {
		replace_string(req, "$replace$", replace_str);
	}
	else {
		sep = *strings;
#ifdef DEBUG
		printf("sep: '%c'\n", sep);
#endif
		end = strings + strlen(strings);
		pos = strings + 1;
		while (pos < end) {
			atr = pos;
			pos = strchr(atr, sep);
			if (pos != NULL) {
				*pos = '\0';
				val = pos + 1;
				pos = strchr(val, sep);
				if (pos != NULL) {
					*pos = '\0';
					pos++;
				}
			}
#ifdef DEBUG
			printf("atr: '%s'\nval: '%s'\n", atr, val);
#endif
			if ((atr != NULL) && (val != NULL)) {
				repl = str_alloc(strlen(val) + 3);
				if (repl == NULL) {
					printf("failed to allocate memory\n");
					exit_code(2);
				}
				sprintf(repl, "$%s$", atr);
				replace_string(mes, repl, val);
				free(repl);
			}
#ifdef DEBUG
			printf("pos: '%s'\n", pos);
#endif
		}
	}
#ifdef DEBUG
	printf("mes:\n'%s'\n", mes);
#endif
}
Beispiel #29
0
CSS_class *CSS_construct(BYTE *string) {
	CSS_class *CSS;

	CSS = (CSS_class*) mem_alloc(sizeof(CSS_class));

	if (string == NULL)
		CSS->string = CSS->cur = str_alloc((BYTE*) "");
	else
		CSS->string = CSS->cur = string;

	return (CSS);
}
Beispiel #30
0
char *sub_arg_in_braces(struct session *ses, char *string, char *result, int flag, int sub)
{
	char *buffer = str_alloc(UMAX(strlen(string), BUFFER_SIZE));

	string = get_arg_in_braces(ses, string, buffer, flag);

	substitute(ses, buffer, result, sub);

	str_free(buffer);

	return string;
}