]>
git.proxmox.com Git - pmg-api.git/blob - src/PMG/API2/Quarantine.pm
272dfdb64f1e6dd3ea77247fb5e87ba3f020644b
1 package PMG
:: API2
:: Quarantine
;
13 use Mail
:: SpamAssassin
;
16 use PVE
:: Exception
qw(raise_param_exc raise_perm_exc) ;
17 use PVE
:: Tools
qw(extract_param) ;
18 use PVE
:: JSONSchema
qw(get_standard_option) ;
21 use PVE
:: APIServer
:: Formatter
;
24 use PMG
:: AccessControl
;
32 use base
qw(PVE::RESTHandler) ;
36 my $extract_pmail = sub {
37 my ( $authuser, $role ) = @_ ;
39 if ( $authuser =~ m/^(.+)\@quarantine$/ ) {
42 raise_param_exc
({ pmail
=> "got unexpected authuser ' $authuser ' with role ' $role '" });
45 my $verify_optional_pmail = sub {
46 my ( $authuser, $role, $pmail_param ) = @_ ;
49 if ( $role eq 'quser' ) {
50 $pmail = $extract_pmail ->( $authuser, $role );
51 raise_param_exc
({ pmail
=> "parameter not allwed with role ' $role '" })
52 if defined ( $pmail_param ) && ( $pmail ne $pmail_param );
54 raise_param_exc
({ pmail
=> "parameter required with role ' $role '" })
55 if ! defined ( $pmail_param );
56 $pmail = $pmail_param ;
65 return $res if ! defined ( $info );
67 my $saversion = Mail
:: SpamAssassin-
> VERSION ;
69 my $salocaldir = "/var/lib/spamassassin/ $saversion/updates_spamassassin_org " ;
71 $spamdesc = PMG
:: Utils
:: load_sa_descriptions
([ $salocaldir ]) if ! $spamdesc ;
73 foreach my $test ( split ( ',' , $info )) {
74 my ( $name, $score ) = split ( ':' , $test );
76 my $info = { name
=> $name, score
=> $score + 0 , desc
=> '-' };
77 if ( my $si = $spamdesc ->{ $name }) {
78 $info ->{ desc
} = $si ->{ desc
};
79 $info ->{ url
} = $si ->{ url
} if defined ( $si ->{ url
});
87 my $extract_email = sub {
90 return $data if ! $data ;
92 if ( $data =~ m/^.*\s(\S+)\s*$/ ) {
96 if ( $data =~ m/^<([^<>\s]+)>$/ ) {
100 if ( $data !~ m/[\s><]/ && $data =~ m/^(.+\@[^\.]+\..*[^\.]+)$/ ) {
109 my $get_real_sender = sub {
112 my @lines = split ( ' \n ' , $ref ->{ header
});
113 my $head = Mail
:: Header-
> new ( \
@lines );
115 my @fromarray = split ( '\s*,\s*' , $head -> get ( 'from' ) || $ref ->{ sender
});
116 my $from = $extract_email ->( $fromarray [ 0 ]) || $ref ->{ sender
};;
117 my $sender = $extract_email ->( $head -> get ( 'sender' ));
119 return $sender if $sender ;
124 my $parse_header_info = sub {
127 my $res = { subject
=> '' , from
=> '' };
129 my @lines = split ( ' \n ' , $ref ->{ header
});
130 my $head = Mail
:: Header-
> new ( \
@lines );
132 $res ->{ subject
} = PMG
:: Utils
:: decode_rfc1522
( PVE
:: Tools
:: trim
( $head -> get ( 'subject' ))) // '' ;
134 my @fromarray = split ( '\s*,\s*' , $head -> get ( 'from' ) || $ref ->{ sender
});
136 $res ->{ from
} = PMG
:: Utils
:: decode_rfc1522
( PVE
:: Tools
:: trim
( $fromarray [ 0 ])) // '' ;
138 my $sender = PMG
:: Utils
:: decode_rfc1522
( PVE
:: Tools
:: trim
( $head -> get ( 'sender' )));
139 $res ->{ sender
} = $sender if $sender && ( $sender ne $res ->{ from
});
141 $res ->{ envelope_sender
} = $ref ->{ sender
};
142 $res ->{ receiver
} = $ref ->{ receiver
} // $ref ->{ pmail
};
143 $res ->{ id
} = 'C' . $ref ->{ cid
} . 'R' . $ref ->{ rid
} . 'T' . $ref ->{ ticketid
};
144 $res ->{ time } = $ref ->{ time };
145 $res ->{ bytes
} = $ref ->{ bytes
};
147 my $qtype = $ref ->{ qtype
};
150 $res ->{ virusname
} = $ref ->{ info
};
151 $res ->{ spamlevel
} = 0 ;
152 } elsif ( $qtype eq 'S' ) {
153 $res ->{ spamlevel
} = $ref ->{ spamlevel
} // 0 ;
159 my $pmail_param_type = get_standard_option
( 'pmg-email-address' , {
160 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." ,
164 __PACKAGE__-
> register_method ({
168 permissions
=> { user
=> 'all' },
169 description
=> "Directory index." ,
171 additionalProperties
=> 0 ,
180 links
=> [ { rel
=> 'child' , href
=> "{name}" } ],
186 { name
=> 'whitelist' },
187 { name
=> 'blacklist' },
188 { name
=> 'content' },
190 { name
=> 'spamusers' },
191 { name
=> 'spamstatus' },
193 { name
=> 'virusstatus' },
194 { name
=> 'quarusers' },
195 { name
=> 'attachment' },
196 { name
=> 'listattachments' },
197 { name
=> 'download' },
204 my $read_or_modify_user_bw_list = sub {
205 my ( $listname, $param, $addrs, $delete ) = @_ ;
207 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
208 my $authuser = $rpcenv -> get_user ();
209 my $role = $rpcenv -> get_role ();
211 my $pmail = $verify_optional_pmail ->( $authuser, $role, $param ->{ pmail
});
213 my $dbh = PMG
:: DBTools
:: open_ruledb
();
215 my $list = PMG
:: Quarantine
:: add_to_blackwhite
(
216 $dbh, $pmail, $listname, $addrs, $delete );
219 foreach my $a ( @$list ) { push @$res, { address
=> $a }; }
223 __PACKAGE__-
> register_method ({
227 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
228 description
=> "Show user whitelist." ,
230 additionalProperties
=> 0 ,
232 pmail
=> $pmail_param_type,
249 return $read_or_modify_user_bw_list ->( 'WL' , $param );
252 __PACKAGE__-
> register_method ({
253 name
=> 'whitelist_add' ,
256 description
=> "Add user whitelist entries." ,
257 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
260 additionalProperties
=> 0 ,
262 pmail
=> $pmail_param_type,
263 address
=> get_standard_option
( 'pmg-whiteblacklist-entry-list' , {
264 description
=> "The address you want to add." ,
268 returns
=> { type
=> 'null' },
272 my $addresses = [ split ( ',' , $param ->{ address
})];
273 $read_or_modify_user_bw_list ->( 'WL' , $param, $addresses );
278 __PACKAGE__-
> register_method ({
279 name
=> 'whitelist_delete_base' ,
282 description
=> "Delete user whitelist entries." ,
283 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
286 additionalProperties
=> 0 ,
288 pmail
=> $pmail_param_type,
289 address
=> get_standard_option
( 'pmg-whiteblacklist-entry-list' , {
291 description
=> "The address you want to remove." ,
295 returns
=> { type
=> 'null' },
299 my $addresses = [ split ( ',' , $param ->{ address
})];
300 $read_or_modify_user_bw_list ->( 'WL' , $param, $addresses, 1 );
305 __PACKAGE__-
> register_method ({
306 name
=> 'whitelist_delete' ,
307 path
=> 'whitelist/{address}' ,
309 description
=> "Delete user whitelist entries." ,
310 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
313 additionalProperties
=> 0 ,
315 pmail
=> $pmail_param_type,
316 address
=> get_standard_option
( 'pmg-whiteblacklist-entry-list' , {
317 description
=> "The address you want to remove." ,
321 returns
=> { type
=> 'null' },
325 my $addresses = [ split ( ',' , $param ->{ address
})];
326 $read_or_modify_user_bw_list ->( 'WL' , $param, $addresses, 1 );
331 __PACKAGE__-
> register_method ({
335 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
336 description
=> "Show user blacklist." ,
338 additionalProperties
=> 0 ,
340 pmail
=> $pmail_param_type,
357 return $read_or_modify_user_bw_list ->( 'BL' , $param );
360 __PACKAGE__-
> register_method ({
361 name
=> 'blacklist_add' ,
364 description
=> "Add user blacklist entries." ,
365 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
368 additionalProperties
=> 0 ,
370 pmail
=> $pmail_param_type,
371 address
=> get_standard_option
( 'pmg-whiteblacklist-entry-list' , {
372 description
=> "The address you want to add." ,
376 returns
=> { type
=> 'null' },
380 my $addresses = [ split ( ',' , $param ->{ address
})];
381 $read_or_modify_user_bw_list ->( 'BL' , $param, $addresses );
386 __PACKAGE__-
> register_method ({
387 name
=> 'blacklist_delete_base' ,
390 description
=> "Delete user blacklist entries." ,
391 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
394 additionalProperties
=> 0 ,
396 pmail
=> $pmail_param_type,
397 address
=> get_standard_option
( 'pmg-whiteblacklist-entry-list' , {
399 description
=> "The address you want to remove." ,
403 returns
=> { type
=> 'null' },
407 my $addresses = [ split ( ',' , $param ->{ address
})];
408 $read_or_modify_user_bw_list ->( 'BL' , $param, $addresses, 1 );
413 __PACKAGE__-
> register_method ({
414 name
=> 'blacklist_delete' ,
415 path
=> 'blacklist/{address}' ,
417 description
=> "Delete user blacklist entries." ,
418 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
421 additionalProperties
=> 0 ,
423 pmail
=> $pmail_param_type,
424 address
=> get_standard_option
( 'pmg-whiteblacklist-entry-list' , {
425 description
=> "The address you want to remove." ,
429 returns
=> { type
=> 'null' },
433 my $addresses = [ split ( ',' , $param ->{ address
})];
434 $read_or_modify_user_bw_list ->( 'BL' , $param, $addresses, 1 );
439 __PACKAGE__-
> register_method ({
443 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
444 description
=> "Get a list of receivers of spam in the given timespan (Default the last 24 hours)." ,
446 additionalProperties
=> 0 ,
448 starttime
=> get_standard_option
( 'pmg-starttime' ),
449 endtime
=> get_standard_option
( 'pmg-endtime' ),
458 description
=> 'the receiving email' ,
467 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
468 my $authuser = $rpcenv -> get_user ();
472 my $dbh = PMG
:: DBTools
:: open_ruledb
();
474 my $start = $param ->{ starttime
} // ( time - 86400 );
475 my $end = $param ->{ endtime
} // ( $start + 86400 );
477 my $sth = $dbh -> prepare (
478 "SELECT DISTINCT pmail " .
479 "FROM CMailStore, CMSReceivers WHERE " .
480 "time >= $start AND time < $end AND " .
481 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
482 "AND Status = 'N' ORDER BY pmail" );
486 while ( my $ref = $sth -> fetchrow_hashref ()) {
487 push @$res, { mail
=> $ref ->{ pmail
} };
493 __PACKAGE__-
> register_method ({
494 name
=> 'spamstatus' ,
495 path
=> 'spamstatus' ,
497 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
498 description
=> "Get Spam Quarantine Status" ,
500 additionalProperties
=> 0 ,
507 description
=> 'Number of stored mails.' ,
511 description
=> "Estimated disk space usage in MByte." ,
515 description
=> "Average size of stored mails in bytes." ,
519 description
=> "Average spam level." ,
527 my $dbh = PMG
:: DBTools
:: open_ruledb
();
528 my $ref = PMG
:: DBTools
:: get_quarantine_count
( $dbh, 'S' );
533 __PACKAGE__-
> register_method ({
537 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
538 description
=> "Get a list of users with whitelist/blacklist setttings." ,
540 additionalProperties
=> 0 ,
544 description
=> 'If set, limits the result to the given list.' ,
545 enum
=> [ 'BL' , 'WL' ],
556 description
=> 'the receiving email' ,
565 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
566 my $authuser = $rpcenv -> get_user ();
570 my $dbh = PMG
:: DBTools
:: open_ruledb
();
573 if ( $param ->{ list
}) {
574 $sth = $dbh -> prepare ( "SELECT DISTINCT pmail FROM UserPrefs WHERE name = ? ORDER BY pmail" );
575 $sth -> execute ( $param ->{ list
});
577 $sth = $dbh -> prepare ( "SELECT DISTINCT pmail FROM UserPrefs ORDER BY pmail" );
581 while ( my $ref = $sth -> fetchrow_hashref ()) {
582 push @$res, { mail
=> $ref ->{ pmail
} };
588 my $quarantine_api = sub {
589 my ( $param, $quartype, $check_pmail ) = @_ ;
591 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
592 my $authuser = $rpcenv -> get_user ();
594 my $start = $param ->{ starttime
} // ( time - 86400 );
595 my $end = $param ->{ endtime
} // ( $start + 86400 );
600 my $role = $rpcenv -> get_role ();
601 $pmail = $verify_optional_pmail ->( $authuser, $role, $param ->{ pmail
});
602 $select = "SELECT * " .
603 "FROM CMailStore, CMSReceivers WHERE " .
604 "pmail = ? AND time >= $start AND time < $end AND " .
605 "QType = ' $quartype ' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
606 "AND Status = 'N' ORDER BY pmail, time, receiver" ;
608 $select = "SELECT * " .
609 "FROM CMailStore, CMSReceivers WHERE " .
610 "time >= $start AND time < $end AND " .
611 "QType = ' $quartype ' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
612 "AND Status = 'N' ORDER BY time, receiver" ;
617 my $dbh = PMG
:: DBTools
:: open_ruledb
();
619 my $sth = $dbh -> prepare ( $select );
622 $sth -> execute ( $pmail );
627 while ( my $ref = $sth -> fetchrow_hashref ()) {
628 my $data = $parse_header_info ->( $ref );
635 __PACKAGE__-
> register_method ({
639 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
640 description
=> "Get a list of quarantined spam mails in the given timeframe (default the last 24 hours) for the given user." ,
642 additionalProperties
=> 0 ,
644 starttime
=> get_standard_option
( 'pmg-starttime' ),
645 endtime
=> get_standard_option
( 'pmg-endtime' ),
646 pmail
=> $pmail_param_type,
655 description
=> 'Unique ID' ,
659 description
=> "Size of raw email." ,
663 description
=> "SMTP envelope sender." ,
667 description
=> "Header 'From' field." ,
671 description
=> "Header 'Sender' field." ,
676 description
=> "Receiver email address" ,
680 description
=> "Header 'Subject' field." ,
684 description
=> "Receive time stamp" ,
688 description
=> "Spam score." ,
696 return $quarantine_api ->( $param, 'S' , 1 );
699 __PACKAGE__-
> register_method ({
703 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
704 description
=> "Get a list of quarantined virus mails in the given timeframe (default the last 24 hours)." ,
706 additionalProperties
=> 0 ,
708 starttime
=> get_standard_option
( 'pmg-starttime' ),
709 endtime
=> get_standard_option
( 'pmg-endtime' ),
718 description
=> 'Unique ID' ,
722 description
=> "Size of raw email." ,
726 description
=> "SMTP envelope sender." ,
730 description
=> "Header 'From' field." ,
734 description
=> "Header 'Sender' field." ,
739 description
=> "Receiver email address" ,
743 description
=> "Header 'Subject' field." ,
747 description
=> "Receive time stamp" ,
751 description
=> "Virus name." ,
759 return $quarantine_api ->( $param, 'V' );
762 __PACKAGE__-
> register_method ({
763 name
=> 'attachment' ,
764 path
=> 'attachment' ,
766 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
767 description
=> "Get a list of quarantined attachment mails in the given timeframe (default the last 24 hours)." ,
769 additionalProperties
=> 0 ,
771 starttime
=> get_standard_option
( 'pmg-starttime' ),
772 endtime
=> get_standard_option
( 'pmg-endtime' ),
781 description
=> 'Unique ID' ,
785 description
=> "Size of raw email." ,
789 description
=> "SMTP envelope sender." ,
793 description
=> "Header 'From' field." ,
797 description
=> "Header 'Sender' field." ,
802 description
=> "Receiver email address" ,
806 description
=> "Header 'Subject' field." ,
810 description
=> "Receive time stamp" ,
818 return $quarantine_api ->( $param, 'A' );
821 __PACKAGE__-
> register_method ({
822 name
=> 'virusstatus' ,
823 path
=> 'virusstatus' ,
825 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
826 description
=> "Get Virus Quarantine Status" ,
828 additionalProperties
=> 0 ,
835 description
=> 'Number of stored mails.' ,
839 description
=> "Estimated disk space usage in MByte." ,
843 description
=> "Average size of stored mails in bytes." ,
851 my $dbh = PMG
:: DBTools
:: open_ruledb
();
852 my $ref = PMG
:: DBTools
:: get_quarantine_count
( $dbh, 'V' );
854 delete $ref ->{ avgspam
};
859 my $get_and_check_mail = sub {
860 my ( $id, $rpcenv, $dbh ) = @_ ;
862 my ( $cid, $rid, $tid ) = $id =~ m/^C(\d+)R(\d+)T(\d+)$/ ;
868 $dbh = PMG
:: DBTools
:: open_ruledb
();
871 my $ref = PMG
:: DBTools
:: load_mail_data
( $dbh, $cid, $rid, $tid );
873 my $authuser = $rpcenv -> get_user ();
874 my $role = $rpcenv -> get_role ();
876 if ( $role eq 'quser' ) {
877 my $quar_username = $ref ->{ pmail
} . ' @quarantine ' ;
878 raise_perm_exc
( "mail does not belong to user ' $authuser ' ( $ref ->{pmail})" )
879 if $authuser ne $quar_username ;
885 __PACKAGE__-
> register_method ({
889 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
890 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)." ,
892 additionalProperties
=> 0 ,
895 description
=> 'Unique ID' ,
897 pattern
=> 'C\d+R\d+T\d+' ,
901 description
=> "Display 'raw' eml data. Deactivates size limit." ,
912 description
=> 'Unique ID' ,
916 description
=> "Size of raw email." ,
920 description
=> "SMTP envelope sender." ,
924 description
=> "Header 'From' field." ,
928 description
=> "Header 'Sender' field." ,
933 description
=> "Receiver email address" ,
937 description
=> "Header 'Subject' field." ,
941 description
=> "Receive time stamp" ,
945 description
=> "Spam score." ,
949 description
=> "Information about matched spam tests (name, score, desc, url)." ,
953 description
=> "Raw email header data." ,
957 description
=> "Raw email data (first 4096 bytes). Useful for preview. NOTE: The 'htmlmail' formatter displays the whole email." ,
965 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
966 my $format = $rpcenv -> get_format ();
968 my $raw = $param ->{ raw
} // 0 ;
970 my $ref = $get_and_check_mail ->( $param ->{ id
}, $rpcenv );
972 my $res = $parse_header_info ->( $ref );
974 my $filename = $ref ->{ file
};
975 my $spooldir = $PMG :: MailQueue
:: spooldir
;
977 my $path = " $spooldir/$filename " ;
979 if ( $format eq 'htmlmail' ) {
981 my $cfg = PMG
:: Config-
> new ();
982 my $viewimages = $cfg -> get ( 'spamquar' , 'viewimages' );
983 my $allowhref = $cfg -> get ( 'spamquar' , 'allowhrefs' );
985 $res ->{ content
} = PMG
:: HTMLMail
:: email_to_html
( $path, $raw, $viewimages, $allowhref ) // 'unable to parse mail' ;
987 # to make result verification happy
990 $res ->{ spamlevel
} = 0 ;
991 $res ->{ spaminfo
} = [];
993 # include additional details
995 # we want to get the whole email in raw mode
996 my $maxbytes = (! $raw ) ?
4096 : undef ;
998 my ( $header, $content ) = PMG
:: HTMLMail
:: read_raw_email
( $path, $maxbytes );
1000 $res ->{ file
} = $ref ->{ file
};
1001 $res ->{ spaminfo
} = decode_spaminfo
( $ref ->{ info
});
1002 $res ->{ header
} = $header ;
1003 $res ->{ content
} = $content ;
1010 my $get_attachments = sub {
1011 my ( $mailid, $dumpdir, $with_path ) = @_ ;
1013 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
1015 my $ref = $get_and_check_mail ->( $mailid, $rpcenv );
1017 my $filename = $ref ->{ file
};
1018 my $spooldir = $PMG :: MailQueue
:: spooldir
;
1020 my $parser = PMG
:: MIMEUtils
:: new_mime_parser
({
1023 extract_uuencode
=> 0 ,
1024 dumpdir
=> $dumpdir,
1027 my $entity = $parser -> parse_open ( " $spooldir/$filename " );
1028 PMG
:: MIMEUtils
:: fixup_multipart
( $entity );
1029 PMG
:: MailQueue
:: decode_entities
( $parser, 'attachmentquarantine' , $entity );
1034 PMG
:: MIMEUtils
:: traverse_mime_parts
( $entity, sub {
1036 my $name = PMG
:: Utils
:: extract_filename
( $part -> head ) || "part- $id " ;
1037 my $attachment_path = $part ->{ PMX_decoded_path
};
1038 return if ! $attachment_path || ! - f
$attachment_path ;
1039 my $size = - s
$attachment_path // 0 ;
1044 'content-type' => $part -> head -> mime_attr ( 'content-type' ),
1046 $entry ->{ path
} = $attachment_path if $with_path ;
1054 __PACKAGE__-
> register_method ({
1055 name
=> 'listattachments' ,
1056 path
=> 'listattachments' ,
1058 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' ] },
1059 description
=> "Get Attachments for E-Mail in Quarantine." ,
1061 additionalProperties
=> 0 ,
1064 description
=> 'Unique ID' ,
1066 pattern
=> 'C\d+R\d+T\d+' ,
1077 description
=> 'Attachment ID' ,
1081 description
=> "Size of raw attachment in bytes." ,
1085 description
=> "Raw email header data." ,
1089 description
=> "Raw email header data." ,
1098 my $dumpdir = "/run/pmgproxy/pmg- $param ->{id}- $$ " ;
1099 my $res = $get_attachments ->( $param ->{ id
}, $dumpdir );
1106 __PACKAGE__-
> register_method ({
1110 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'audit' , 'quser' ] },
1111 description
=> "Download E-Mail or Attachment from Quarantine." ,
1114 additionalProperties
=> 0 ,
1117 description
=> 'Unique ID' ,
1119 pattern
=> 'C\d+R\d+T\d+' ,
1123 description
=> "The Attachment ID for the mail." ,
1135 my $mailid = $param ->{ mailid
};
1136 my $attachmentid = $param ->{ attachmentid
};
1138 my $dumpdir = "/run/pmgproxy/pmg- $mailid - $$/ " ;
1141 if ( $attachmentid ) {
1142 my $attachments = $get_attachments ->( $mailid, $dumpdir, 1 );
1143 $res = $attachments ->[ $attachmentid ];
1145 raise_param_exc
({ attachmentid
=> "Invalid Attachment ID for Mail." });
1148 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
1149 my $ref = $get_and_check_mail ->( $mailid, $rpcenv );
1150 my $spooldir = $PMG :: MailQueue
:: spooldir
;
1153 'content-type' => 'message/rfc822' ,
1154 path
=> " $spooldir/$ref ->{file}" ,
1158 $res ->{ fh
} = IO
:: File-
> new ( $res ->{ path
}, '<' ) ||
1159 die "unable to open file ' $res ->{path}' - $!\n " ;
1161 rmtree
$dumpdir if - e
$dumpdir ;
1167 PVE
:: APIServer
:: Formatter
:: register_page_formatter
(
1168 'format' => 'htmlmail' ,
1170 path
=> '/quarantine/content' ,
1172 my ( $res, $data, $param, $path, $auth, $config ) = @_ ;
1174 if (! HTTP
:: Status
:: is_success
( $res ->{ status
})) {
1175 return ( "Error $res ->{status}: $res ->{message}" , "text/plain" );
1178 my $ct = "text/html;charset=UTF-8" ;
1180 my $raw = $data ->{ content
};
1182 return ( encode
( 'UTF-8' , $raw ), $ct, 1 );
1185 __PACKAGE__-
> register_method ({
1189 description
=> "Execute quarantine actions." ,
1190 permissions
=> { check
=> [ 'admin' , 'qmanager' , 'quser' ] },
1193 additionalProperties
=> 0 ,
1196 description
=> 'Unique IDs, seperate with ;' ,
1198 pattern
=> 'C\d+R\d+T\d+(;C\d+R\d+T\d+)*' ,
1201 description
=> 'Action - specify what you want to do with the mail.' ,
1203 enum
=> [ 'whitelist' , 'blacklist' , 'deliver' , 'delete' ],
1207 returns
=> { type
=> "null" },
1211 my $rpcenv = PMG
:: RESTEnvironment-
> get ();
1212 my $action = $param ->{ action
};
1213 my @idlist = split ( ';' , $param ->{ id
});
1215 my $dbh = PMG
:: DBTools
:: open_ruledb
();
1217 for my $id ( @idlist ) {
1219 my $ref = $get_and_check_mail ->( $id, $rpcenv, $dbh );
1220 my $sender = $get_real_sender ->( $ref );
1222 if ( $action eq 'whitelist' ) {
1223 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $ref ->{ pmail
}, 'WL' , [ $sender ]);
1224 } elsif ( $action eq 'blacklist' ) {
1225 PMG
:: Quarantine
:: add_to_blackwhite
( $dbh, $ref ->{ pmail
}, 'BL' , [ $sender ]);
1226 } elsif ( $action eq 'deliver' ) {
1227 PMG
:: Quarantine
:: deliver_quarantined_mail
( $dbh, $ref, $ref ->{ receiver
} // $ref ->{ pmail
});
1228 } elsif ( $action eq 'delete' ) {
1229 PMG
:: Quarantine
:: delete_quarantined_mail
( $dbh, $ref );
1231 die "internal error" ; # should not be reached