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

Annotation of src/lib/libwrap/percent_x.c, Revision 1.4.66.2

1.4.66.2! riz         1: /*     $NetBSD: percent_x.c,v 1.4.66.1 2012/04/23 16:48:54 riz Exp $   */
1.2       christos    2:
1.1       mrg         3:  /*
                      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
                      8:   * underscores.
1.4       simonb      9:   *
1.1       mrg        10:   * Diagnostics are reported through syslog(3).
1.4       simonb     11:   *
1.1       mrg        12:   * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
                     13:   */
                     14:
1.2       christos   15: #include <sys/cdefs.h>
1.1       mrg        16: #ifndef lint
1.2       christos   17: #if 0
1.1       mrg        18: static char sccsid[] = "@(#) percent_x.c 1.4 94/12/28 17:42:37";
1.2       christos   19: #else
1.4.66.2! riz        20: __RCSID("$NetBSD: percent_x.c,v 1.4.66.1 2012/04/23 16:48:54 riz Exp $");
1.2       christos   21: #endif
1.1       mrg        22: #endif
                     23:
                     24: /* System libraries. */
                     25:
                     26: #include <stdio.h>
                     27: #include <syslog.h>
1.2       christos   28: #include <stdlib.h>
                     29: #include <unistd.h>
1.1       mrg        30: #include <string.h>
                     31:
                     32: /* Local stuff. */
                     33:
                     34: #include "tcpd.h"
                     35:
                     36: /* percent_x - do %<char> expansion, abort if result buffer is too small */
                     37:
1.4.66.2! riz        38: char   *percent_x(result, result_len, string, request)
        !            39: char   *result;
        !            40: int     result_len;
        !            41: char   *string;
        !            42: struct request_info *request;
1.1       mrg        43: {
                     44:     char   *bp = result;
                     45:     char   *end = result + result_len - 1;     /* end of result buffer */
                     46:     char   *expansion;
1.4.66.2! riz        47:     int     expansion_len;
        !            48:     static char ok_chars[] = "1234567890!@%-_=+:,./\
        !            49: abcdefghijklmnopqrstuvwxyz\
        !            50: ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1.1       mrg        51:     char   *str = string;
                     52:     char   *cp;
                     53:     int     ch;
                     54:
                     55:     /*
                     56:      * Warning: we may be called from a child process or after pattern
                     57:      * matching, so we cannot use clean_exit() or tcpd_jump().
                     58:      */
                     59:
                     60:     while (*str) {
                     61:        if (*str == '%' && (ch = str[1]) != 0) {
                     62:            str += 2;
                     63:            expansion =
                     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) :
1.4.66.2! riz        75:                ch == '%' ? "%" : (tcpd_warn("unrecognized %%%c", ch), "");
1.1       mrg        76:            for (cp = expansion; *(cp += strspn(cp, ok_chars)); /* */ )
                     77:                *cp = '_';
                     78:            expansion_len = cp - expansion;
                     79:        } else {
                     80:            expansion = str++;
                     81:            expansion_len = 1;
                     82:        }
                     83:        if (bp + expansion_len >= end) {
                     84:            tcpd_warn("percent_x: expansion too long: %.30s...", result);
                     85:            sleep(5);
                     86:            exit(0);
                     87:        }
                     88:        memcpy(bp, expansion, expansion_len);
                     89:        bp += expansion_len;
                     90:     }
                     91:     *bp = 0;
                     92:     return (result);
                     93: }

CVSweb <webmaster@jp.NetBSD.org>