forked from virtualmin/virtualmin-registrar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gandi-type-lib.pl
executable file
·532 lines (489 loc) · 14.2 KB
/
gandi-type-lib.pl
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# Functions for talking to gandi.net
use strict;
no strict 'refs';
use warnings;
our (%text);
our $module_name;
my $gandi_api_url_test = "https://api.ote.gandi.net/xmlrpc/";
my $gandi_api_url = "https://api.gandi.net/xmlrpc/";
$@ = undef;
eval "use Frontier::Client";
my $frontier_client_err = $@;
# Returns the name of this registrar
sub type_gandi_desc
{
return $text{'type_gandi'};
}
# Returns an error message if needed dependencies are missing
sub type_gandi_check
{
if ($frontier_client_err) {
my $rv = &text('gandi_eperl', "<tt>Frontier::Client</tt>",
"<tt>".&html_escape($frontier_client_err)."</tt>");
if (&foreign_available("cpan")) {
$rv .= "\n".&text('gandi_cpan',
"../cpan/download.cgi?source=3&cpan=Frontier::Client&".
"return=../$module_name/&".
"returndesc=".&urlize($text{'index_return'}));
}
return $rv;
}
return undef;
}
# type_gandi_domains(&account)
# Returns a list of TLDs that can be used with gandi.net. Hard-coded for
# now, as I don't know of any programatic way to get this.
sub type_gandi_domains
{
return (".asia", ".biz", ".com", ".info", ".mobi", ".name", ".net",
".org", ".pro", ".tel", ".at", ".be", ".cc", ".ch", ".cn", ".co.uk",
".cz", ".de", ".es", ".eu", ".fm", ".fr", ".in", ".it", ".li", ".lu",
".me", ".nu", ".org.uk", ".pl", ".re", ".ru", ".se", ".tv", ".tw",
".us");
}
# type_gandi_edit_inputs(&account, new?)
# Returns table fields for entering the account login details
sub type_gandi_edit_inputs
{
my ($account, $new) = @_;
my $rv;
$rv .= &ui_table_row($text{'gandi_account'},
&ui_textbox("gandi_account", $account->{'gandi_account'}, 30));
$rv .= &ui_table_row($text{'gandi_pass'},
&ui_textbox("gandi_pass", $account->{'gandi_pass'}, 30));
$rv .= &ui_table_row($text{'rcom_years'},
&ui_opt_textbox("gandi_years", $account->{'gandi_years'},
4, $text{'rcom_yearsdef'}));
$rv .= &ui_table_row($text{'rcom_test'},
&ui_radio("gandi_test", int($account->{'gandi_test'}),
[ [ 1, $text{'rcom_test1'} ], [ 0, $text{'rcom_test0'} ] ]));
return $rv;
}
# type_gandi_edit_parse(&account, new?, &in)
# Updates the account object with parsed inputs. Returns undef on success or
# an error message on failure.
sub type_gandi_edit_parse
{
my ($account, $new, $in) = @_;
$in->{'gandi_account'} =~ /^\S+$/ || return $text{'gandi_eaccount'};
$account->{'gandi_account'} = $in->{'gandi_account'};
$in->{'gandi_pass'} =~ /^\S+$/ || return $text{'gandi_epass'};
$account->{'gandi_pass'} = $in->{'gandi_pass'};
if ($in->{'gandi_years_def'}) {
delete($account->{'gandi_years'});
}
else {
$in->{'gandi_years'} =~ /^\d+$/ && $in->{'gandi_years'} > 0 &&
$in->{'gandi_years'} <= 10 || return $text{'rcom_eyears'};
$account->{'gandi_years'} = $in->{'gandi_years'};
}
$account->{'gandi_test'} = $in->{'gandi_test'};
return undef;
}
# type_gandi_renew_years(&account, &domain)
# Returns the number of years by default to renew a domain for
sub type_gandi_renew_years
{
my ($account, $d) = @_;
return $account->{'gandi_years'} || 2;
}
# type_gandi_validate(&account)
# Checks if an account's details are vaid. Returns undef if OK or an error
# message if the login or password are wrong.
sub type_gandi_validate
{
my ($account) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
if ($server) {
return undef;
}
else {
return $sid;
}
}
# type_gandi_check_domain(&account, domain)
# Checks if some domain name is available for registration, returns undef if
# yes, or an error message if not.
sub type_gandi_check_domain
{
my ($account, $dname) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
return &text('gandi_error', $sid) if (!$server);
my $avail;
eval {
$avail = $server->call("domain_available", $sid, [ $dname ]);
};
return &text('gandi_error', "$@") if ($@);
return $avail->{$dname} && $avail->{$dname}->value() ?
undef : $text{'gandi_taken'};
}
# type_gandi_owned_domain(&account, domain, [id])
# Checks if some domain is owned by the given account. If so, returns the
# 1 and the registrar's ID - if not, returns 1 and undef, on failure returns
# 0 and an error message.
sub type_gandi_owned_domain
{
my ($account, $dname, $id) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
return (0, &text('gandi_error', $sid)) if (!$server);
# Get the domain list
my $list;
eval {
$list = $server->call("domain_list", $sid);
};
return (0, &text('gandi_error', "$@")) if ($@);
# Check if on list
foreach my $l (@$list) {
return (1, $l) if ($l eq $dname);
}
return (1, undef);
}
# type_gandi_create_domain(&account, &domain)
# Actually register a domain, if possible. Returns 0 and an error message if
# it failed, or 1 and an ID for the domain on success.
sub type_gandi_create_domain
{
my ($account, $d) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
return (0, &text('gandi_error', $sid)) if (!$server);
# Get the nameservers
my $nss = &get_domain_nameservers($account, $d);
if (!ref($nss)) {
return (0, $nss);
}
elsif (!@$nss) {
return (0, $text{'rcom_ensrecords'});
}
elsif (@$nss < 2) {
return (0, &text('gandi_enstwo', 2, $nss->[0]));
}
# Call to create
my $opid;
eval {
$opid = $server->call("domain_create",
$sid,
$d->{'dom'},
$d->{'registrar_years'} ||
$account->{'gandi_years'} || 1,
$account->{'gandi_account'},
$account->{'gandi_account'},
$account->{'gandi_account'},
$account->{'gandi_account'},
$nss);
};
return (0, &text('gandi_error', "$@")) if ($@);
return (1, $d->{'dom'});
}
# type_gandi_get_nameservers(&account, &domain)
# Returns a array ref list of nameserver hostnames for some domain, or
# an error message on failure.
sub type_gandi_get_nameservers
{
my ($account, $d) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
return &text('gandi_error', $sid) if (!$server);
my $rv;
eval {
$rv = $server->call("domain_ns_list", $sid, $d->{'dom'});
$rv = [ map { $_."" } @$rv ];
};
return $@ ? &text('gandi_error', "$@") : $rv;
}
# type_gandi_set_nameservers(&account, &domain, [&nameservers])
# Updates the nameservers for a domain to match DNS. Returns undef on success
# or an error message on failure.
sub type_gandi_set_nameservers
{
my ($account, $d, $nss) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
return (0, &text('gandi_error', $sid)) if (!$server);
# Get nameservers in DNS
$nss ||= &get_domain_nameservers($account, $d);
if (!ref($nss)) {
return $nss;
}
elsif (!@$nss) {
return $text{'rcom_ensrecords'};
}
elsif (@$nss < 2) {
return (0, &text('gandi_enstwo', 2, $nss->[0]));
}
# Call to set nameservers
eval {
$server->call("domain_ns_set", $sid, $d->{'dom'}, $nss);
};
return $@ ? &text('gandi_error', "$@") : undef;
}
# type_gandi_delete_domain(&account, &domain)
# Deletes a domain previously created with this registrar
sub type_gandi_delete_domain
{
my ($account, $d) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
return (0, &text('gandi_error', $sid)) if (!$server);
# Call to delete
my $opid;
eval {
$opid = $server->call("domain_del", $sid, $d->{'dom'});
};
return (0, &text('gandi_error', "$@")) if ($@);
return (1, $opid);
}
# type_gandi_get_contact(&account, &domain)
# Returns a array containing hashes of domain contact information, or an error
# message if it could not be found.
sub type_gandi_get_contact
{
my ($account, $d) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
return &text('gandi_error', $sid) if (!$server);
# Get the contact IDs and contact details
my $info;
eval {
$info = $server->call("domain_info", $sid, $d->{'dom'});
};
return &text('gandi_error', "$@") if ($@);
my @rv;
foreach my $ct ('admin', 'tech', 'billing') {
next if (!$info->{$ct.'_handle'});
my $con;
eval {
$con = $server->call("contact_info", $sid,
$info->{$ct.'_handle'});
};
if (!$@ && $con) {
$con->{'purpose'} = $ct;
$con->{'handle'} ||= $info->{$ct.'_handle'};
$con->{'name'} = $con->{'company_name'} ||
$con->{'association_name'} ||
$con->{'body_name'};
push(@rv, $con);
}
}
return \@rv;
}
# type_gandi_save_contact(&&account, &domain, &contacts)
# Updates contacts from an array of hashes
sub type_gandi_save_contact
{
my ($account, $d, $cons) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
return &text('gandi_error', $sid) if (!$server);
# Get existing contacts
my $oldcons = &type_gandi_get_contact($account, $d);
return $oldcons if (!ref($oldcons));
# For changed contacts, create a new one and associate with the domain
my %same;
foreach my $c (@$cons) {
my ($oldc) = grep { $_->{'purpose'} eq $c->{'purpose'} } @$oldcons;
my $hash = &contact_hash_to_string($c);
if ($oldc && $hash eq &contact_hash_to_string($oldc)) {
# No change
next;
}
eval {
# Make the new contact
if ($same{$hash}) {
# Re-use same details
$c->{'handle'} = $same{$hash};
}
else {
# Keep all original extra parameters the same
my %params;
my @skip = ( 'id', 'type', 'purpose', 'handle', 'name', 'class', 'firstname', 'lastname', 'address', 'zipcode', 'city', 'country', 'phone', 'email' );
foreach my $k (keys %$c) {
if (&indexof($k, @skip) < 0 &&
$c->{$k}) {
$params{$k} = $c->{$k};
}
}
# Convert name param into appropriate for class
if ($c->{'name'}) {
if ($c->{'class'} eq 'individual') {
die $text{'gandi_eindivname'};
}
$params{$c->{'class'}.'_name'} = $c->{'name'};
}
$c->{'handle'} = $server->call("contact_create", $sid,
$c->{'class'},
$c->{'firstname'},
$c->{'lastname'},
$c->{'address'},
$server->string($c->{'zipcode'}),
$c->{'city'},
$c->{'country'},
$server->string($c->{'phone'}),
$c->{'email'},
\%params);
$same{$hash} = $c->{'handle'};
}
# Update in the domain
if ($c->{'purpose'} eq 'owner') {
$server->call("domain_change_owner", $sid,
$d->{'dom'}, $c->{'handle'});
}
else {
$server->call("domain_change_contact", $sid,
$d->{'dom'}, $c->{'purpose'}, $c->{'handle'});
}
};
return &text('gandi_error', "$@") if ($@);
}
return undef;
}
# type_gandi_get_contact_schema(&account, &domain, type)
# Returns a list of fields for domain contacts, as seen by gandi.net
sub type_gandi_get_contact_schema
{
my ($account, $d, $type) = @_;
return ( { 'name' => 'handle',
'readonly' => 1 },
{ 'name' => 'class',
'choices' => [ [ 'individual', $text{'gandi_individual'} ],
[ 'company', $text{'gandi_company'} ],
[ 'public', $text{'gandi_public'} ],
[ 'association', $text{'gandi_association'} ] ],
'opt' => 0,
},
{ 'name' => 'name',
'size' => 60,
'opt' => 1 },
{ 'name' => 'firstname',
'size' => 40,
'opt' => 0 },
{ 'name' => 'lastname',
'size' => 40,
'opt' => 0 },
{ 'name' => 'address',
'size' => 60,
'opt' => 0 },
{ 'name' => 'city',
'size' => 40,
'opt' => 0 },
{ 'name' => 'state',
'size' => 40,
'opt' => 1 },
{ 'name' => 'zipcode',
'size' => 20,
'opt' => 1 },
{ 'name' => 'country',
'choices' => [ map { [ $_->[1], $_->[0] ] } &list_countries() ],
'opt' => 1 },
{ 'name' => 'email',
'size' => 60,
'opt' => 0 },
{ 'name' => 'phone',
'size' => 40,
'opt' => 0 },
);
}
# type_gandi_get_expiry(&account, &domain)
# Returns either 1 and the expiry time (unix) for a domain, or 0 and an error
# message.
sub type_gandi_get_expiry
{
my ($account, $d) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
return (0, &text('gandi_error', $sid)) if (!$server);
# Call to get info
my $info;
eval {
$info = $server->call("domain_info", $sid, $d->{'dom'});
};
return (0, &text('gandi_error', "$@")) if ($@);
my $expirydate = $info->{'registry_expiration_date'}->value();
if ($expirydate =~ /^(\d{4})(\d\d)(\d\d)T(\d\d):(\d\d):(\d\d)$/) {
return (1, timelocal($6, $5, $4, $3, $2-1, $1-1900));
}
return (0, &text('gandi_eexpiry', $expirydate));
}
# type_gandi_renew_domain(&account, &domain, years)
# Attempts to renew a domain for the specified period. Returns 1 and the
# registrars confirmation code on success, or 0 and an error message on
# failure.
sub type_gandi_renew_domain
{
my ($account, $d, $years) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
return (0, &text('gandi_error', $sid)) if (!$server);
# Call to renew
my $opid;
eval {
$opid = $server->call("domain_renew", $sid, $d->{'dom'}, $years);
};
return (0, &text('gandi_error', "$@")) if ($@);
return (1, $opid);
}
# type_gandi_add_instructions()
# Returns HTML for instructions to be shown on the account adding form, such
# as where to create one.
sub type_gandi_add_instructions
{
return &text('gandi_instructions', 'https://www.gandi.net/resellers/');
}
# type_gandi_transfer_domain(&account, &domain, key)
# Transfer a domain from whatever registrar it is currently hosted with to
# this Gandi account. Returns 1 and an order ID on succes, or 0
# and an error mesasge on failure. If a number of years is given, also renews
# the domain for that period.
sub type_gandi_transfer_domain
{
my ($account, $d, $key) = @_;
my ($server, $sid) = &connect_gandi_api($account, 1);
return (0, &text('gandi_error', $sid)) if (!$server);
# Get my nameservers
my $nss = &get_domain_nameservers($account, $d);
if (!ref($nss)) {
return (0, $nss);
}
elsif (!@$nss) {
return (0, $text{'rcom_ensrecords'});
}
elsif (@$nss < 2) {
return (0, &text('gandi_enstwo', 2, $nss->[0]));
}
# Do the transfer
my $tid;
eval {
$tid = $server->call("domain_transfer_in", $sid, $d->{'dom'},
$account->{'gandi_account'},
$account->{'gandi_account'},
$account->{'gandi_account'},
$account->{'gandi_account'},
$nss,
$key);
};
return (0, &text('gandi_error', "$@")) if ($@);
return (1, $tid);
}
# connect_gandi_api(&account, [return-error])
# Returns a handle connected to the Gandi XML-RPC API and a session ID,
# or calls error
sub connect_gandi_api
{
my ($account, $reterr) = @_;
my $server;
eval {
$server = Frontier::Client->new(
'url' => $account->{'gandi_test'} ? $gandi_api_url_test
: $gandi_api_url,
'debug' => 0);
};
if ($@) {
if ($reterr) {
return (undef, $@);
}
else {
&error($@);
}
}
my $sid;
eval {
my $safe = $server->boolean(0);
$sid = $server->call("login", $account->{'gandi_account'},
$account->{'gandi_pass'}, $safe);
};
return $@ =~ /DataError:\s*(.*)/ ? ( undef, $1 ) :
$@ ? (undef, $@) : ($server, $sid);
}
1;