1 /* $NetBSD: percent_x.c,v 1.2 1997/10/09 21:20:41 christos Exp $ */
4 * percent_x() takes a string and performs %<char> expansions. It aborts the
5 * program when the expansion would overflow the output buffer. The result
6 * of %<char> expansion may be passed on to a shell process. For this
7 * reason, characters with a special meaning to shells are replaced by
10 * Diagnostics are reported through syslog(3).
12 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
15 #include <sys/cdefs.h>
18 static char sccsid
[] = "@(#) percent_x.c 1.4 94/12/28 17:42:37";
20 __RCSID("$NetBSD: percent_x.c,v 1.2 1997/10/09 21:20:41 christos Exp $");
24 /* System libraries. */
36 /* percent_x - do %<char> expansion, abort if result buffer is too small */
38 char *percent_x(result
, result_len
, string
, request
)
42 struct request_info
*request
;
45 char *end
= result
+ result_len
- 1; /* end of result buffer */
48 static char ok_chars
[] = "1234567890!@%-_=+:,./\
49 abcdefghijklmnopqrstuvwxyz\
50 ABCDEFGHIJKLMNOPQRSTUVWXYZ";
56 * Warning: we may be called from a child process or after pattern
57 * matching, so we cannot use clean_exit() or tcpd_jump().
61 if (*str
== '%' && (ch
= str
[1]) != 0) {
64 ch
== 'a' ? eval_hostaddr(request
->client
) :
65 ch
== 'A' ? eval_hostaddr(request
->server
) :
66 ch
== 'c' ? eval_client(request
) :
67 ch
== 'd' ? eval_daemon(request
) :
68 ch
== 'h' ? eval_hostinfo(request
->client
) :
69 ch
== 'H' ? eval_hostinfo(request
->server
) :
70 ch
== 'n' ? eval_hostname(request
->client
) :
71 ch
== 'N' ? eval_hostname(request
->server
) :
72 ch
== 'p' ? eval_pid(request
) :
73 ch
== 's' ? eval_server(request
) :
74 ch
== 'u' ? eval_user(request
) :
75 ch
== '%' ? "%" : (tcpd_warn("unrecognized %%%c", ch
), "");
76 for (cp
= expansion
; *(cp
+= strspn(cp
, ok_chars
)); /* */ )
78 expansion_len
= cp
- expansion
;
83 if (bp
+ expansion_len
>= end
) {
84 tcpd_warn("percent_x: expansion too long: %.30s...", result
);
88 memcpy(bp
, expansion
, expansion_len
);