{"id":51,"date":"2009-08-13T13:00:00","date_gmt":"2009-08-13T18:00:00","guid":{"rendered":"http:\/\/blogs.wp.stage.cpanel.net\/2009\/08\/writing_an_ftp_password_trap_in_perl\/"},"modified":"2009-08-13T13:00:00","modified_gmt":"2009-08-13T18:00:00","slug":"writing_an_ftp_password_trap_in_perl","status":"publish","type":"post","link":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/","title":{"rendered":"Writing an FTP password trap in Perl"},"content":{"rendered":"

Password traps are probably the type of plugin that generates the most support requests at the moment. When creating a password trap, it is important that you use a Function Hook<\/a> rather than a Custom Event Handler<\/a>. The reason for this is that function hooks are executed as root, while custom event handlers are executed as the user. If you are not familiar with function hooks, please read the Documentation<\/a>.<\/p>\n

Function hooks reside as scripts in the subdirectories of \/usr\/local\/cpanel\/hooks\/<\/em>. Inside of the hooks\/ <\/em>directory are subdirectories for each module of cPanel, such as ftp\/<\/em> or email\/<\/em>. When an API call is made, the corresponding module’s subdirectory is checked for a script. If one exists, the script is passed XML data that contains both the parameters passed to the API call and information about the user. The XML data looks something like this:
\n<cpanelevent>
\n<errors><\/errors>
\n<event>FUNCTION NAME<\/event>
\n<module>MODULE NAME<\/module>
\n<params>
\n<param0><\/param0>
\n<param1><\/param1>
\n<param2><\/param2>
\n...
\n<\/params>
\n<\/cpanelevent>
\n<CPDATA>
\n<BWLIMIT>unlimited<\/BWLIMIT>
\n.. Data from \/var\/cpanel\/users\/USERNAME
\n<USER>cptest<\/USER>
\n<\/CPDATA><\/code><\/p>\n

As you can see from the example, each piece of information is held between tags called containers. In this example, the container merely correlates to data contained within the \/var\/cpanel\/users\/ <\/em>directory. The parameters used to call the function are contained within the container.<\/p>\n

In order to read this data in in Perl, we will need to have the following bit of code:
\nmy $xml;
\nwhile()
\n{
\n$xml .= $_;
\n} <\/code><\/p>\n

This piece of code will assign data to the $xml <\/em>variable, reading one line at a time. Once the data is contained within the variable, we’ll need to transform it into a hash reference using XML::Simple’s XMLin<\/em> function.
\nmy $call_info = XMLin($xml);<\/code><\/p>\n

Once the information you need has been transformed into a hash, you can access the parameters like any normal hash. In the case of cpanelevent, it looks like the following:
\n{
\n'params' => {
\n'param5' => 'public_html\/wierdo',
\n'param4' => {},
\n'param0' => 'wierdo',
\n'param2' => 'public_html\/wierdo',
\n'param3' => 'unlimited',
\n'param1' => 'Tm$qgIAwrH4A'
\n},
\n'errors' => {},
\n'event' => 'addftp',
\n'module' => 'ftp'
\n}<\/code><\/p>\n

As you can see from the example, each parameter is named using the “param” prefix. These parameters will show up on any API1 call. To make sorting this information easier, we will need to define what information we want to save into a couple of scalar variables. For example:
\nmy $system_user = $api_call->{'CPDATA'}->{'USER'};
\nmy $ftp_user = $api_call->{'cpanelevent'}->{'params'}->{'param0'};
\nmy $ftp_password = $api_call->{'cpanelevent'}->{'params'}->{'param1'};<\/code><\/p>\n

At this point, we’ve worked out how to access all that data that we need. Now, we’ll need to figure out where to log it. For the purposes of this article, we’ll use YAML::Syck<\/a>. YAML is fast, extremely lightweight, and YAML::Syck can already be found on every cPanel system. But before we can really begin working with YAML::Syck, we’ll need to work on our data structure. The following example should work well:
\n{
\nsystem_user => {
\nftp_user => ftp_password,
\nftp_user2 => ftp_password2
\n},
\nsystem_user2 => {
\nanotheruser => anotherpassword
\n}
\n} <\/code><\/p>\n

At this point we’ll need to load the YAML data from a file (if it exists):
\nmy $datastore = [];
\nif (-e \"\/root\/ftp_accounts\") {
\n$datastore = LoadFile(\"\/root\/ftp_accounts\");
\n}<\/code><\/p>\n

And then add the data to the hash:
\n$datastore->{ $system_user }->{ $ftp_user } = $ftp_password; <\/code><\/p>\n

At this point, our data structure has been set up and is loading from a file, so all we have to do is save it to the file when it completes adding the data to the hash.
\nDumpFile(\"\/root\/ftp_accounts\", $datastore);<\/code><\/p>\n

Now that the script is finished, we’ll simply need to upload it, because this password trap affects cPanel’s FTP module, \/usr\/local\/cpanel\/hooks\/ftp\/addftp<\/em> and \/usr\/local\/cpanel\/hooks\/ftp\/passwdftp<\/em>. Once this has been done, \/root\/ftp_accounts <\/em>will contain a complete list of FTP accounts on the system after they have been created or had their passwords changed.<\/p>\n

If you wish to download the information in this tutorial, it is available here.<\/p>\n","protected":false},"excerpt":{"rendered":"

Password traps are probably the type of plugin that generates the most support requests at the moment. When creating a password trap, it is important that you use a Function Hook rather than a Custom Event Handler. The reason for this is that function hooks are executed as root, while custom event handlers are executed […]<\/p>\n","protected":false},"author":77,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[49],"tags":[313,317,321,325,329],"class_list":["post-51","post","type-post","status-publish","format-standard","hentry","category-products","tag-function-hooks","tag-inside-cpanel","tag-password-trap","tag-product-development","tag-yaml"],"acf":[],"yoast_head":"\nWriting an FTP password trap in Perl | cPanel<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Writing an FTP password trap in Perl | cPanel\" \/>\n<meta property=\"og:description\" content=\"Password traps are probably the type of plugin that generates the most support requests at the moment. When creating a password trap, it is important that you use a Function Hook rather than a Custom Event Handler. The reason for this is that function hooks are executed as root, while custom event handlers are executed […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/\" \/>\n<meta property=\"og:site_name\" content=\"cPanel\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cpanel\/\" \/>\n<meta property=\"article:published_time\" content=\"2009-08-13T18:00:00+00:00\" \/>\n<meta name=\"author\" content=\"cPanel Community\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@cPanel\" \/>\n<meta name=\"twitter:site\" content=\"@cPanel\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"cPanel Community\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/\",\"url\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/\",\"name\":\"Writing an FTP password trap in Perl | cPanel\",\"isPartOf\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/#website\"},\"datePublished\":\"2009-08-13T18:00:00+00:00\",\"dateModified\":\"2009-08-13T18:00:00+00:00\",\"author\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8\"},\"breadcrumb\":{\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/devel.www.cpanel.net\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Writing an FTP password trap in Perl\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#website\",\"url\":\"https:\/\/devel.www.cpanel.net\/\",\"name\":\"cPanel\",\"description\":\"Hosting Platform of Choices\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/devel.www.cpanel.net\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8\",\"name\":\"cPanel Community\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g\",\"caption\":\"cPanel Community\"},\"description\":\"The web hosting industry's most reliable management solution since 1997. With our first-class support and rich feature set, it's easy to see why our customers and partners make cPanel & WHM their hosting platform of choice. For more information, visit cPanel.net.\",\"sameAs\":[\"https:\/\/cpanel.net\"],\"url\":\"https:\/\/devel.www.cpanel.net\/blog\/author\/cpadmin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Writing an FTP password trap in Perl | cPanel","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/","og_locale":"en_US","og_type":"article","og_title":"Writing an FTP password trap in Perl | cPanel","og_description":"Password traps are probably the type of plugin that generates the most support requests at the moment. When creating a password trap, it is important that you use a Function Hook rather than a Custom Event Handler. The reason for this is that function hooks are executed as root, while custom event handlers are executed […]","og_url":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/","og_site_name":"cPanel","article_publisher":"https:\/\/www.facebook.com\/cpanel\/","article_published_time":"2009-08-13T18:00:00+00:00","author":"cPanel Community","twitter_card":"summary_large_image","twitter_creator":"@cPanel","twitter_site":"@cPanel","twitter_misc":{"Written by":"cPanel Community","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/","url":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/","name":"Writing an FTP password trap in Perl | cPanel","isPartOf":{"@id":"https:\/\/devel.www.cpanel.net\/#website"},"datePublished":"2009-08-13T18:00:00+00:00","dateModified":"2009-08-13T18:00:00+00:00","author":{"@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8"},"breadcrumb":{"@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/devel.www.cpanel.net\/blog\/products\/writing_an_ftp_password_trap_in_perl\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/devel.www.cpanel.net\/"},{"@type":"ListItem","position":2,"name":"Writing an FTP password trap in Perl"}]},{"@type":"WebSite","@id":"https:\/\/devel.www.cpanel.net\/#website","url":"https:\/\/devel.www.cpanel.net\/","name":"cPanel","description":"Hosting Platform of Choices","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/devel.www.cpanel.net\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/8cf97408aad4fb70cf55d11a1d4f57f8","name":"cPanel Community","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/devel.www.cpanel.net\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e1949945083b5526bb95711bd3d616b3?s=96&d=mm&r=g","caption":"cPanel Community"},"description":"The web hosting industry's most reliable management solution since 1997. With our first-class support and rich feature set, it's easy to see why our customers and partners make cPanel & WHM their hosting platform of choice. For more information, visit cPanel.net.","sameAs":["https:\/\/cpanel.net"],"url":"https:\/\/devel.www.cpanel.net\/blog\/author\/cpadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts\/51"}],"collection":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/users\/77"}],"replies":[{"embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/comments?post=51"}],"version-history":[{"count":0,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/posts\/51\/revisions"}],"wp:attachment":[{"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/media?parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/categories?post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devel.www.cpanel.net\/wp-json\/wp\/v2\/tags?post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}