[BACK]Return to percent_x.c CVS log [TXT][DIR] Up to [local] / src / lib / libwrap

File: [local] / src / lib / libwrap / Attic / percent_x.c (download)

Revision 1.2, Fri Jul 18 23:05:13 2003 UTC (20 years, 10 months ago) by david
Branch: MAIN
CVS Tags: OPENBSD_4_6_BASE, OPENBSD_4_6, OPENBSD_4_5_BASE, OPENBSD_4_5, OPENBSD_4_4_BASE, OPENBSD_4_4, OPENBSD_4_3_BASE, OPENBSD_4_3, OPENBSD_4_2_BASE, OPENBSD_4_2, OPENBSD_4_1_BASE, OPENBSD_4_1, OPENBSD_4_0_BASE, OPENBSD_4_0, OPENBSD_3_9_BASE, OPENBSD_3_9, OPENBSD_3_8_BASE, OPENBSD_3_8, OPENBSD_3_7_BASE, OPENBSD_3_7, OPENBSD_3_6_BASE, OPENBSD_3_6, OPENBSD_3_5_BASE, OPENBSD_3_5, OPENBSD_3_4_BASE, OPENBSD_3_4
Changes since 1.1: +3 -2 lines

add missing includes
ok tedu@

/*	$OpenBSD: percent_x.c,v 1.2 2003/07/18 23:05:13 david Exp $	*/

 /*
  * percent_x() takes a string and performs %<char> expansions. It aborts the
  * program when the expansion would overflow the output buffer. The result
  * of %<char> expansion may be passed on to a shell process. For this
  * reason, characters with a special meaning to shells are replaced by
  * underscores.
  * 
  * Diagnostics are reported through syslog(3).
  * 
  * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
  */

#ifndef lint
#if 0
static char sccsid[] = "@(#) percent_x.c 1.4 94/12/28 17:42:37";
#else
static char rcsid[] = "$OpenBSD: percent_x.c,v 1.2 2003/07/18 23:05:13 david Exp $";
#endif
#endif

/* System libraries. */

#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <string.h>
#include <unistd.h>

/* Local stuff. */

#include "tcpd.h"

/* percent_x - do %<char> expansion, abort if result buffer is too small */

char   *percent_x(result, result_len, string, request)
char   *result;
int     result_len;
char   *string;
struct request_info *request;
{
    char   *bp = result;
    char   *end = result + result_len - 1;	/* end of result buffer */
    char   *expansion;
    int     expansion_len;
    static char ok_chars[] = "1234567890!@%-_=+:,./\
abcdefghijklmnopqrstuvwxyz\
ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    char   *str = string;
    char   *cp;
    int     ch;

    /*
     * Warning: we may be called from a child process or after pattern
     * matching, so we cannot use clean_exit() or tcpd_jump().
     */

    while (*str) {
	if (*str == '%' && (ch = str[1]) != 0) {
	    str += 2;
	    expansion =
		ch == 'a' ? eval_hostaddr(request->client) :
		ch == 'A' ? eval_hostaddr(request->server) :
		ch == 'c' ? eval_client(request) :
		ch == 'd' ? eval_daemon(request) :
		ch == 'h' ? eval_hostinfo(request->client) :
		ch == 'H' ? eval_hostinfo(request->server) :
		ch == 'n' ? eval_hostname(request->client) :
		ch == 'N' ? eval_hostname(request->server) :
		ch == 'p' ? eval_pid(request) :
		ch == 's' ? eval_server(request) :
		ch == 'u' ? eval_user(request) :
		ch == '%' ? "%" : (tcpd_warn("unrecognized %%%c", ch), "");
	    for (cp = expansion; *(cp += strspn(cp, ok_chars)); /* */ )
		*cp = '_';
	    expansion_len = cp - expansion;
	} else {
	    expansion = str++;
	    expansion_len = 1;
	}
	if (bp + expansion_len >= end) {
	    tcpd_warn("percent_x: expansion too long: %.30s...", result);
	    sleep(5);
	    exit(0);
	}
	memcpy(bp, expansion, expansion_len);
	bp += expansion_len;
    }
    *bp = 0;
    return (result);
}