~ubuntu-branches/ubuntu/hoary/postfix/hoary-security

« back to all changes in this revision

Viewing changes to src/util/host_port.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-10-06 11:50:33 UTC
  • Revision ID: james.westby@ubuntu.com-20041006115033-ooo6yfg6kmoteu04
Tags: upstream-2.1.3
ImportĀ upstreamĀ versionĀ 2.1.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*++
 
2
/* NAME
 
3
/*      host_port 3
 
4
/* SUMMARY
 
5
/*      split string into host and port, destroy string
 
6
/* SYNOPSIS
 
7
/*      #include <host_port.h>
 
8
/*
 
9
/*      const char *host_port(string, host, port, def_service)
 
10
/*      char    *string;
 
11
/*      char    **host;
 
12
/*      char    **port;
 
13
/*      char    *def_service;
 
14
/* DESCRIPTION
 
15
/*      host_port() splits a string into substrings with the host
 
16
/*      name or address, and the service name or port number.
 
17
/*      The input string is modified.
 
18
/*
 
19
/*      The following input formats are understood:
 
20
/*
 
21
/*      [host]:port, [host]:, [host].
 
22
/*
 
23
/*      host:port, host:, host.
 
24
/* DIAGNOSTICS
 
25
/*      The result is a null pointer in case of success.
 
26
/*      In case of problems the result is a string pointer with
 
27
/*      the problem type.
 
28
/* LICENSE
 
29
/* .ad
 
30
/* .fi
 
31
/*      The Secure Mailer license must be distributed with this software.
 
32
/* AUTHOR(S)
 
33
/*      Wietse Venema
 
34
/*      IBM T.J. Watson Research
 
35
/*      P.O. Box 704
 
36
/*      Yorktown Heights, NY 10598, USA
 
37
/*--*/
 
38
 
 
39
/* System library. */
 
40
 
 
41
#include <sys_defs.h>
 
42
#include <string.h>
 
43
#include <ctype.h>
 
44
 
 
45
/* Utility library. */
 
46
 
 
47
#include <msg.h>
 
48
#include <split_at.h>
 
49
#include <stringops.h>
 
50
#include <valid_hostname.h>
 
51
 
 
52
/* Global library. */
 
53
 
 
54
#include <host_port.h>
 
55
 
 
56
/* host_port - parse string into host and port, destroy string */
 
57
 
 
58
const char *host_port(char *buf, char **host, char **port,
 
59
                              char *def_service)
 
60
{
 
61
    char   *cp = buf;
 
62
 
 
63
    /*
 
64
     * [host]:port, [host]:, [host].
 
65
     */
 
66
    if (*cp == '[') {
 
67
        *host = ++cp;
 
68
        if ((cp = split_at(cp, ']')) == 0)
 
69
            return ("missing \"]\"");
 
70
        if (*cp && *cp++ != ':')
 
71
            return ("garbage after \"]\"");
 
72
        if (*cp == 0)
 
73
            *port = def_service;
 
74
        else
 
75
            *port = cp;
 
76
 
 
77
        /*
 
78
         * [host:port], [host:]. These conflict with IPV6 address literals.
 
79
         */
 
80
#if 1
 
81
        if (strchr(*host, ':')) {
 
82
            msg_warn("old-style address form: [%s]", *host);
 
83
            msg_warn("support for [host:port] forms will go away");
 
84
            msg_warn("specify [host]:port instead");
 
85
            return (host_port(buf + 1, host, port, def_service));
 
86
        }
 
87
#endif
 
88
    }
 
89
 
 
90
    /*
 
91
     * host:port, host:, host.
 
92
     */
 
93
    else {
 
94
        *host = cp;
 
95
        if ((cp = split_at_right(cp, ':')) == 0 || *cp == 0)
 
96
            *port = def_service;
 
97
        else
 
98
            *port = cp;
 
99
    }
 
100
 
 
101
    /*
 
102
     * Final sanity checks. We're still sloppy, allowing bare numerical
 
103
     * network addresses instead of requiring proper [ipaddress] forms.
 
104
     */
 
105
    if (!valid_hostname(*host, DONT_GRIPE)
 
106
        && !valid_hostaddr(*host, DONT_GRIPE))
 
107
        return ("valid hostname or network address required");
 
108
    if (ISDIGIT(**port) && !alldig(*port))
 
109
        return ("garbage after numerical service");
 
110
    return (0);
 
111
}
 
112
 
 
113
#ifdef TEST
 
114
 
 
115
#include <vstream.h>
 
116
#include <vstring.h>
 
117
#include <vstring_vstream.h>
 
118
 
 
119
#define STR(x) vstring_str(x)
 
120
 
 
121
int     main(int unused_argc, char **unused_argv)
 
122
{
 
123
    VSTRING *in_buf = vstring_alloc(10);
 
124
    VSTRING *parse_buf = vstring_alloc(10);
 
125
    char   *host;
 
126
    char   *port;
 
127
    const char *err;
 
128
 
 
129
    while (vstring_fgets_nonl(in_buf, VSTREAM_IN)) {
 
130
        vstream_printf(">> %s\n", STR(in_buf));
 
131
        vstream_fflush(VSTREAM_OUT);
 
132
        vstring_strcpy(parse_buf, STR(in_buf));
 
133
        if ((err = host_port(STR(parse_buf), &host, &port, "default-service")) != 0) {
 
134
            msg_warn("%s in %s", err, STR(in_buf));
 
135
        } else {
 
136
            vstream_printf("host %s port %s\n", host, port);
 
137
            vstream_fflush(VSTREAM_OUT);
 
138
        }
 
139
    }
 
140
    vstring_free(in_buf);
 
141
    vstring_free(parse_buf);
 
142
    return (0);
 
143
}
 
144
 
 
145
#endif