summaryrefslogtreecommitdiffstats
path: root/ldap/admin/src/updatedsgw
blob: d3f65db54bd478b704f752334a2cb2eb52b0d36c (plain)
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!perl
#
# BEGIN COPYRIGHT BLOCK
# Copyright 2001 Sun Microsystems, Inc.
# Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
# All rights reserved.
# END COPYRIGHT BLOCK
#

$nshome = $ENV{'NETSITE_ROOT'};

# If there is no nshome, then assume that we're running 
# as a commandline script.
if (!$nshome ) {
    $script_mode = 1;

# get the commandline options
    if (!getopts('h:i:d:e:s:t:n:') || !$opt_n || !$opt_h ) {
	print "usage: " . $0 . " -n nshome -h current_slapd_host:current_slapd_port [options]\n";
	print "\nrequired:\n";
	print "    -n directory\tthe directory where 7.1 is installed (NS-HOME)\n";
	print "    -h host[:port]\tthe current host and port of the directory server\n";
	print "                  \tto which the gateway connects.\n";
	print "\noptions:\n";
	print "    -i host[:port]\tthe new host and port of the directory server\n";
	print "    -s suffix\t\tthe old suffix of the directory server\n";
	print "    -t suffix\t\tthe new ESCAPED suffix of the directory server\n";
	print "    -d dirmgrdn\t\tthe old manager dn of the directory server\n";
	print "    -e dirmgrdn\t\tthe new manager dn of the directory server\n";
	print "\nexample:\n " . $0 . "  -n /home/servers/ds71/ -h gargoyle:1974 -i brooklyn -s \"dc=example,dc=com\" -t \"o%3Dbrandx.com\" -d \"cn=directory manager\" -e \"cn=directory guru\"\n";
	
	exit;
    }

    # Parse the commandline options
    handle_script_input();
    
} else {
# output cgi header
    print "Content-type: text/plain\n\n";

#    print "Done\n";
# parse the input
    while ( <> ) {
	&parse_input( $_ );
    }
    
    if ( !$vars{'old_host'}) {
	rpt_err( -13, "host"); 
# -13 = null parameter.
#	print "Invalid input for DSGW changer CGI\n\n";
	exit;
    }

#    print "$nshome $vars{'old_host'} $vars{'old_port'}\n";
}

# setup the path separator
$isNT = -d '\\';
$PS = $isNT ? "\\" : "/";

$contextdir = "$nshome"."$PS"."dsgw"."$PS"."context";

#make sure that the target directory exists
if (! -e "$contextdir") {
    rpt_err( -16 ,"$contextdir");
# -16 = not a directory
#    print "$contextdir does not exist\n";
    exit;
}


# cd into NS-HOME/dsgw/context directory
chdir "$contextdir" or die "Unable to cd to $contextdir: $!\n";

# read the files 
    opendir DSGW_CONTEXT, "." or die "$!";
    @dsgwconfs = grep !/^\.\.?$/, readdir DSGW_CONTEXT;
    closedir DSGW_CONTEXT;


# unescape the new and old suffixes
if ($vars{'old_suffix'} && $vars{'new_suffix'}) {
    $escaped_suffix = $vars{'new_suffix'};
    $vars{'new_suffix'} =~ s/%(\w\w)/chr(hex($1))/eg;
    $unescaped_suffix = $vars{'new_suffix'};
#    print "Normal new suffix: $unescaped_suffix\n";
#    print "Escaped new suffix: $escaped_suffix\n";
    $unescaped_oldsuffix = $vars{'old_suffix'};
    $unescaped_oldsuffix =~ s/%(\w\w)/chr(hex($1))/eg;
}

#unescape the digmrs
if ($vars{'new_dirmgr'}){
    $vars{'new_dirmgr'} =~ s/%(\w\w)/chr(hex($1))/eg;
}

if ($vars{'old_dirmgr'}){
    $vars{'old_dirmgr'} =~ s/%(\w\w)/chr(hex($1))/eg;
}


				# 
    $changed = "";
    foreach $file (@dsgwconfs){	

#	print "working on $file\n";
	
	# If it's not a .conf file, skip it.
	if ( $file !~ m/.*?\.conf$/ ) {
#	    print "skipping $file\n\n\n\n";
	    next;	      
	}
	
	$relevant_conf = 0;
	$relevant_suffix = 0;

        # open the old file
	open(OLDFILE, "$file") or die "Cannot read $file. $!\n"; # 
	
        # Need to test to see if this conf file has a baseurl that 
        # matches the old host and port
	for ($line=<OLDFILE>; $line ; $line=<OLDFILE>) { # 	    

	    # If we find the matching baseurl, then set a flag and break out of the loop.
	    if ($line =~ m:^baseurl\s*("){0,1}\s*ldap(s){0,1}\://$vars{'old_host'}(\:$vars{'old_port'}){0,1}/:) { #") 
		# Also check for a matching suffix.
		if ($vars{'old_suffix'} && $vars{'new_suffix'}) {
		    @baseurl = split("/", $line);
		    $curr_suff = $baseurl[3];

		    # Get rid of any double quotes.
		    @baseurl = split("\"", $curr_suff);
		    $curr_suff = $baseurl[0];

		    $unescaped_currsuffix = $curr_suff;
		    $unescaped_currsuffix =~ s/%(\w\w)/chr(hex($1))/eg;

#		    print "curr Suffix: $curr_suff\n";
#		    print "old Suffix: $vars{'old_suffix'}\n";
#		    print "unescaped curr Suffix: $unescaped_currsuffix\n";
#		    print "unescaped old Suffix: $unescaped_oldsuffix\n";

		    if ($unescaped_currsuffix eq $unescaped_oldsuffix) {
			$relevant_suffix = 1;
#			print "suffix match for $file\n";
		    }
		    
		}
		# set a flag
		$relevant_conf = 1;
#		print "host:port match for $file\n";
		last;
	    }


	}
	
	# If there was no match, then go on to the next file.
	if (! $relevant_conf) {
	    close(OLDFILE);
#	    print "no match for $file\n";
	    next;
	}

	# Else, there is a match start over at the beginning of the file
	seek OLDFILE, 0, 0;
	if ($changed eq "") {
		$changed = $file;
	}

	# open the new file
	open(NEWFILE, ">"."tmpcp_"."$file"."1") or die "Cannot write $contextdir$PStmpcp_$file1. $!\n";
    
	# Go through each line, replacing the relevant information
	for ($line=<OLDFILE>; $line ; $line=<OLDFILE>) { # 	    
	
	    # If there is a new host
	    if ($vars{'new_host'}) {
		$line =~ s:^baseurl\s*("){0,1}\s*ldap(s){0,1}\://.*?(\:\d*){0,1}/:baseurl\t$1ldap$2\://$vars{'new_host'}$3/:og; #")
#		print "new host for $file\n";

	    }

	    # a new port
	    if ($vars{'new_port'}) {
		$line =~ s:^baseurl\s*("){0,1}\s*ldap(s){0,1}\://(.*?)(\:\d*){0,1}/:baseurl\t$1ldap$2\://$3\:$vars{'new_port'}/:og; #")
#		print "new port for $file\n";
	    }

	    # new dirmgr
	    if ($vars{'new_dirmgr'} && $vars{'old_dirmgr'}) {
		$line =~ s:(?i)^dirmgr\s*("){0,1}$vars{'old_dirmgr'}("){0,1}:dirmgr\t"$vars{'new_dirmgr'}":g;
#		print "new dirmgr for $file\n";
	    }
	    
	    # new suffix
	    if ($relevant_suffix) {
		$line =~ s:(^baseurl\s*("){0,1}\s*ldap(s){0,1}\://.*?(\:\d*){0,1}/)((.*?("))|(.*?)):$1$escaped_suffix$7:og; # 
	    $line =~ s:^location-suffix.*:location-suffix\t"$unescaped_suffix":og; 
#	    print "new suffix for $file\n";
	    }


	    print NEWFILE $line;

	}
	# 
	close(OLDFILE);
	close(NEWFILE);
	    
	rename "tmpcp_" . "$file"."1", "$file";

    }
rpt_err(0, $changed);


sub parse_input
{
    local( $line ) = @_;
    local($var, $value, $assign );


    foreach $assign ( split( /&/, $line ) ) {
        ( $var, $value ) = split( /=/, $assign );
        $value =~ s/\+/ /g;
        $value =~ s/ /%20/g;
#        $value =~ s/%(\w\w)/chr(hex($1))/eg;
        $var =~ s/\+/ /g;
#        $var =~ s/%(\w\w)/chr(hex($1))/eg;


	$vars{$var} = $value;
    }
}


sub handle_script_input 
{

    if ($opt_h) {
	@temp_array = split(":", $opt_h);

	$vars{'old_host'} = $temp_array[0];
	$vars{'old_port'} = $temp_array[1];

#	print "host: $vars{'old_port'}\n";
#	print "port: $vars{'old_host'}\n";
    }

    if ($opt_i) {
	@temp_array = split(":", $opt_i);

	$vars{'new_host'} = $temp_array[0];
	$vars{'new_port'} = $temp_array[1];

#	print "	$vars{'new_port'}\n";
#	print "	$vars{'new_host'}\n";
    }
    if ($opt_d) {
	$vars{'old_dirmgr'} = $opt_d;
    }
    if ($opt_e) {
	$vars{'new_dirmgr'} = $opt_e;
    }
    if ($opt_s) {
	$vars{'old_suffix'} = $opt_s;
    }
    if ($opt_t) {
	$vars{'new_suffix'} = $opt_t;
    }
    if ($opt_n) {
	$nshome = $opt_n;
    }

}


sub rpt_err
{
    my	$code = shift;
    my	$err_string = shift;

    print "NMC_ErrInfo: " . "$err_string" . "\n";
    print "NMC_STATUS: " . "$code"."\n";

}

sub getopts {
    local($argumentative) = @_;
    local(@args,$_,$first,$rest);
    local($errs) = 0;
    local($[) = 0;

    @args = split( / */, $argumentative );
    while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
        ($first,$rest) = ($1,$2);
        $pos = index($argumentative,$first);
        if($pos >= $[) {
            if($args[$pos+1] eq ':') {
                shift(@ARGV);
                if($rest eq '') {
                    ++$errs unless @ARGV;
                    $rest = shift(@ARGV);
                }
                eval "\$opt_$first = \$rest;";
            }
            else {
                eval "\$opt_$first = 1";
                if($rest eq '') {
                    shift(@ARGV);
                }
                else {
                    $ARGV[0] = "-$rest";
                }
            }
        }
        else {
            print STDERR "Unknown option: $first\n";
            ++$errs;
            if($rest ne '') {
                $ARGV[0] = "-$rest";
            }
            else {
                shift(@ARGV);
            }
        }
    }
    $errs == 0;
}