]>
git.proxmox.com Git - pmg-api.git/blob - PMG/API2/Quarantine.pm
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 = get_standard_option
( 'pmg-email-address' , {
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." ,
159 __PACKAGE__-
> register_method ({
163 permissions
=> { user
=> 'all' },
164 description
=> "Directory index." ,
166 additionalProperties
=> 0 ,
175 links
=> [ { rel
=> 'child' , href
=> "{name}" } ],
181 { name
=> 'whitelist' },
182 { name
=> 'blacklist' },
183 { name
=> 'content' },
185 { name
=> 'spamusers' },
186 { name
=> 'spamstatus' },
188 { name
=> 'virusstatus' },
189 { name
=> 'quarusers' },
196 my $read_or_modify_user_bw_list = sub {
197 my ( $listname, $param, $addrs, $delete ) = @_ ;
199 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
200 my $authuser = $rpcenv -> get_user ();
201 my $role = $rpcenv -> get_role ();
203 my $pmail = $verify_optional_pmail ->( $authuser, $role, $param ->{ pmail
});
205 my $dbh = PMG
:: DBTools
:: open_ruledb
();
207 my $list = PMG
:: Quarantine
:: add_to_blackwhite
(
208 $dbh, $pmail, $listname, $addrs, $delete );
211 foreach my $a ( @$list ) { push @$res, { address
=> $a }; }
215 __PACKAGE__-
> register_method ({
219 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
220 description
=> "Show user whitelist." ,
222 additionalProperties
=> 0 ,
224 pmail
=> $pmail_param_type,
241 return $read_or_modify_user_bw_list ->( 'WL' , $param );
244 __PACKAGE__-
> register_method ({
245 name
=> 'whitelist_add' ,
248 description
=> "Add user whitelist entries." ,
249 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
252 additionalProperties
=> 0 ,
254 pmail
=> $pmail_param_type,
255 address
=> get_standard_option
( 'pmg-whiteblacklist-entry-list' , {
256 description
=> "The address you want to add." ,
260 returns
=> { type
=> 'null' },
264 my $addresses = [ split ( ',' , $param ->{ address
})];
265 $read_or_modify_user_bw_list ->( 'WL' , $param, $addresses );
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-whiteblacklist-entry-list' , {
282 description
=> "The address you want to remove." ,
286 returns
=> { type
=> 'null' },
290 my $addresses = [ split ( ',' , $param ->{ address
})];
291 $read_or_modify_user_bw_list ->( 'WL' , $param, $addresses, 1 );
296 __PACKAGE__-
> register_method ({
300 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
301 description
=> "Show user blacklist." ,
303 additionalProperties
=> 0 ,
305 pmail
=> $pmail_param_type,
322 return $read_or_modify_user_bw_list ->( 'BL' , $param );
325 __PACKAGE__-
> register_method ({
326 name
=> 'blacklist_add' ,
329 description
=> "Add user blacklist entries." ,
330 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
333 additionalProperties
=> 0 ,
335 pmail
=> $pmail_param_type,
336 address
=> get_standard_option
( 'pmg-whiteblacklist-entry-list' , {
337 description
=> "The address you want to add." ,
341 returns
=> { type
=> 'null' },
345 my $addresses = [ split ( ',' , $param ->{ address
})];
346 $read_or_modify_user_bw_list ->( 'BL' , $param, $addresses );
351 __PACKAGE__-
> register_method ({
352 name
=> 'blacklist_delete' ,
353 path
=> 'blacklist/{address}' ,
355 description
=> "Delete user blacklist entries." ,
356 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
359 additionalProperties
=> 0 ,
361 pmail
=> $pmail_param_type,
362 address
=> get_standard_option
( 'pmg-whiteblacklist-entry-list' , {
363 description
=> "The address you want to remove." ,
367 returns
=> { type
=> 'null' },
371 my $addresses = [ split ( ',' , $param ->{ address
})];
372 $read_or_modify_user_bw_list ->( 'BL' , $param, $addresses, 1 );
377 __PACKAGE__-
> register_method ({
381 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
382 description
=> "Get a list of receivers of spam in the given timespan (Default the last 24 hours)." ,
384 additionalProperties
=> 0 ,
386 starttime
=> get_standard_option
( 'pmg-starttime' ),
387 endtime
=> get_standard_option
( 'pmg-endtime' ),
396 description
=> 'the receiving email' ,
405 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
406 my $authuser = $rpcenv -> get_user ();
410 my $dbh = PMG
:: DBTools
:: open_ruledb
();
412 my $start = $param ->{ starttime
} // ( time - 86400 );
413 my $end = $param ->{ endtime
} // ( $start + 86400 );
415 my $sth = $dbh -> prepare (
416 "SELECT DISTINCT pmail " .
417 "FROM CMailStore, CMSReceivers WHERE " .
418 "time >= $start AND time < $end AND " .
419 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
420 "AND Status = 'N' ORDER BY pmail" );
424 while ( my $ref = $sth -> fetchrow_hashref ()) {
425 push @$res, { mail
=> $ref ->{ pmail
} };
431 __PACKAGE__-
> register_method ({
432 name
=> 'spamstatus' ,
433 path
=> 'spamstatus' ,
435 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
436 description
=> "Get Spam Quarantine Status" ,
438 additionalProperties
=> 0 ,
445 description
=> 'Number of stored mails.' ,
449 description
=> "Estimated disk space usage in MByte." ,
453 description
=> "Average size of stored mails in bytes." ,
457 description
=> "Average spam level." ,
465 my $dbh = PMG
:: DBTools
:: open_ruledb
();
466 my $ref = PMG
:: DBTools
:: get_quarantine_count
( $dbh, 'S' );
471 __PACKAGE__-
> register_method ({
475 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
476 description
=> "Get a list of users with whitelist/blacklist setttings." ,
478 additionalProperties
=> 0 ,
482 description
=> 'If set, limits the result to the given list.' ,
483 enum
=> [ 'BL' , 'WL' ],
494 description
=> 'the receiving email' ,
503 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
504 my $authuser = $rpcenv -> get_user ();
508 my $dbh = PMG
:: DBTools
:: open_ruledb
();
511 if ( $param ->{ list
}) {
512 $sth = $dbh -> prepare ( "SELECT DISTINCT pmail FROM UserPrefs WHERE name = ? ORDER BY pmail" );
513 $sth -> execute ( $param ->{ list
});
515 $sth = $dbh -> prepare ( "SELECT DISTINCT pmail FROM UserPrefs ORDER BY pmail" );
519 while ( my $ref = $sth -> fetchrow_hashref ()) {
520 push @$res, { mail
=> $ref ->{ pmail
} };
526 __PACKAGE__-
> register_method ({
530 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
531 description
=> "Get a list of quarantined spam mails in the given timeframe (default the last 24 hours) for the given user." ,
533 additionalProperties
=> 0 ,
535 starttime
=> get_standard_option
( 'pmg-starttime' ),
536 endtime
=> get_standard_option
( 'pmg-endtime' ),
537 pmail
=> $pmail_param_type,
546 description
=> 'Unique ID' ,
550 description
=> "Size of raw email." ,
554 description
=> "SMTP envelope sender." ,
558 description
=> "Header 'From' field." ,
562 description
=> "Header 'Sender' field." ,
567 description
=> "Receiver email address" ,
571 description
=> "Header 'Subject' field." ,
575 description
=> "Receive time stamp" ,
579 description
=> "Spam score." ,
588 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
589 my $authuser = $rpcenv -> get_user ();
590 my $role = $rpcenv -> get_role ();
592 my $pmail = $verify_optional_pmail ->( $authuser, $role, $param ->{ pmail
});
596 my $dbh = PMG
:: DBTools
:: open_ruledb
();
598 my $start = $param ->{ starttime
} // ( time - 86400 );
599 my $end = $param ->{ endtime
} // ( $start + 86400 );
601 my $sth = $dbh -> prepare (
603 "FROM CMailStore, CMSReceivers WHERE " .
604 "pmail = ? AND time >= $start AND time < $end AND " .
605 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
606 "AND Status = 'N' ORDER BY pmail, time, receiver" );
608 $sth -> execute ( $pmail );
610 while ( my $ref = $sth -> fetchrow_hashref ()) {
611 my $data = $parse_header_info ->( $ref );
618 __PACKAGE__-
> register_method ({
622 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
623 description
=> "Get a list of quarantined virus mails in the given timeframe (default the last 24 hours)." ,
625 additionalProperties
=> 0 ,
627 starttime
=> get_standard_option
( 'pmg-starttime' ),
628 endtime
=> get_standard_option
( 'pmg-endtime' ),
637 description
=> 'Unique ID' ,
641 description
=> "Size of raw email." ,
645 description
=> "SMTP envelope sender." ,
649 description
=> "Header 'From' field." ,
653 description
=> "Header 'Sender' field." ,
658 description
=> "Receiver email address" ,
662 description
=> "Header 'Subject' field." ,
666 description
=> "Receive time stamp" ,
670 description
=> "Virus name." ,
679 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
680 my $authuser = $rpcenv -> get_user ();
684 my $dbh = PMG
:: DBTools
:: open_ruledb
();
686 my $start = $param ->{ starttime
} // ( time - 86400 );
687 my $end = $param ->{ endtime
} // ( $start + 86400 );
689 my $sth = $dbh -> prepare (
691 "FROM CMailStore, CMSReceivers WHERE " .
692 "time >= $start AND time < $end AND " .
693 "QType = 'V' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
694 "AND Status = 'N' ORDER BY time, receiver" );
698 while ( my $ref = $sth -> fetchrow_hashref ()) {
699 my $data = $parse_header_info ->( $ref );
706 __PACKAGE__-
> register_method ({
707 name
=> 'virusstatus' ,
708 path
=> 'virusstatus' ,
710 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
711 description
=> "Get Virus Quarantine Status" ,
713 additionalProperties
=> 0 ,
720 description
=> 'Number of stored mails.' ,
724 description
=> "Estimated disk space usage in MByte." ,
728 description
=> "Average size of stored mails in bytes." ,
736 my $dbh = PMG
:: DBTools
:: open_ruledb
();
737 my $ref = PMG
:: DBTools
:: get_quarantine_count
( $dbh, 'V' );
739 delete $ref ->{ avgspam
};
744 __PACKAGE__-
> register_method ({
748 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
749 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)." ,
751 additionalProperties
=> 0 ,
754 description
=> 'Unique ID' ,
756 pattern
=> 'C\d+R\d+T\d+' ,
760 description
=> "Display 'raw' eml data. Deactivates size limit." ,
771 description
=> 'Unique ID' ,
775 description
=> "Size of raw email." ,
779 description
=> "SMTP envelope sender." ,
783 description
=> "Header 'From' field." ,
787 description
=> "Header 'Sender' field." ,
792 description
=> "Receiver email address" ,
796 description
=> "Header 'Subject' field." ,
800 description
=> "Receive time stamp" ,
804 description
=> "Spam score." ,
808 description
=> "Information about matched spam tests (name, score, desc, url)." ,
812 description
=> "Raw email header data." ,
816 description
=> "Raw email data (first 4096 bytes). Useful for preview. NOTE: The 'htmlmail' formatter displays the whole email." ,
824 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
825 my $authuser = $rpcenv -> get_user ();
826 my $role = $rpcenv -> get_role ();
827 my $format = $rpcenv -> get_format ();
829 my $raw = $param ->{ raw
} // 0 ;
831 my ( $cid, $rid, $tid ) = $param ->{ id
} =~ m/^C(\d+)R(\d+)T(\d+)$/ ;
836 my $dbh = PMG
:: DBTools
:: open_ruledb
();
838 my $ref = PMG
:: DBTools
:: load_mail_data
( $dbh, $cid, $rid, $tid );
840 if ( $role eq 'quser' ) {
841 my $quar_username = $ref ->{ pmail
} . ' @quarantine ' ;
842 raise_perm_exc
( "mail does not belong to user ' $authuser ' ( $ref ->{pmail})" )
843 if $authuser ne $quar_username ;
846 my $res = $parse_header_info ->( $ref );
848 my $filename = $ref ->{ file
};
849 my $spooldir = $PMG :: MailQueue
:: spooldir
;
851 my $path = " $spooldir/$filename " ;
853 if ( $format eq 'htmlmail' ) {
855 my $cfg = PMG
:: Config-
> new ();
856 my $viewimages = $cfg -> get ( 'spamquar' , 'viewimages' );
857 my $allowhref = $cfg -> get ( 'spamquar' , 'allowhrefs' );
859 $res ->{ content
} = PMG
:: HTMLMail
:: email_to_html
( $path, $raw, $viewimages, $allowhref );
861 # to make result verification happy
864 $res ->{ spamlevel
} = 0 ;
865 $res ->{ spaminfo
} = [];
867 # include additional details
869 # we want to get the whole email in raw mode
870 my $maxbytes = (! $raw ) ?
4096 : undef ;
872 my ( $header, $content ) = PMG
:: HTMLMail
:: read_raw_email
( $path, $maxbytes );
874 $res ->{ file
} = $ref ->{ file
};
875 $res ->{ spaminfo
} = decode_spaminfo
( $ref ->{ info
});
876 $res ->{ header
} = $header ;
877 $res ->{ content
} = $content ;
885 PVE
:: APIServer
:: Formatter
:: register_page_formatter
(
886 'format' => 'htmlmail' ,
888 path
=> '/quarantine/content' ,
890 my ( $res, $data, $param, $path, $auth, $config ) = @_ ;
892 if (! HTTP
:: Status
:: is_success
( $res ->{ status
})) {
893 return ( "Error $res ->{status}: $res ->{message}" , "text/plain" );
896 my $ct = "text/html;charset=UTF-8" ;
898 my $raw = $data ->{ content
};
900 return ( encode
( 'UTF-8' , $raw ), $ct, 1 );
903 __PACKAGE__-
> register_method ({
907 description
=> "Execute quarantine actions." ,
908 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'quser' ] },
911 additionalProperties
=> 0 ,
914 description
=> 'Unique IDs, seperate with ;' ,
916 pattern
=> 'C\d+R\d+T\d+(;C\d+R\d+T\d+)*' ,
919 description
=> 'Action - specify what you want to do with the mail.' ,
921 enum
=> [ 'whitelist' , 'blacklist' , 'deliver' , 'delete' ],
925 returns
=> { type
=> "null" },
929 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
930 my $authuser = $rpcenv -> get_user ();
931 my $role = $rpcenv -> get_role ();
932 my $action = $param ->{ action
};
933 my @idlist = split ( ';' , $param ->{ id
});
935 my $dbh = PMG
:: DBTools
:: open_ruledb
();
937 for my $id ( @idlist ) {
938 my ( $cid, $rid, $tid ) = $id =~ m/^C(\d+)R(\d+)T(\d+)$/ ;
943 my $ref = PMG
:: DBTools
:: load_mail_data
( $dbh, $cid, $rid, $tid );
945 if ( $role eq 'quser' ) {
946 my $quar_username = $ref ->{ pmail
} . ' @quarantine ' ;
947 raise_perm_exc
( "mail does not belong to user ' $authuser ' ( $ref ->{pmail})" )
948 if $authuser ne $quar_username ;
951 my $sender = $get_real_sender ->( $ref );
953 if ( $action eq 'whitelist' ) {
954 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $ref ->{ pmail
}, 'WL' , [ $sender ]);
955 } elsif ( $action eq 'blacklist' ) {
956 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $ref ->{ pmail
}, 'BL' , [ $sender ]);
957 } elsif ( $action eq 'deliver' ) {
958 PMG
:: Quarantine
:: deliver_quarantined_mail
( $dbh, $ref, $ref ->{ receiver
} // $ref ->{ pmail
});
959 } elsif ( $action eq 'delete' ) {
960 PMG
:: Quarantine
:: delete_quarantined_mail
( $dbh, $ref );
962 die "internal error" ; # should not be reached