]>
git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Quarantine.pm
abcccdb099d114aed2c8b466c0695db458994727
1 package PMG
:: API2
:: Quarantine
;
11 use Mail
:: SpamAssassin
;
14 use PVE
:: Exception
qw(raise_param_exc raise_perm_exc) ;
15 use PVE
:: Tools
qw(extract_param) ;
16 use PVE
:: JSONSchema
qw(get_standard_option) ;
19 use PVE
:: APIServer
:: Formatter
;
22 use PMG
:: AccessControl
;
28 use base
qw(PVE::RESTHandler) ;
32 my $extract_pmail = sub {
33 my ( $authuser, $role ) = @_ ;
35 if ( $authuser =~ m/^(.+)\@quarantine$/ ) {
38 raise_param_exc
({ pmail
=> "got unexpected authuser ' $authuser ' with role ' $role '" });
41 my $verify_optional_pmail = sub {
42 my ( $authuser, $role, $pmail_param ) = @_ ;
45 if ( $role eq 'quser' ) {
46 $pmail = $extract_pmail ->( $authuser, $role );
47 raise_param_exc
({ pmail
=> "parameter not allwed with role ' $role '" })
48 if defined ( $pmail_param ) && ( $pmail ne $pmail_param );
50 raise_param_exc
({ pmail
=> "parameter required with role ' $role '" })
51 if ! defined ( $pmail_param );
52 $pmail = $pmail_param ;
60 my $saversion = Mail
:: SpamAssassin-
> VERSION ;
62 my $salocaldir = "/var/lib/spamassassin/ $saversion/updates_spamassassin_org " ;
64 $spamdesc = PMG
:: Utils
:: load_sa_descriptions
([ $salocaldir ]) if ! $spamdesc ;
68 foreach my $test ( split ( ',' , $info )) {
69 my ( $name, $score ) = split ( ':' , $test );
71 my $info = { name
=> $name, score
=> $score + 0 , desc
=> '-' };
72 if ( my $si = $spamdesc ->{ $name }) {
73 $info ->{ desc
} = $si ->{ desc
};
74 $info ->{ url
} = $si ->{ url
} if defined ( $si ->{ url
});
82 my $extract_email = sub {
85 return $data if ! $data ;
87 if ( $data =~ m/^.*\s(\S+)\s*$/ ) {
91 if ( $data =~ m/^<([^<>\s]+)>$/ ) {
95 if ( $data !~ m/[\s><]/ && $data =~ m/^(.+\@[^\.]+\..*[^\.]+)$/ ) {
104 my $get_real_sender = sub {
107 my @lines = split ( ' \n ' , $ref ->{ header
});
108 my $head = Mail
:: Header-
> new ( \
@lines );
110 my @fromarray = split ( '\s*,\s*' , $head -> get ( 'from' ) || $ref ->{ sender
});
111 my $from = $extract_email ->( $fromarray [ 0 ]) || $ref ->{ sender
};;
112 my $sender = $extract_email ->( $head -> get ( 'sender' ));
114 return $sender if $sender ;
119 my $parse_header_info = sub {
122 my $res = { subject
=> '' , from
=> '' };
124 my @lines = split ( ' \n ' , $ref ->{ header
});
125 my $head = Mail
:: Header-
> new ( \
@lines );
127 $res ->{ subject
} = PMG
:: Utils
:: decode_rfc1522
( PVE
:: Tools
:: trim
( $head -> get ( 'subject' ))) // '' ;
129 my @fromarray = split ( '\s*,\s*' , $head -> get ( 'from' ) || $ref ->{ sender
});
131 $res ->{ from
} = PMG
:: Utils
:: decode_rfc1522
( PVE
:: Tools
:: trim
( $fromarray [ 0 ])) // '' ;
133 my $sender = PMG
:: Utils
:: decode_rfc1522
( PVE
:: Tools
:: trim
( $head -> get ( 'sender' )));
134 $res ->{ sender
} = $sender if $sender && ( $sender ne $res ->{ from
});
136 $res ->{ envelope_sender
} = $ref ->{ sender
};
137 $res ->{ receiver
} = $ref ->{ receiver
} // $ref ->{ pmail
};
138 $res ->{ id
} = 'C' . $ref ->{ cid
} . 'R' . $ref ->{ rid
} . 'T' . $ref ->{ ticketid
};
139 $res ->{ time } = $ref ->{ time };
140 $res ->{ bytes
} = $ref ->{ bytes
};
142 my $qtype = $ref ->{ qtype
};
145 $res ->{ virusname
} = $ref ->{ info
};
146 $res ->{ spamlevel
} = 0 ;
147 } elsif ( $qtype eq 'S' ) {
148 $res ->{ spamlevel
} = $ref ->{ spamlevel
} // 0 ;
154 my $pmail_param_type = {
155 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." ,
156 type
=> 'string' , format
=> 'email' ,
160 __PACKAGE__-
> register_method ({
164 permissions
=> { user
=> 'all' },
165 description
=> "Directory index." ,
167 additionalProperties
=> 0 ,
176 links
=> [ { rel
=> 'child' , href
=> "{name}" } ],
182 { name
=> 'whitelist' },
183 { name
=> 'blacklist' },
184 { name
=> 'content' },
186 { name
=> 'spamusers' },
187 { name
=> 'spamstatus' },
189 { name
=> 'virusstatus' },
190 { name
=> 'quarusers' },
197 my $read_or_modify_user_bw_list = sub {
198 my ( $listname, $param, $addrs, $delete ) = @_ ;
200 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
201 my $authuser = $rpcenv -> get_user ();
202 my $role = $rpcenv -> get_role ();
204 my $pmail = $verify_optional_pmail ->( $authuser, $role, $param ->{ pmail
});
206 my $dbh = PMG
:: DBTools
:: open_ruledb
();
208 my $list = PMG
:: Quarantine
:: add_to_blackwhite
(
209 $dbh, $pmail, $listname, $addrs, $delete );
212 foreach my $a ( @$list ) { push @$res, { address
=> $a }; }
216 __PACKAGE__-
> register_method ({
220 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
221 description
=> "Show user whitelist." ,
223 additionalProperties
=> 0 ,
225 pmail
=> $pmail_param_type,
242 return $read_or_modify_user_bw_list ->( 'WL' , $param );
245 __PACKAGE__-
> register_method ({
246 name
=> 'whitelist_add' ,
249 description
=> "Add user whitelist entries." ,
250 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
253 additionalProperties
=> 0 ,
255 pmail
=> $pmail_param_type,
256 address
=> get_standard_option
( 'pmg-email-address' , {
257 description
=> "The address you want to add." ,
261 returns
=> { type
=> 'null' },
265 $read_or_modify_user_bw_list ->( 'WL' , $param, [ $param ->{ address
} ]);
270 __PACKAGE__-
> register_method ({
271 name
=> 'whitelist_delete' ,
272 path
=> 'whitelist/{address}' ,
274 description
=> "Delete user whitelist entries." ,
275 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
278 additionalProperties
=> 0 ,
280 pmail
=> $pmail_param_type,
281 address
=> get_standard_option
( 'pmg-email-address' , {
282 description
=> "The address you want to remove." ,
286 returns
=> { type
=> 'null' },
290 $read_or_modify_user_bw_list ->( 'WL' , $param, [ $param ->{ address
} ], 1 );
295 __PACKAGE__-
> register_method ({
299 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
300 description
=> "Show user blacklist." ,
302 additionalProperties
=> 0 ,
304 pmail
=> $pmail_param_type,
321 return $read_or_modify_user_bw_list ->( 'BL' , $param );
324 __PACKAGE__-
> register_method ({
325 name
=> 'blacklist_add' ,
328 description
=> "Add user blacklist entries." ,
329 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
332 additionalProperties
=> 0 ,
334 pmail
=> $pmail_param_type,
335 address
=> get_standard_option
( 'pmg-email-address' , {
336 description
=> "The address you want to add." ,
340 returns
=> { type
=> 'null' },
344 $read_or_modify_user_bw_list ->( 'BL' , $param, [ $param ->{ address
} ]);
349 __PACKAGE__-
> register_method ({
350 name
=> 'blacklist_delete' ,
351 path
=> 'blacklist/{address}' ,
353 description
=> "Delete user blacklist entries." ,
354 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
357 additionalProperties
=> 0 ,
359 pmail
=> $pmail_param_type,
360 address
=> get_standard_option
( 'pmg-email-address' , {
361 description
=> "The address you want to remove." ,
365 returns
=> { type
=> 'null' },
369 $read_or_modify_user_bw_list ->( 'BL' , $param, [ $param ->{ address
} ], 1 );
374 __PACKAGE__-
> register_method ({
378 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
379 description
=> "Get a list of receivers of spam in the given timespan (Default the last 24 hours)." ,
381 additionalProperties
=> 0 ,
383 starttime
=> get_standard_option
( 'pmg-starttime' ),
384 endtime
=> get_standard_option
( 'pmg-endtime' ),
393 description
=> 'the receiving email' ,
402 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
403 my $authuser = $rpcenv -> get_user ();
407 my $dbh = PMG
:: DBTools
:: open_ruledb
();
409 my $start = $param ->{ starttime
} // ( time - 86400 );
410 my $end = $param ->{ endtime
} // ( $start + 86400 );
412 my $sth = $dbh -> prepare (
413 "SELECT DISTINCT pmail " .
414 "FROM CMailStore, CMSReceivers WHERE " .
415 "time >= $start AND time < $end AND " .
416 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
417 "AND Status = 'N' ORDER BY pmail" );
421 while ( my $ref = $sth -> fetchrow_hashref ()) {
422 push @$res, { mail
=> $ref ->{ pmail
} };
428 __PACKAGE__-
> register_method ({
429 name
=> 'spamstatus' ,
430 path
=> 'spamstatus' ,
432 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
433 description
=> "Get Spam Quarantine Status" ,
435 additionalProperties
=> 0 ,
442 description
=> 'Number of stored mails.' ,
446 description
=> "Estimated disk space usage in MByte." ,
450 description
=> "Average size of stored mails in bytes." ,
454 description
=> "Average spam level." ,
462 my $dbh = PMG
:: DBTools
:: open_ruledb
();
463 my $ref = PMG
:: DBTools
:: get_quarantine_count
( $dbh, 'S' );
468 __PACKAGE__-
> register_method ({
472 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
473 description
=> "Get a list of users with whitelist/blacklist setttings." ,
475 additionalProperties
=> 0 ,
484 description
=> 'the receiving email' ,
493 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
494 my $authuser = $rpcenv -> get_user ();
498 my $dbh = PMG
:: DBTools
:: open_ruledb
();
500 my $sth = $dbh -> prepare (
501 "SELECT DISTINCT pmail FROM UserPrefs ORDER BY pmail" );
505 while ( my $ref = $sth -> fetchrow_hashref ()) {
506 push @$res, { mail
=> $ref ->{ pmail
} };
512 __PACKAGE__-
> register_method ({
516 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
517 description
=> "Get a list of quarantined spam mails in the given timeframe (default the last 24 hours) for the given user." ,
519 additionalProperties
=> 0 ,
521 starttime
=> get_standard_option
( 'pmg-starttime' ),
522 endtime
=> get_standard_option
( 'pmg-endtime' ),
523 pmail
=> $pmail_param_type,
532 description
=> 'Unique ID' ,
536 description
=> "Size of raw email." ,
540 description
=> "SMTP envelope sender." ,
544 description
=> "Header 'From' field." ,
548 description
=> "Header 'Sender' field." ,
553 description
=> "Receiver email address" ,
557 description
=> "Header 'Subject' field." ,
561 description
=> "Receive time stamp" ,
565 description
=> "Spam score." ,
574 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
575 my $authuser = $rpcenv -> get_user ();
576 my $role = $rpcenv -> get_role ();
578 my $pmail = $verify_optional_pmail ->( $authuser, $role, $param ->{ pmail
});
582 my $dbh = PMG
:: DBTools
:: open_ruledb
();
584 my $start = $param ->{ starttime
} // ( time - 86400 );
585 my $end = $param ->{ endtime
} // ( $start + 86400 );
587 my $sth = $dbh -> prepare (
589 "FROM CMailStore, CMSReceivers WHERE " .
590 "pmail = ? AND time >= $start AND time < $end AND " .
591 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
592 "AND Status = 'N' ORDER BY pmail, time, receiver" );
594 $sth -> execute ( $pmail );
596 while ( my $ref = $sth -> fetchrow_hashref ()) {
597 my $data = $parse_header_info ->( $ref );
604 __PACKAGE__-
> register_method ({
608 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
609 description
=> "Get a list of quarantined virus mails in the given timeframe (default the last 24 hours)." ,
611 additionalProperties
=> 0 ,
613 starttime
=> get_standard_option
( 'pmg-starttime' ),
614 endtime
=> get_standard_option
( 'pmg-endtime' ),
623 description
=> 'Unique ID' ,
627 description
=> "Size of raw email." ,
631 description
=> "SMTP envelope sender." ,
635 description
=> "Header 'From' field." ,
639 description
=> "Header 'Sender' field." ,
644 description
=> "Receiver email address" ,
648 description
=> "Header 'Subject' field." ,
652 description
=> "Receive time stamp" ,
656 description
=> "Virus name." ,
665 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
666 my $authuser = $rpcenv -> get_user ();
670 my $dbh = PMG
:: DBTools
:: open_ruledb
();
672 my $start = $param ->{ starttime
} // ( time - 86400 );
673 my $end = $param ->{ endtime
} // ( $start + 86400 );
675 my $sth = $dbh -> prepare (
677 "FROM CMailStore, CMSReceivers WHERE " .
678 "time >= $start AND time < $end AND " .
679 "QType = 'V' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
680 "AND Status = 'N' ORDER BY time, receiver" );
684 while ( my $ref = $sth -> fetchrow_hashref ()) {
685 my $data = $parse_header_info ->( $ref );
692 __PACKAGE__-
> register_method ({
693 name
=> 'virusstatus' ,
694 path
=> 'virusstatus' ,
696 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
697 description
=> "Get Virus Quarantine Status" ,
699 additionalProperties
=> 0 ,
706 description
=> 'Number of stored mails.' ,
710 description
=> "Estimated disk space usage in MByte." ,
714 description
=> "Average size of stored mails in bytes." ,
722 my $dbh = PMG
:: DBTools
:: open_ruledb
();
723 my $ref = PMG
:: DBTools
:: get_quarantine_count
( $dbh, 'V' );
725 delete $ref ->{ avgspam
};
730 __PACKAGE__-
> register_method ({
734 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
735 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)." ,
737 additionalProperties
=> 0 ,
740 description
=> 'Unique ID' ,
742 pattern
=> 'C\d+R\d+T\d+' ,
746 description
=> "Display 'raw' eml data. Deactivates size limit." ,
757 description
=> 'Unique ID' ,
761 description
=> "Size of raw email." ,
765 description
=> "SMTP envelope sender." ,
769 description
=> "Header 'From' field." ,
773 description
=> "Header 'Sender' field." ,
778 description
=> "Receiver email address" ,
782 description
=> "Header 'Subject' field." ,
786 description
=> "Receive time stamp" ,
790 description
=> "Spam score." ,
794 description
=> "Information about matched spam tests (name, score, desc, url)." ,
798 description
=> "Raw email header data." ,
802 description
=> "Raw email data (first 4096 bytes). Useful for preview. NOTE: The 'htmlmail' formatter displays the whole email." ,
810 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
811 my $authuser = $rpcenv -> get_user ();
812 my $role = $rpcenv -> get_role ();
813 my $format = $rpcenv -> get_format ();
815 my $raw = $param ->{ raw
} // 0 ;
817 my ( $cid, $rid, $tid ) = $param ->{ id
} =~ m/^C(\d+)R(\d+)T(\d+)$/ ;
822 my $dbh = PMG
:: DBTools
:: open_ruledb
();
824 my $ref = PMG
:: DBTools
:: load_mail_data
( $dbh, $cid, $rid, $tid );
826 if ( $role eq 'quser' ) {
827 my $quar_username = $ref ->{ pmail
} . ' @quarantine ' ;
828 raise_perm_exc
( "mail does not belong to user ' $authuser ' ( $ref ->{pmail})" )
829 if $authuser ne $quar_username ;
832 my $res = $parse_header_info ->( $ref );
834 my $filename = $ref ->{ file
};
835 my $spooldir = $PMG :: MailQueue
:: spooldir
;
837 my $path = " $spooldir/$filename " ;
839 if ( $format eq 'htmlmail' ) {
841 my $cfg = PMG
:: Config-
> new ();
842 my $viewimages = $cfg -> get ( 'spamquar' , 'viewimages' );
843 my $allowhref = $cfg -> get ( 'spamquar' , 'allowhrefs' );
845 $res ->{ content
} = PMG
:: HTMLMail
:: email_to_html
( $path, $raw, $viewimages, $allowhref );
847 # to make result verification happy
850 $res ->{ spamlevel
} = 0 ;
851 $res ->{ spaminfo
} = [];
853 # include additional details
855 # we want to get the whole email in raw mode
856 my $maxbytes = (! $raw ) ?
4096 : undef ;
858 my ( $header, $content ) = PMG
:: HTMLMail
:: read_raw_email
( $path, $maxbytes );
860 $res ->{ file
} = $ref ->{ file
};
861 $res ->{ spaminfo
} = decode_spaminfo
( $ref ->{ info
});
862 $res ->{ header
} = $header ;
863 $res ->{ content
} = $content ;
871 PVE
:: APIServer
:: Formatter
:: register_page_formatter
(
872 'format' => 'htmlmail' ,
874 path
=> '/quarantine/content' ,
876 my ( $res, $data, $param, $path, $auth, $config ) = @_ ;
878 if (! HTTP
:: Status
:: is_success
( $res ->{ status
})) {
879 return ( "Error $res ->{status}: $res ->{message}" , "text/plain" );
882 my $ct = "text/html;charset=UTF-8" ;
884 my $raw = $data ->{ content
};
886 return ( encode
( 'UTF-8' , $raw ), $ct, 1 );
889 __PACKAGE__-
> register_method ({
893 description
=> "Execute quarantine actions." ,
894 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'quser' ] },
897 additionalProperties
=> 0 ,
900 description
=> 'Unique IDs, seperate with ;' ,
902 pattern
=> 'C\d+R\d+T\d+(;C\d+R\d+T\d+)*' ,
905 description
=> 'Action - specify what you want to do with the mail.' ,
907 enum
=> [ 'whitelist' , 'blacklist' , 'deliver' , 'delete' ],
911 returns
=> { type
=> "null" },
915 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
916 my $authuser = $rpcenv -> get_user ();
917 my $role = $rpcenv -> get_role ();
918 my $action = $param ->{ action
};
919 my @idlist = split ( ';' , $param ->{ id
});
921 my $dbh = PMG
:: DBTools
:: open_ruledb
();
923 for my $id ( @idlist ) {
924 my ( $cid, $rid, $tid ) = $id =~ m/^C(\d+)R(\d+)T(\d+)$/ ;
929 my $ref = PMG
:: DBTools
:: load_mail_data
( $dbh, $cid, $rid, $tid );
931 if ( $role eq 'quser' ) {
932 my $quar_username = $ref ->{ pmail
} . ' @quarantine ' ;
933 raise_perm_exc
( "mail does not belong to user ' $authuser ' ( $ref ->{pmail})" )
934 if $authuser ne $quar_username ;
937 my $sender = $get_real_sender ->( $ref );
939 if ( $action eq 'whitelist' ) {
940 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $ref ->{ pmail
}, 'WL' , [ $sender ]);
941 } elsif ( $action eq 'blacklist' ) {
942 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $ref ->{ pmail
}, 'BL' , [ $sender ]);
943 } elsif ( $action eq 'deliver' ) {
944 PMG
:: Quarantine
:: deliver_quarantined_mail
( $dbh, $ref, $ref ->{ receiver
} // $ref ->{ pmail
});
945 } elsif ( $action eq 'delete' ) {
946 PMG
:: Quarantine
:: delete_quarantined_mail
( $dbh, $ref );
948 die "internal error" ; # should not be reached