]>
git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Quarantine.pm
1 package PMG
:: API2
:: Quarantine
;
13 use PVE
:: Exception
qw(raise_param_exc raise_perm_exc) ;
14 use PVE
:: Tools
qw(extract_param) ;
15 use PVE
:: JSONSchema
qw(get_standard_option) ;
18 use PVE
:: APIServer
:: Formatter
;
21 use PMG
:: AccessControl
;
27 use base
qw(PVE::RESTHandler) ;
31 my $verify_optional_pmail = sub {
32 my ( $authuser, $role, $pmail ) = @_ ;
34 if ( $role eq 'quser' ) {
35 raise_param_exc
({ pmail
=> "parameter not allwed with role ' $role '" })
36 if defined ( $pmail ) && ( $pmail ne $authuser );
39 raise_param_exc
({ pmail
=> "parameter required with role ' $role '" })
48 $spamdesc = PMG
:: Utils
:: load_sa_descriptions
() if ! $spamdesc ;
52 foreach my $test ( split ( ',' , $info )) {
53 my ( $name, $score ) = split ( ':' , $test );
55 my $info = { name
=> $name, score
=> $score, desc
=> '-' };
56 if ( my $si = $spamdesc ->{ $name }) {
57 $info ->{ desc
} = $si ->{ desc
};
58 $info ->{ url
} = $si ->{ url
} if defined ( $si ->{ url
});
66 my $extract_email = sub {
69 return $data if ! $data ;
71 if ( $data =~ m/^.*\s(\S+)\s*$/ ) {
75 if ( $data =~ m/^<([^<>\s]+)>$/ ) {
79 if ( $data !~ m/[\s><]/ && $data =~ m/^(.+\@[^\.]+\..*[^\.]+)$/ ) {
88 my $get_real_sender = sub {
91 my @lines = split ( ' \n ' , $ref ->{ header
});
92 my $head = Mail
:: Header-
> new ( \
@lines );
94 my @fromarray = split ( '\s*,\s*' , $head -> get ( 'from' ) || $ref ->{ sender
});
95 my $from = $extract_email ->( $fromarray [ 0 ]) || $ref ->{ sender
};;
96 my $sender = $extract_email ->( $head -> get ( 'sender' ));
98 return $sender if $sender ;
103 my $parse_header_info = sub {
106 my $res = { subject
=> '' , from
=> '' };
108 my @lines = split ( ' \n ' , $ref ->{ header
});
109 my $head = Mail
:: Header-
> new ( \
@lines );
111 $res ->{ subject
} = PMG
:: Utils
:: decode_rfc1522
( PVE
:: Tools
:: trim
( $head -> get ( 'subject' ))) // '' ;
113 my @fromarray = split ( '\s*,\s*' , $head -> get ( 'from' ) || $ref ->{ sender
});
115 $res ->{ from
} = PMG
:: Utils
:: decode_rfc1522
( PVE
:: Tools
:: trim
( $fromarray [ 0 ])) // '' ;
117 my $sender = PMG
:: Utils
:: decode_rfc1522
( PVE
:: Tools
:: trim
( $head -> get ( 'sender' )));
118 $res ->{ sender
} = $sender if $sender && ( $sender ne $res ->{ from
});
120 $res ->{ envelope_sender
} = $ref ->{ sender
};
121 $res ->{ receiver
} = $ref ->{ receiver
} // $ref ->{ pmail
};
122 $res ->{ id
} = 'C' . $ref ->{ cid
} . 'R' . $ref ->{ rid
};
123 $res ->{ time } = $ref ->{ time };
124 $res ->{ bytes
} = $ref ->{ bytes
};
126 my $qtype = $ref ->{ qtype
};
129 $res ->{ virusname
} = $ref ->{ info
};
130 } elsif ( $qtype eq 'S' ) {
131 $res ->{ spamlevel
} = $ref ->{ spamlevel
} // 0 ;
137 my $pmail_param_type = {
138 description
=> "List entries for the user with this primary email address. Quarantine users cannot speficy this parameter, but it is required for all other roles." ,
139 type
=> 'string' , format
=> 'email' ,
143 __PACKAGE__-
> register_method ({
147 permissions
=> { user
=> 'all' },
148 description
=> "Directory index." ,
150 additionalProperties
=> 0 ,
159 links
=> [ { rel
=> 'child' , href
=> "{name}" } ],
165 { name
=> 'whitelist' },
166 { name
=> 'blacklist' },
167 { name
=> 'content' },
169 { name
=> 'spamusers' },
170 { name
=> 'spamstatus' },
172 { name
=> 'virusstatus' },
173 { name
=> 'quarusers' },
180 my $read_or_modify_user_bw_list = sub {
181 my ( $listname, $param, $addrs, $delete ) = @_ ;
183 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
184 my $authuser = $rpcenv -> get_user ();
185 my $role = $rpcenv -> get_role ();
187 my $pmail = $verify_optional_pmail ->( $authuser, $role, $param ->{ pmail
});
189 my $dbh = PMG
:: DBTools
:: open_ruledb
();
191 my $list = PMG
:: Quarantine
:: add_to_blackwhite
(
192 $dbh, $pmail, $listname, $addrs, $delete );
195 foreach my $a ( @$list ) { push @$res, { address
=> $a }; }
199 __PACKAGE__-
> register_method ({
203 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
204 description
=> "Show user whitelist." ,
206 additionalProperties
=> 0 ,
208 pmail
=> $pmail_param_type,
225 return $read_or_modify_user_bw_list ->( 'WL' , $param );
228 __PACKAGE__-
> register_method ({
229 name
=> 'whitelist_add' ,
232 description
=> "Add user whitelist entries." ,
233 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
236 additionalProperties
=> 0 ,
238 pmail
=> $pmail_param_type,
239 address
=> get_standard_option
( 'pmg-email-address' , {
240 description
=> "The address you want to add." ,
244 returns
=> { type
=> 'null' },
248 $read_or_modify_user_bw_list ->( 'WL' , $param, [ $param ->{ address
} ]);
253 __PACKAGE__-
> register_method ({
254 name
=> 'whitelist_delete' ,
255 path
=> 'whitelist/{address}' ,
257 description
=> "Delete user whitelist entries." ,
258 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
261 additionalProperties
=> 0 ,
263 pmail
=> $pmail_param_type,
264 address
=> get_standard_option
( 'pmg-email-address' , {
265 description
=> "The address you want to remove." ,
269 returns
=> { type
=> 'null' },
273 $read_or_modify_user_bw_list ->( 'WL' , $param, [ $param ->{ address
} ], 1 );
278 __PACKAGE__-
> register_method ({
282 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
283 description
=> "Show user blacklist." ,
285 additionalProperties
=> 0 ,
287 pmail
=> $pmail_param_type,
304 return $read_or_modify_user_bw_list ->( 'BL' , $param );
307 __PACKAGE__-
> register_method ({
308 name
=> 'blacklist_add' ,
311 description
=> "Add user blacklist entries." ,
312 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
315 additionalProperties
=> 0 ,
317 pmail
=> $pmail_param_type,
318 address
=> get_standard_option
( 'pmg-email-address' , {
319 description
=> "The address you want to add." ,
323 returns
=> { type
=> 'null' },
327 $read_or_modify_user_bw_list ->( 'BL' , $param, [ $param ->{ address
} ]);
332 __PACKAGE__-
> register_method ({
333 name
=> 'blacklist_delete' ,
334 path
=> 'blacklist/{address}' ,
336 description
=> "Delete user blacklist entries." ,
337 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
340 additionalProperties
=> 0 ,
342 pmail
=> $pmail_param_type,
343 address
=> get_standard_option
( 'pmg-email-address' , {
344 description
=> "The address you want to remove." ,
348 returns
=> { type
=> 'null' },
352 $read_or_modify_user_bw_list ->( 'BL' , $param, [ $param ->{ address
} ], 1 );
357 __PACKAGE__-
> register_method ({
361 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
362 description
=> "Get a list of receivers of spam in the given timespan (Default the last 24 hours)." ,
364 additionalProperties
=> 0 ,
366 starttime
=> get_standard_option
( 'pmg-starttime' ),
367 endtime
=> get_standard_option
( 'pmg-endtime' ),
376 description
=> 'the receiving email' ,
385 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
386 my $authuser = $rpcenv -> get_user ();
390 my $dbh = PMG
:: DBTools
:: open_ruledb
();
392 my $start = $param ->{ starttime
} // ( time - 86400 );
393 my $end = $param ->{ endtime
} // ( $start + 86400 );
395 my $sth = $dbh -> prepare (
396 "SELECT DISTINCT pmail " .
397 "FROM CMailStore, CMSReceivers WHERE " .
398 "time >= $start AND time < $end AND " .
399 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
400 "AND Status = 'N' ORDER BY pmail" );
404 while ( my $ref = $sth -> fetchrow_hashref ()) {
405 push @$res, { mail
=> $ref ->{ pmail
} };
411 __PACKAGE__-
> register_method ({
412 name
=> 'spamstatus' ,
413 path
=> 'spamstatus' ,
415 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
416 description
=> "Get Spam Quarantine Status" ,
418 additionalProperties
=> 0 ,
425 description
=> 'Number of stored mails.' ,
429 description
=> "Estimated disk space usage in MByte." ,
433 description
=> "Average size of stored mails in bytes." ,
437 description
=> "Average spam level." ,
445 my $dbh = PMG
:: DBTools
:: open_ruledb
();
446 my $ref = PMG
:: DBTools
:: get_quarantine_count
( $dbh, 'S' );
451 __PACKAGE__-
> register_method ({
455 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
456 description
=> "Get a list of users with whitelist/blacklist setttings." ,
458 additionalProperties
=> 0 ,
467 description
=> 'the receiving email' ,
476 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
477 my $authuser = $rpcenv -> get_user ();
481 my $dbh = PMG
:: DBTools
:: open_ruledb
();
483 my $sth = $dbh -> prepare (
484 "SELECT DISTINCT pmail FROM UserPrefs ORDER BY pmail" );
488 while ( my $ref = $sth -> fetchrow_hashref ()) {
489 push @$res, { mail
=> $ref ->{ pmail
} };
495 __PACKAGE__-
> register_method ({
499 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
500 description
=> "Get a list of quarantined spam mails in the given timeframe (default the last 24 hours) for the given user." ,
502 additionalProperties
=> 0 ,
504 starttime
=> get_standard_option
( 'pmg-starttime' ),
505 endtime
=> get_standard_option
( 'pmg-endtime' ),
506 pmail
=> $pmail_param_type,
515 description
=> 'Unique ID' ,
519 description
=> "Size of raw email." ,
523 description
=> "SMTP envelope sender." ,
527 description
=> "Header 'From' field." ,
531 description
=> "Header 'Sender' field." ,
536 description
=> "Receiver email address" ,
540 description
=> "Header 'Subject' field." ,
544 description
=> "Receive time stamp" ,
548 description
=> "Spam score." ,
557 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
558 my $authuser = $rpcenv -> get_user ();
559 my $role = $rpcenv -> get_role ();
561 my $pmail = $verify_optional_pmail ->( $authuser, $role, $param ->{ pmail
});
565 my $dbh = PMG
:: DBTools
:: open_ruledb
();
567 my $start = $param ->{ starttime
} // ( time - 86400 );
568 my $end = $param ->{ endtime
} // ( $start + 86400 );
570 my $sth = $dbh -> prepare (
572 "FROM CMailStore, CMSReceivers WHERE " .
573 "pmail = ? AND time >= $start AND time < $end AND " .
574 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
575 "AND Status = 'N' ORDER BY pmail, time, receiver" );
577 $sth -> execute ( $pmail );
579 while ( my $ref = $sth -> fetchrow_hashref ()) {
580 my $data = $parse_header_info ->( $ref );
587 __PACKAGE__-
> register_method ({
591 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
592 description
=> "Get a list of quarantined virus mails in the given timeframe (default the last 24 hours)." ,
594 additionalProperties
=> 0 ,
596 starttime
=> get_standard_option
( 'pmg-starttime' ),
597 endtime
=> get_standard_option
( 'pmg-endtime' ),
606 description
=> 'Unique ID' ,
610 description
=> "Size of raw email." ,
614 description
=> "SMTP envelope sender." ,
618 description
=> "Header 'From' field." ,
622 description
=> "Header 'Sender' field." ,
627 description
=> "Receiver email address" ,
631 description
=> "Header 'Subject' field." ,
635 description
=> "Receive time stamp" ,
639 description
=> "Virus name." ,
648 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
649 my $authuser = $rpcenv -> get_user ();
653 my $dbh = PMG
:: DBTools
:: open_ruledb
();
655 my $start = $param ->{ starttime
} // ( time - 86400 );
656 my $end = $param ->{ endtime
} // ( $start + 86400 );
658 my $sth = $dbh -> prepare (
660 "FROM CMailStore, CMSReceivers WHERE " .
661 "time >= $start AND time < $end AND " .
662 "QType = 'V' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
663 "AND Status = 'N' ORDER BY time, receiver" );
667 while ( my $ref = $sth -> fetchrow_hashref ()) {
668 my $data = $parse_header_info ->( $ref );
675 __PACKAGE__-
> register_method ({
676 name
=> 'virusstatus' ,
677 path
=> 'virusstatus' ,
679 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
680 description
=> "Get Virus Quarantine Status" ,
682 additionalProperties
=> 0 ,
689 description
=> 'Number of stored mails.' ,
693 description
=> "Estimated disk space usage in MByte." ,
697 description
=> "Average size of stored mails in bytes." ,
705 my $dbh = PMG
:: DBTools
:: open_ruledb
();
706 my $ref = PMG
:: DBTools
:: get_quarantine_count
( $dbh, 'V' );
708 delete $ref ->{ avgspam
};
713 __PACKAGE__-
> register_method ({
717 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
718 description
=> "Get email data. There is a special formatter called 'htmlmail' to get sanitized html view of the mail content (use the '/api2/htmlmail/quarantine/content' url)." ,
720 additionalProperties
=> 0 ,
723 description
=> 'Unique ID' ,
725 pattern
=> 'C\d+R\d+' ,
729 description
=> "Display 'raw' eml data. This is only used with the 'htmlmail' formatter." ,
740 description
=> 'Unique ID' ,
744 description
=> "Size of raw email." ,
748 description
=> "SMTP envelope sender." ,
752 description
=> "Header 'From' field." ,
756 description
=> "Header 'Sender' field." ,
761 description
=> "Receiver email address" ,
765 description
=> "Header 'Subject' field." ,
769 description
=> "Receive time stamp" ,
773 description
=> "Spam score." ,
777 description
=> "Information about matched spam tests (name, score, desc, url)." ,
781 description
=> "Raw email header data." ,
785 description
=> "Raw email data (first 4096 bytes). Useful for preview. NOTE: The 'htmlmail' formatter displays the whole email." ,
793 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
794 my $authuser = $rpcenv -> get_user ();
795 my $role = $rpcenv -> get_role ();
796 my $format = $rpcenv -> get_format ();
798 my ( $cid, $rid ) = $param ->{ id
} =~ m/^C(\d+)R(\d+)$/ ;
802 my $dbh = PMG
:: DBTools
:: open_ruledb
();
804 my $ref = PMG
:: DBTools
:: load_mail_data
( $dbh, $cid, $rid );
806 if ( $role eq 'quser' ) {
807 raise_perm_exc
( "mail does not belong to user ' $authuser '" )
808 if $authuser ne $ref ->{ pmail
};
811 my $res = $parse_header_info ->( $ref );
814 my $filename = $ref ->{ file
};
815 my $spooldir = $PMG :: MailQueue
:: spooldir
;
817 my $path = " $spooldir/$filename " ;
819 if ( $format eq 'htmlmail' ) {
821 my $cfg = PMG
:: Config-
> new ();
822 my $viewimages = $cfg -> get ( 'spamquar' , 'viewimages' );
823 my $allowhref = $cfg -> get ( 'spamquar' , 'allowhrefs' );
825 $res ->{ content
} = PMG
:: HTMLMail
:: email_to_html
( $path, $param ->{ raw
}, $viewimages, $allowhref );
827 # to make result verification happy
830 $res ->{ spaminfo
} = [];
832 # include additional details
834 my ( $header, $content ) = PMG
:: HTMLMail
:: read_raw_email
( $path, 4096 );
836 $res ->{ file
} = $ref ->{ file
};
837 $res ->{ spaminfo
} = decode_spaminfo
( $ref ->{ info
});
838 $res ->{ header
} = $header ;
839 $res ->{ content
} = $content ;
847 PVE
:: APIServer
:: Formatter
:: register_page_formatter
(
848 'format' => 'htmlmail' ,
850 path
=> '/quarantine/content' ,
852 my ( $res, $data, $param, $path, $auth, $config ) = @_ ;
854 if (! HTTP
:: Status
:: is_success
( $res ->{ status
})) {
855 return ( "Error $res ->{status}: $res ->{message}" , "text/plain" );
858 my $ct = "text/html;charset=UTF-8" ;
860 my $raw = $data ->{ content
};
862 return ( encode
( 'UTF-8' , $raw ), $ct, 1 );
865 __PACKAGE__-
> register_method ({
869 description
=> "Execute quarantine actions." ,
870 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'quser' ] },
873 additionalProperties
=> 0 ,
876 description
=> 'Unique ID' ,
878 pattern
=> 'C\d+R\d+' ,
882 description
=> 'Action - specify what you want to do with the mail.' ,
884 enum
=> [ 'whitelist' , 'blacklist' , 'deliver' , 'delete' ],
888 returns
=> { type
=> "null" },
892 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
893 my $authuser = $rpcenv -> get_user ();
894 my $role = $rpcenv -> get_role ();
895 my $action = $param ->{ action
};
897 my ( $cid, $rid ) = $param ->{ id
} =~ m/^C(\d+)R(\d+)$/ ;
901 my $dbh = PMG
:: DBTools
:: open_ruledb
();
903 my $ref = PMG
:: DBTools
:: load_mail_data
( $dbh, $cid, $rid );
905 if ( $role eq 'quser' ) {
906 raise_perm_exc
( "mail does not belong to user ' $authuser '" )
907 if $authuser ne $ref ->{ pmail
};
910 my $sender = $get_real_sender ->( $ref );
911 my $username = $ref ->{ pmail
};
913 if ( $action eq 'whitelist' ) {
914 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $username, 'WL' , [ $sender ]);
915 } elsif ( $action eq 'blacklist' ) {
916 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $username, 'BL' , [ $sender ]);
917 } elsif ( $action eq 'deliver' ) {
918 my $targets = [ $ref ->{ pmail
} ];
919 PMG
:: Quarantine
:: deliver_quarantined_mail
( $dbh, $ref, $targets );
920 } elsif ( $action eq 'delete' ) {
921 PMG
:: Quarantine
:: delete_quarantined_mail
( $dbh, $ref );
923 die "internal error" ; # should not be reached