]>
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 '" })
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' },
177 my $read_or_modify_user_bw_list = sub {
178 my ( $listname, $param, $addrs, $delete ) = @_ ;
180 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
181 my $authuser = $rpcenv -> get_user ();
182 my $role = $rpcenv -> get_role ();
184 my $pmail = $verify_optional_pmail ->( $authuser, $role, $param ->{ pmail
});
186 my $dbh = PMG
:: DBTools
:: open_ruledb
();
188 my $list = PMG
:: Quarantine
:: add_to_blackwhite
(
189 $dbh, $pmail, $listname, $addrs, $delete );
192 foreach my $a ( @$list ) { push @$res, { address
=> $a }; }
196 my $address_pattern = '[a-zA-Z0-9\+\-\_\*\.\@]+' ;
198 __PACKAGE__-
> register_method ({
202 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
203 description
=> "Show user whitelist." ,
205 additionalProperties
=> 0 ,
207 pmail
=> $pmail_param_type,
224 return $read_or_modify_user_bw_list ->( 'WL' , $param );
227 __PACKAGE__-
> register_method ({
228 name
=> 'whitelist_add' ,
231 description
=> "Add user whitelist entries." ,
232 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
235 additionalProperties
=> 0 ,
237 pmail
=> $pmail_param_type,
239 description
=> "The address you want to add." ,
241 pattern
=> $address_pattern,
246 returns
=> { type
=> 'null' },
250 $read_or_modify_user_bw_list ->( 'WL' , $param, [ $param ->{ address
} ]);
255 __PACKAGE__-
> register_method ({
256 name
=> 'whitelist_delete' ,
257 path
=> 'whitelist/{address}' ,
259 description
=> "Delete user whitelist entries." ,
260 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
263 additionalProperties
=> 0 ,
265 pmail
=> $pmail_param_type,
267 description
=> "The address you want to remove." ,
269 pattern
=> $address_pattern,
274 returns
=> { type
=> 'null' },
278 $read_or_modify_user_bw_list ->( 'WL' , $param, [ $param ->{ address
} ], 1 );
283 __PACKAGE__-
> register_method ({
287 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
288 description
=> "Show user blacklist." ,
290 additionalProperties
=> 0 ,
292 pmail
=> $pmail_param_type,
309 return $read_or_modify_user_bw_list ->( 'BL' , $param );
312 __PACKAGE__-
> register_method ({
313 name
=> 'blacklist_add' ,
316 description
=> "Add user blacklist entries." ,
317 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
320 additionalProperties
=> 0 ,
322 pmail
=> $pmail_param_type,
324 description
=> "The address you want to add." ,
326 pattern
=> $address_pattern,
331 returns
=> { type
=> 'null' },
335 $read_or_modify_user_bw_list ->( 'BL' , $param, [ $param ->{ address
} ]);
340 __PACKAGE__-
> register_method ({
341 name
=> 'blacklist_delete' ,
342 path
=> 'blacklist/{address}' ,
344 description
=> "Delete user blacklist entries." ,
345 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
348 additionalProperties
=> 0 ,
350 pmail
=> $pmail_param_type,
352 description
=> "The address you want to remove." ,
354 pattern
=> $address_pattern,
359 returns
=> { type
=> 'null' },
363 $read_or_modify_user_bw_list ->( 'BL' , $param, [ $param ->{ address
} ], 1 );
368 __PACKAGE__-
> register_method ({
372 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
373 description
=> "Get a list of receivers of spam in the given timespan (Default the last 24 hours)." ,
375 additionalProperties
=> 0 ,
378 description
=> "Only consider entries newer than 'starttime' (unix epoch). Default is 'now - 1day'." ,
384 description
=> "Only consider entries older than 'endtime' (unix epoch). This is set to '<start> + 1day' by default." ,
397 description
=> 'the receiving email' ,
406 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
407 my $authuser = $rpcenv -> get_user ();
411 my $dbh = PMG
:: DBTools
:: open_ruledb
();
413 my $start = $param ->{ starttime
} // ( time - 86400 );
414 my $end = $param ->{ endtime
} // ( $start + 86400 );
416 my $sth = $dbh -> prepare (
417 "SELECT DISTINCT pmail " .
418 "FROM CMailStore, CMSReceivers WHERE " .
419 "time >= $start AND time < $end AND " .
420 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
421 "AND Status = 'N' ORDER BY pmail" );
425 while ( my $ref = $sth -> fetchrow_hashref ()) {
426 push @$res, { mail
=> $ref ->{ pmail
} };
432 __PACKAGE__-
> register_method ({
436 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
437 description
=> "Get a list of users with whitelist/blacklist setttings." ,
439 additionalProperties
=> 0 ,
448 description
=> 'the receiving email' ,
457 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
458 my $authuser = $rpcenv -> get_user ();
462 my $dbh = PMG
:: DBTools
:: open_ruledb
();
464 my $sth = $dbh -> prepare (
465 "SELECT DISTINCT pmail FROM UserPrefs ORDER BY pmail" );
469 while ( my $ref = $sth -> fetchrow_hashref ()) {
470 push @$res, { mail
=> $ref ->{ pmail
} };
476 __PACKAGE__-
> register_method ({
480 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
481 description
=> "Get a list of quarantined spam mails in the given timeframe (default the last 24 hours) for the given user." ,
483 additionalProperties
=> 0 ,
486 description
=> "Only consider entries newer than 'starttime' (unix epoch). This is set to 'now - 1day' by default." ,
492 description
=> "Only consider entries older than 'endtime' (unix epoch). This is set to '<start> + 1day' by default." ,
497 pmail
=> $pmail_param_type,
506 description
=> 'Unique ID' ,
510 description
=> "Size of raw email." ,
514 description
=> "SMTP envelope sender." ,
518 description
=> "Header 'From' field." ,
522 description
=> "Header 'Sender' field." ,
527 description
=> "Receiver email address" ,
531 description
=> "Header 'Subject' field." ,
535 description
=> "Receive time stamp" ,
539 description
=> "Spam score." ,
548 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
549 my $authuser = $rpcenv -> get_user ();
550 my $role = $rpcenv -> get_role ();
552 my $pmail = $verify_optional_pmail ->( $authuser, $role, $param ->{ pmail
});
556 my $dbh = PMG
:: DBTools
:: open_ruledb
();
558 my $start = $param ->{ starttime
} // ( time - 86400 );
559 my $end = $param ->{ endtime
} // ( $start + 86400 );
561 my $sth = $dbh -> prepare (
563 "FROM CMailStore, CMSReceivers WHERE " .
564 "pmail = ? AND time >= $start AND time < $end AND " .
565 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
566 "AND Status = 'N' ORDER BY pmail, time, receiver" );
568 $sth -> execute ( $pmail );
570 while ( my $ref = $sth -> fetchrow_hashref ()) {
571 my $data = $parse_header_info ->( $ref );
578 __PACKAGE__-
> register_method ({
582 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
583 description
=> "Get a list of quarantined virus mails in the given timeframe (default the last 24 hours)." ,
585 additionalProperties
=> 0 ,
588 description
=> "Only consider entries newer than 'starttime' (unix epoch). This is set to 'now - 1day' by default." ,
594 description
=> "Only consider entries older than 'endtime' (unix epoch). This is set to '<start> + 1day' by default." ,
607 description
=> 'Unique ID' ,
611 description
=> "Size of raw email." ,
615 description
=> "SMTP envelope sender." ,
619 description
=> "Header 'From' field." ,
623 description
=> "Header 'Sender' field." ,
628 description
=> "Receiver email address" ,
632 description
=> "Header 'Subject' field." ,
636 description
=> "Receive time stamp" ,
640 description
=> "Virus name." ,
649 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
650 my $authuser = $rpcenv -> get_user ();
654 my $dbh = PMG
:: DBTools
:: open_ruledb
();
656 my $start = $param ->{ starttime
} // ( time - 86400 );
657 my $end = $param ->{ endtime
} // ( $start + 86400 );
659 my $sth = $dbh -> prepare (
661 "FROM CMailStore, CMSReceivers WHERE " .
662 "time >= $start AND time < $end AND " .
663 "QType = 'V' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
664 "AND Status = 'N' ORDER BY time, receiver" );
668 while ( my $ref = $sth -> fetchrow_hashref ()) {
669 my $data = $parse_header_info ->( $ref );
676 __PACKAGE__-
> register_method ({
680 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
681 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)." ,
683 additionalProperties
=> 0 ,
686 description
=> 'Unique ID' ,
688 pattern
=> 'C\d+R\d+' ,
692 description
=> "Display 'raw' eml data. This is only used with the 'htmlmail' formatter." ,
703 description
=> 'Unique ID' ,
707 description
=> "Size of raw email." ,
711 description
=> "SMTP envelope sender." ,
715 description
=> "Header 'From' field." ,
719 description
=> "Header 'Sender' field." ,
724 description
=> "Receiver email address" ,
728 description
=> "Header 'Subject' field." ,
732 description
=> "Receive time stamp" ,
736 description
=> "Spam score." ,
740 description
=> "Information about matched spam tests (name, score, desc, url)." ,
744 description
=> "Raw email header data." ,
748 description
=> "Raw email data (first 4096 bytes). Useful for preview. NOTE: The 'htmlmail' formatter displays the whole email." ,
756 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
757 my $authuser = $rpcenv -> get_user ();
758 my $role = $rpcenv -> get_role ();
759 my $format = $rpcenv -> get_format ();
761 my ( $cid, $rid ) = $param ->{ id
} =~ m/^C(\d+)R(\d+)$/ ;
765 my $dbh = PMG
:: DBTools
:: open_ruledb
();
767 my $ref = PMG
:: DBTools
:: load_mail_data
( $dbh, $cid, $rid );
769 if ( $role eq 'quser' ) {
770 raise_perm_exc
( "mail does not belong to user ' $authuser '" )
771 if $authuser ne $ref ->{ pmail
};
774 my $res = $parse_header_info ->( $ref );
777 my $filename = $ref ->{ file
};
778 my $spooldir = $PMG :: MailQueue
:: spooldir
;
780 my $path = " $spooldir/$filename " ;
782 if ( $format eq 'htmlmail' ) {
784 my $cfg = PMG
:: Config-
> new ();
785 my $viewimages = $cfg -> get ( 'spamquar' , 'viewimages' );
786 my $allowhref = $cfg -> get ( 'spamquar' , 'allowhrefs' );
788 $res ->{ content
} = PMG
:: HTMLMail
:: email_to_html
( $path, $param ->{ raw
}, $viewimages, $allowhref );
790 # to make result verification happy
793 $res ->{ spaminfo
} = [];
795 # include additional details
797 my ( $header, $content ) = PMG
:: HTMLMail
:: read_raw_email
( $path, 4096 );
799 $res ->{ file
} = $ref ->{ file
};
800 $res ->{ spaminfo
} = decode_spaminfo
( $ref ->{ info
});
801 $res ->{ header
} = $header ;
802 $res ->{ content
} = $content ;
810 PVE
:: APIServer
:: Formatter
:: register_page_formatter
(
811 'format' => 'htmlmail' ,
813 path
=> '/quarantine/content' ,
815 my ( $res, $data, $param, $path, $auth, $config ) = @_ ;
817 if (! HTTP
:: Status
:: is_success
( $res ->{ status
})) {
818 return ( "Error $res ->{status}: $res ->{message}" , "text/plain" );
821 my $ct = "text/html;charset=UTF-8" ;
823 my $raw = $data ->{ content
};
825 return ( encode
( 'UTF-8' , $raw ), $ct, 1 );
828 __PACKAGE__-
> register_method ({
832 description
=> "Execute quarantine actions." ,
833 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'quser' ] },
836 additionalProperties
=> 0 ,
839 description
=> 'Unique ID' ,
841 pattern
=> 'C\d+R\d+' ,
845 description
=> 'Action - specify what you want to do with the mail.' ,
847 enum
=> [ 'whitelist' , 'blacklist' , 'deliver' , 'delete' ],
851 returns
=> { type
=> "null" },
855 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
856 my $authuser = $rpcenv -> get_user ();
857 my $role = $rpcenv -> get_role ();
858 my $action = $param ->{ action
};
860 my ( $cid, $rid ) = $param ->{ id
} =~ m/^C(\d+)R(\d+)$/ ;
864 my $dbh = PMG
:: DBTools
:: open_ruledb
();
866 my $ref = PMG
:: DBTools
:: load_mail_data
( $dbh, $cid, $rid );
868 if ( $role eq 'quser' ) {
869 raise_perm_exc
( "mail does not belong to user ' $authuser '" )
870 if $authuser ne $ref ->{ pmail
};
873 my $sender = $get_real_sender ->( $ref );
874 my $username = $ref ->{ pmail
};
876 if ( $action eq 'whitelist' ) {
877 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $username, 'WL' , [ $sender ]);
878 } elsif ( $action eq 'blacklist' ) {
879 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $username, 'BL' , [ $sender ]);
880 } elsif ( $action eq 'deliver' ) {
881 my $targets = [ $ref ->{ pmail
} ];
882 PMG
:: Quarantine
:: deliver_quarantined_mail
( $dbh, $ref, $targets );
883 } elsif ( $action eq 'delete' ) {
884 PMG
:: Quarantine
:: delete_quarantined_mail
( $dbh, $ref );
886 die "internal error" ; # should not be reached