~ubuntu-branches/ubuntu/hardy/openswan/hardy-updates

« back to all changes in this revision

Viewing changes to lib/libopenswan/ttoprotoport.c

  • Committer: Bazaar Package Importer
  • Author(s): Rene Mayrhofer
  • Date: 2005-01-27 16:10:11 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050127161011-idgybmyz3vwhpfiq
Tags: 2.3.0-2
Urgency HIGH due to security issue and problems with build-deps in sarge.
* Fix the security issue. Please see
  http://www.idefense.com/application/poi/display?id=190&
      type=vulnerabilities&flashstatus=false
  for more details. Thanks to Martin Schulze for informing me about
  this issue.
  Closes: #292458: Openswan XAUTH/PAM Buffer Overflow Vulnerability
* Added a Build-Dependency to lynx.
  Closes: #291143: openswan: FTBFS: Missing build dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * conversion from protocol/port string to protocol and port
 
3
 * Copyright (C) 2002 Mario Strasser <mast@gmx.net>,
 
4
 *                    Zuercher Hochschule Winterthur,
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify it
 
7
 * under the terms of the GNU General Public License as published by the
 
8
 * Free Software Foundation; either version 2 of the License, or (at your
 
9
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful, but
 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
13
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
14
 * for more details.
 
15
 *
 
16
 * RCSID $Id: ttoprotoport.c,v 1.5 2004/06/09 00:41:09 mcr Exp $
 
17
 */
 
18
 
 
19
#include "internal.h"
 
20
#include "openswan.h"
 
21
 
 
22
/*
 
23
 * ttoprotoport - converts from protocol/port string to protocol and port
 
24
 */
 
25
err_t
 
26
ttoprotoport(src, src_len, proto, port, has_port_wildcard)
 
27
char *src;              /* input string */
 
28
size_t src_len;         /* length of input string, use strlen() if 0 */
 
29
u_int8_t *proto;        /* extracted protocol number */
 
30
u_int16_t *port;        /* extracted port number if it exists */
 
31
int *has_port_wildcard; /* set if port is %any */
 
32
{
 
33
    char *end, *service_name;
 
34
    char proto_name[16];
 
35
    int proto_len;
 
36
    long int l;
 
37
    struct protoent *protocol;
 
38
    struct servent *service;
 
39
    int  wildcard;
 
40
 
 
41
    /* get the length of the string */
 
42
    if (!src_len) src_len = strlen(src);
 
43
 
 
44
    /* locate delimiter '/' between protocol and port */
 
45
    end = strchr(src, '/');
 
46
    if (end != NULL) {
 
47
      proto_len = end - src;
 
48
      service_name = end + 1;
 
49
    } else {
 
50
      proto_len = src_len;
 
51
      service_name = src + src_len;
 
52
    }
 
53
 
 
54
   /* copy protocol name*/
 
55
    memset(proto_name, '\0', sizeof(proto_name));
 
56
    memcpy(proto_name, src, proto_len);
 
57
 
 
58
    /* extract protocol by trying to resolve it by name */
 
59
    protocol = getprotobyname(proto_name);
 
60
    if (protocol != NULL) {
 
61
        *proto = protocol->p_proto;
 
62
    }
 
63
    else  /* failed, now try it by number */
 
64
    {
 
65
        l = strtol(proto_name, &end, 0);
 
66
 
 
67
        if (*proto_name && *end)
 
68
            return "<protocol> is neither a number nor a valid name";
 
69
 
 
70
        if (l < 0 || l > 0xff)
 
71
            return "<protocol> must be between 0 and 255";
 
72
 
 
73
        *proto = (u_int8_t)l;
 
74
    }
 
75
 
 
76
    /* is there a port wildcard? */
 
77
    wildcard = (strcmp(service_name, "%any") == 0);
 
78
   
 
79
    if(has_port_wildcard) {
 
80
      *has_port_wildcard = wildcard;
 
81
    }
 
82
    
 
83
    if (wildcard) {
 
84
      *port = 0;
 
85
      return NULL;
 
86
    }
 
87
 
 
88
    /* extract port by trying to resolve it by name */
 
89
    service = getservbyname(service_name, NULL);
 
90
    if (service != NULL) {
 
91
        *port = ntohs(service->s_port);
 
92
    }
 
93
    else /* failed, now try it by number */
 
94
    {
 
95
        l = strtol(service_name, &end, 0);
 
96
 
 
97
        if (*service_name && *end)
 
98
            return "<port> is neither a number nor a valid name";
 
99
 
 
100
        if (l < 0 || l > 0xffff)
 
101
            return "<port> must be between 0 and 65535";
 
102
 
 
103
        *port = (u_int16_t)l;
 
104
    }
 
105
    return NULL;
 
106
}
 
107
 
 
108
#ifdef TTOPROTOPORT_MAIN
 
109
 
 
110
#include <stdio.h>
 
111
 
 
112
struct artab;
 
113
static void regress(char *pgm);
 
114
 
 
115
/*
 
116
 - main - convert first argument to hex, or run regression
 
117
 */
 
118
int
 
119
main(int argc, char *argv[])
 
120
{
 
121
        char *pgm = argv[0];
 
122
        const char *oops;
 
123
        u_int8_t proto;
 
124
        u_int16_t port;
 
125
        int has_port_wildcard;
 
126
 
 
127
        if (argc < 2) {
 
128
                fprintf(stderr, "Usage: %s {0x<hex>|0s<base64>|-r}\n", pgm);
 
129
                exit(2);
 
130
        }
 
131
 
 
132
        if (strcmp(argv[1], "-r") == 0) {
 
133
          regress(pgm); /* should not return */
 
134
          fprintf(stderr, "%s: regress() returned?!?\n", pgm);
 
135
          exit(1);
 
136
        }
 
137
 
 
138
        oops = ttoprotoport(argv[1], strlen(argv[1]),
 
139
                            &proto, &port, &has_port_wildcard);
 
140
 
 
141
        if (oops != NULL) {
 
142
                fprintf(stderr, "%s: ttodata error `%s' in `%s'\n", pgm,
 
143
                                                                oops, argv[1]);
 
144
                exit(1);
 
145
        }
 
146
 
 
147
        printf("%s -> %d/%d with %d\n",
 
148
               argv[1], proto, port, has_port_wildcard);
 
149
 
 
150
        exit(0);
 
151
}
 
152
 
 
153
struct artab {
 
154
  char *ascii;          /* NULL for end */
 
155
  int proto, port, wild;
 
156
} atodatatab[] = {
 
157
  /*{ "",               0, 0, -1 }, */
 
158
        { "tcp/%any",   6, 0, 1,  },
 
159
        { NULL,         0, 0, 0, },
 
160
};
 
161
 
 
162
static void                     /* should not return at all, in fact */
 
163
regress(pgm)
 
164
char *pgm;
 
165
{
 
166
        struct artab *r;
 
167
        int status = 0;
 
168
        err_t err;
 
169
 
 
170
        for (r = atodatatab; r->ascii != NULL; r++) {
 
171
          u_int8_t proto;
 
172
          u_int16_t port;
 
173
          int has_port_wildcard;
 
174
          
 
175
          err = ttoprotoport(r->ascii, strlen(r->ascii),
 
176
                             &proto, &port, &has_port_wildcard);
 
177
 
 
178
          if(r->wild == -1) {
 
179
            if(err != NULL) {
 
180
              /* okay, error expected */
 
181
              continue;
 
182
            } else {
 
183
              printf("%s expected error, got none.\n", r->ascii);
 
184
              status = 1;
 
185
              continue;
 
186
            }
 
187
          }
 
188
 
 
189
          if(err) {
 
190
            printf("%s got error: %s\n", r->ascii, err);
 
191
            status = 1;
 
192
            continue;
 
193
          }
 
194
          
 
195
          if(proto != r->proto) {
 
196
            printf("%s expected proto %d, got %d\n",r->ascii, proto, r->proto);
 
197
            status = 1;
 
198
            continue;
 
199
          }
 
200
            
 
201
          if(port != r->port) {
 
202
            printf("%s expected port %d, got %d\n",r->ascii, port, r->port);
 
203
            status = 1;
 
204
            continue;
 
205
          }
 
206
            
 
207
          if(has_port_wildcard != r->wild) {
 
208
            printf("%s expected wild %d, got %d\n",r->ascii,
 
209
                   has_port_wildcard, r->wild);
 
210
            status = 1;
 
211
            continue;
 
212
          }
 
213
            
 
214
          fflush(stdout);
 
215
        }
 
216
        exit(status);
 
217
}
 
218
 
 
219
#endif /* TTODATA_MAIN */