File: migrate_services.pl

package info (click to toggle)
migrationtools 47-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, jessie, jessie-kfreebsd, sid, squeeze, stretch, wheezy
  • size: 340 kB
  • ctags: 99
  • sloc: perl: 1,305; sh: 661; makefile: 16
file content (315 lines) | stat: -rwxr-xr-x 9,990 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/perl -w
#
# $Id: migrate_services.pl,v 1.8 2003/04/15 03:09:34 lukeh Exp $
#
# Copyright (c) 1997-2003 Luke Howard.
# All rights reserved.
#
# Heavily mangled by Bob Apthorpe sometime in June, 2002.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#        This product includes software developed by Luke Howard.
# 4. The name of the other may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE LUKE HOWARD ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL LUKE HOWARD BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# services migration tool
#
#

require 'migrate_common.ph';

$PROGRAM = "migrate_services.pl";
$NAMINGCONTEXT = &getsuffix($PROGRAM);

# keep Perl quiet
$use_stdout = 0;

&parse_args();
&open_files();

my %services = ();
my %portmap = ();
&parse_services(\%services, \%portmap);

my $service_ldif = &build_service_records(\%services, \%portmap);

if ($use_stdout) {
	print STDOUT $service_ldif;
} else {
	print OUTFILE $service_ldif;
}

close(INFILE);
if (OUTFILE ne STDOUT) { close(OUTFILE); }

##### Subroutines #####

sub parse_services
{
	my $Rh_services = shift;
	my $Rh_portmap = shift;

	# A note about $Rh_services:
	# $Rh_services is a reference to a hash of service
	# information. The structure is:
	# 
	# $Rh_services->{$port}{$servicename}{$proto}{'cn'} = $canonicalservicename;
	# $Rh_services->{$port}{$servicename}{$proto}{'aliases'}{$alias} = 1;
	#
	# so @ports = keys(%{$Rh_services});
	# @services_on_a_port = keys(%{$Rh_services->{$port}});
	#
	# Aliases are stored in a hash to keep them normalized, though
	# it's sort of a waste since the aliases are normalized again when
	# protocols are combined while creating records. It's not clear
	# you save any space by storing aliases as a list (allowing multiple
	# identical names to be stored until being normalized away at the
	# end) vs storing them as a hash (storing useless hash values
	# to keep the aliases normalized as keys.) It's also not clear
	# this is even worth worrying about...

	my %svcmap = ();
	my %protocols_found = ();

	my $card = '';
	readloop:
	while(defined($card = <INFILE>))
	{
		next readloop if ($card =~ m/^\s*#/o || $card eq "\n");
		$card =~ s/#.*//o;

		my ($servicename, $portproto, @aliases) = split(m/\s+/o, $card);
		my ($rawport, $proto) = split(m#[/,]#o, $portproto);

		# Find services specifying a port range (e.g. X11.)
		my $loport = '';
		my $hiport = '';
		if ($rawport =~ m#(\d+)-(\d+)#o) {
			$loport = $1;
			$hiport = $2;
		} else {
			$loport = int($rawport);
			$hiport = $loport;
		}
	
		$hiport = $loport if (!defined($hiport) || ($hiport < $loport));

		# Track the number of unique ports used by a service.
		foreach ($loport .. $hiport) {
			$Rh_portmap->{$servicename}{$proto}{$_} = 1;
		}

		my $indivport = '';
		foreach $indivport ($loport .. $hiport) {
			unless (exists($Rh_services->{$indivport}{$servicename}{$proto}{'cn'})) {
				# We've never seen this port/protocol pair
				# before so we take the first occurence of
				# the name as the canonical one, in case
				# we see repeated listings later (see below)

				$svcmap{$indivport}{$proto} = $servicename;
				$Rh_services->{$indivport}{$servicename}{$proto}{'cn'} = $servicename;
				foreach ($servicename, @aliases) {
					$Rh_services->{$indivport}{$servicename}{$proto}{'aliases'}{$_} = 1;
				}
			} else {
				# We've seen this port/protocol pair
				# before so we'll add the service name and
				# any aliases as aliases in the original
				# (canonical) record.

				my $canonical_svc = $svcmap{$indivport}{$proto};
				foreach ($servicename, @aliases) {
					$Rh_services->{$indivport}{$canonical_svc}{$proto}{'aliases'}{$_} = 1;
				}
			}
		}
	}

	return;
}

sub build_service_records
{
	my $Rh_services = shift;
	my $Rh_portmap = shift;

	foreach $port (sort {$a <=> $b} (keys %{$Rh_services})) {
		foreach $servicename (keys %{$Rh_services->{$port}}) {
			my @protocols = (keys %{$Rh_services->{$port}{$servicename}});
			my %tmpaliases = ();

			# Note on the suffix:
			# If a service name applies to a range of
			# ports, add a suffix to the cn and the aliases
			# to ensure unique dn's for each service. The NIS
			# schema that defines ipService (1.3.6.1.1.1.2.3)
			# and ipServicePort (1.3.6.1.1.1.1.15) only
			# allows a single port to be associated with a
			# service name so we have to mangle the cn to
			# differentiate the dn's for each port. This is
			# ugly; the alternative is to change the schema or
			# the format of the services file. "Irresistable
			# Force, meet Immovable Object..."

			my $suffix = '';
			foreach $proto (@protocols) {
				# Only add suffix if it's absolutely necessary
				if (scalar(keys(%{$Rh_portmap->{$servicename}{$proto}})) > 1) {
					$suffix = "+ipServicePort=" . &escape_metacharacters($port);
				}

				# Normalize aliases across protocols. Yet
				# another uncomfortable compromise.
				foreach (keys %{$Rh_services->{$port}{$servicename}{$proto}{'aliases'}}) {
					$tmpaliases{$_} = 1;
				}
			}

			my @aliases = keys(%tmpaliases);
			
			# Finally we build LDIF records for services.
			$svcrecords .= "dn: cn=" . &escape_metacharacters($servicename)
				. $suffix
				. ",$NAMINGCONTEXT\n"
				. "objectClass: ipService\n"
				. "objectClass: top\n"
				. "ipServicePort: $port\n"
				. join('', map { "ipServiceProtocol: $_\n" } (@protocols))
				. join('', map { "cn: $_\n" } (@aliases))
				. "\n";
		}
	}

	return $svcrecords;

}
__END__

=head1 NAME

migrate_services.pl - translate /etc/services into LDIF format for easy migration into LDAP.

=head1 SYNOPSIS

 migrate_services.pl /etc/services /tmp/services.ldif

which produces LDIF entries similar to:

 dn: cn=rtmp,ou=Services,dc=padl,dc=com
 objectClass: ipService
 objectClass: top
 ipServicePort: 1
 ipServiceProtocol: ddp
 cn: rtmp
  
 dn: cn=tcpmux,ou=Services,dc=padl,dc=com
 objectClass: ipService
 objectClass: top
 ipServicePort: 1
 ipServiceProtocol: udp
 ipServiceProtocol: tcp
 cn: tcpmux
  
 dn: cn=nbp,ou=Services,dc=padl,dc=com
 objectClass: ipService
 objectClass: top
 ipServicePort: 2
 ipServiceProtocol: ddp
 cn: nbp
  
 dn: cn=compressnet+ipServicePort=2,ou=Services,dc=padl,dc=com
 objectClass: ipService
 objectClass: top
 ipServicePort: 2
 ipServiceProtocol: udp
 ipServiceProtocol: tcp
 cn: compressnet

 dn: cn=discard,ou=Services,dc=padl,dc=com
 objectClass: ipService
 objectClass: top
 ipServicePort: 9
 ipServiceProtocol: udp
 ipServiceProtocol: tcp
 cn: null
 cn: sink
 cn: Discard
 cn: discard

=head1 USAGE

 migrate_services.pl services_file [ translated_file.ldif ]

=head1 DESCRIPTION

migrate_services.pl parses /etc/services into LDIF format according to the NIS LDAP schema. 

Services spanning a range of ports are uniquely identified by using
multivalued RDNs. Due to a limitation in the NIS schema, there is an
assumed one-to-one association between port and service name (though,
oddly, multiple protocol names are allowed.)

=head1 HOMEPAGE

 Module Home: http://www.padl.com/OSS/MigrationTools.html

=head1 AUTHOR

 Luke Howard
 Heavily mangled by Bob Apthorpe sometime in June, 2002.

=head1 LICENSE

 Copyright (c) 1997-2003 Luke Howard.
 All rights reserved.

 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:

 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the
    documentation and/or other materials provided with the distribution.
 3. All advertising materials mentioning features or use of this software
    must display the following acknowledgement:
        This product includes software developed by Luke Howard.
 4. The name of the other may not be used to endorse or promote products
    derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE LUKE HOWARD ``AS IS'' AND
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 ARE DISCLAIMED.  IN NO EVENT SHALL LUKE HOWARD BE LIABLE
 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.

=cut