]>
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' , 'quser' ] },
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 ();
408 my $role = $rpcenv -> get_role ();
412 my $dbh = PMG
:: DBTools
:: open_ruledb
();
414 my $start = $param ->{ starttime
} // ( time - 86400 );
415 my $end = $param ->{ endtime
} // ( $start + 86400 );
417 my $sth = $dbh -> prepare (
418 "SELECT DISTINCT pmail " .
419 "FROM CMailStore, CMSReceivers WHERE " .
420 "time >= $start AND time < $end AND " .
421 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
422 "AND Status = 'N' ORDER BY pmail" );
426 while ( my $ref = $sth -> fetchrow_hashref ()) {
427 push @$res, { mail
=> $ref ->{ pmail
} };
433 __PACKAGE__-
> register_method ({
437 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
438 description
=> "Get a list of users with whitelist/blacklist setttings." ,
440 additionalProperties
=> 0 ,
449 description
=> 'the receiving email' ,
458 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
459 my $authuser = $rpcenv -> get_user ();
460 my $role = $rpcenv -> get_role ();
464 my $dbh = PMG
:: DBTools
:: open_ruledb
();
466 my $sth = $dbh -> prepare (
467 "SELECT DISTINCT pmail FROM UserPrefs ORDER BY pmail" );
471 while ( my $ref = $sth -> fetchrow_hashref ()) {
472 push @$res, { mail
=> $ref ->{ pmail
} };
478 __PACKAGE__-
> register_method ({
482 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
483 description
=> "Get a list of quarantined spam mails in the given timeframe (default the last 24 hours) for the given user." ,
485 additionalProperties
=> 0 ,
488 description
=> "Only consider entries newer than 'starttime' (unix epoch). This is set to 'now - 1day' by default." ,
494 description
=> "Only consider entries older than 'endtime' (unix epoch). This is set to '<start> + 1day' by default." ,
499 pmail
=> $pmail_param_type,
508 description
=> 'Unique ID' ,
512 description
=> "Size of raw email." ,
516 description
=> "SMTP envelope sender." ,
520 description
=> "Header 'From' field." ,
524 description
=> "Header 'Sender' field." ,
529 description
=> "Receiver email address" ,
533 description
=> "Header 'Subject' field." ,
537 description
=> "Receive time stamp" ,
541 description
=> "Spam score." ,
550 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
551 my $authuser = $rpcenv -> get_user ();
552 my $role = $rpcenv -> get_role ();
554 my $pmail = $verify_optional_pmail ->( $authuser, $role, $param ->{ pmail
});
558 my $dbh = PMG
:: DBTools
:: open_ruledb
();
560 my $start = $param ->{ starttime
} // ( time - 86400 );
561 my $end = $param ->{ endtime
} // ( $start + 86400 );
563 my $sth = $dbh -> prepare (
565 "FROM CMailStore, CMSReceivers WHERE " .
566 "pmail = ? AND time >= $start AND time < $end AND " .
567 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
568 "AND Status = 'N' ORDER BY pmail, time, receiver" );
570 $sth -> execute ( $pmail );
572 while ( my $ref = $sth -> fetchrow_hashref ()) {
573 my $data = $parse_header_info ->( $ref );
580 __PACKAGE__-
> register_method ({
584 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
585 description
=> "Get a list of quarantined virus mails in the given timeframe (default the last 24 hours)." ,
587 additionalProperties
=> 0 ,
590 description
=> "Only consider entries newer than 'starttime' (unix epoch). This is set to 'now - 1day' by default." ,
596 description
=> "Only consider entries older than 'endtime' (unix epoch). This is set to '<start> + 1day' by default." ,
609 description
=> 'Unique ID' ,
613 description
=> "Size of raw email." ,
617 description
=> "SMTP envelope sender." ,
621 description
=> "Header 'From' field." ,
625 description
=> "Header 'Sender' field." ,
630 description
=> "Receiver email address" ,
634 description
=> "Header 'Subject' field." ,
638 description
=> "Receive time stamp" ,
642 description
=> "Virus name." ,
651 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
652 my $authuser = $rpcenv -> get_user ();
653 my $role = $rpcenv -> get_role ();
658 my $dbh = PMG
:: DBTools
:: open_ruledb
();
660 my $start = $param ->{ starttime
} // ( time - 86400 );
661 my $end = $param ->{ endtime
} // ( $start + 86400 );
663 my $sth = $dbh -> prepare (
665 "FROM CMailStore, CMSReceivers WHERE " .
666 "time >= $start AND time < $end AND " .
667 "QType = 'V' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
668 "AND Status = 'N' ORDER BY time, receiver" );
672 while ( my $ref = $sth -> fetchrow_hashref ()) {
673 my $data = $parse_header_info ->( $ref );
680 __PACKAGE__-
> register_method ({
684 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
685 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)." ,
687 additionalProperties
=> 0 ,
690 description
=> 'Unique ID' ,
692 pattern
=> 'C\d+R\d+' ,
696 description
=> "Display 'raw' eml data. This is only used with the 'htmlmail' formatter." ,
707 description
=> 'Unique ID' ,
711 description
=> "Size of raw email." ,
715 description
=> "SMTP envelope sender." ,
719 description
=> "Header 'From' field." ,
723 description
=> "Header 'Sender' field." ,
728 description
=> "Receiver email address" ,
732 description
=> "Header 'Subject' field." ,
736 description
=> "Receive time stamp" ,
740 description
=> "Spam score." ,
744 description
=> "Information about matched spam tests (name, score, desc, url)." ,
748 description
=> "Raw email header data." ,
752 description
=> "Raw email data (first 4096 bytes). Useful for preview. NOTE: The 'htmlmail' formatter displays the whole email." ,
760 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
761 my $authuser = $rpcenv -> get_user ();
762 my $role = $rpcenv -> get_role ();
763 my $format = $rpcenv -> get_format ();
765 my ( $cid, $rid ) = $param ->{ id
} =~ m/^C(\d+)R(\d+)$/ ;
769 my $dbh = PMG
:: DBTools
:: open_ruledb
();
771 my $ref = PMG
:: DBTools
:: load_mail_data
( $dbh, $cid, $rid );
773 if ( $role eq 'quser' ) {
774 raise_perm_exc
( "mail does not belong to user ' $authuser '" )
775 if $authuser ne $ref ->{ pmail
};
778 my $res = $parse_header_info ->( $ref );
781 my $filename = $ref ->{ file
};
782 my $spooldir = $PMG :: MailQueue
:: spooldir
;
784 my $path = " $spooldir/$filename " ;
786 if ( $format eq 'htmlmail' ) {
788 my $cfg = PMG
:: Config-
> new ();
789 my $viewimages = $cfg -> get ( 'spamquar' , 'viewimages' );
790 my $allowhref = $cfg -> get ( 'spamquar' , 'allowhrefs' );
792 $res ->{ content
} = PMG
:: HTMLMail
:: email_to_html
( $path, $param ->{ raw
}, $viewimages, $allowhref );
794 # to make result verification happy
797 $res ->{ spaminfo
} = [];
799 # include additional details
801 my ( $header, $content ) = PMG
:: HTMLMail
:: read_raw_email
( $path, 4096 );
803 $res ->{ file
} = $ref ->{ file
};
804 $res ->{ spaminfo
} = decode_spaminfo
( $ref ->{ info
});
805 $res ->{ header
} = $header ;
806 $res ->{ content
} = $content ;
814 PVE
:: APIServer
:: Formatter
:: register_page_formatter
(
815 'format' => 'htmlmail' ,
817 path
=> '/quarantine/content' ,
819 my ( $res, $data, $param, $path, $auth, $config ) = @_ ;
821 if (! HTTP
:: Status
:: is_success
( $res ->{ status
})) {
822 return ( "Error $res ->{status}: $res ->{message}" , "text/plain" );
825 my $ct = "text/html;charset=UTF-8" ;
827 my $raw = $data ->{ content
};
829 return ( encode
( 'UTF-8' , $raw ), $ct, 1 );
832 __PACKAGE__-
> register_method ({
836 description
=> "Execute quarantine actions." ,
837 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'quser' ] },
840 additionalProperties
=> 0 ,
843 description
=> 'Unique ID' ,
845 pattern
=> 'C\d+R\d+' ,
849 description
=> 'Action - specify what you want to do with the mail.' ,
851 enum
=> [ 'whitelist' , 'blacklist' , 'deliver' , 'delete' ],
855 returns
=> { type
=> "null" },
859 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
860 my $authuser = $rpcenv -> get_user ();
861 my $role = $rpcenv -> get_role ();
862 my $action = $param ->{ action
};
864 my ( $cid, $rid ) = $param ->{ id
} =~ m/^C(\d+)R(\d+)$/ ;
868 my $dbh = PMG
:: DBTools
:: open_ruledb
();
870 my $ref = PMG
:: DBTools
:: load_mail_data
( $dbh, $cid, $rid );
872 if ( $role eq 'quser' ) {
873 raise_perm_exc
( "mail does not belong to user ' $authuser '" )
874 if $authuser ne $ref ->{ pmail
};
877 my $sender = $get_real_sender ->( $ref );
878 my $username = $ref ->{ pmail
};
880 if ( $action eq 'whitelist' ) {
881 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $username, 'WL' , [ $sender ]);
882 } elsif ( $action eq 'blacklist' ) {
883 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $username, 'BL' , [ $sender ]);
884 } elsif ( $action eq 'deliver' ) {
885 my $targets = [ $ref ->{ pmail
} ];
886 PMG
:: Quarantine
:: deliver_quarantined_mail
( $dbh, $ref, $targets );
887 } elsif ( $action eq 'delete' ) {
888 PMG
:: Quarantine
:: delete_quarantined_mail
( $dbh, $ref );
890 die "internal error" ; # should not be reached