Skip to content

prewwwacct hooks — refusing a createacct before it commits

A pre-create hook can block an account creation that would violate your conventions — username pattern, package eligibility, IP exhaustion — by returning non-zero.

A reseller creates an account with a username that doesn’t match the corporate naming convention. The accountant downstream cannot match it to a customer in the billing system, the support team can’t find it from the customer’s email domain, and three weeks later somebody has to rename the account — which means terminating and recreating, because cPanel doesn’t support rename. A prewwwacct hook that validates the username before the create even runs prevents this entire class of cleanup work.

What’s happening

The Standardized Hooks system supports both pre and post stages for most events. A pre hook that returns a non-zero exit code aborts the event — the createacct call returns an error to the reseller, and no account is created. The hook receives the call parameters on stdin as JSON; it can inspect username, domain, package, owner, IP, and any custom field, and decide whether to permit or reject. The reseller sees an error message you control.

How to verify

Inspect the registered pre-hooks before adding another:

whmapi1 list_hooks category=Whostmgr stage=pre
ls /var/cpanel/hooks/
cat /var/cpanel/hooks/registered_hooks.yaml
tail -f /usr/local/cpanel/logs/error_log | grep prewwwacct

The list shows which hooks run before Accounts::Create. Multiple hooks for the same event run in order; any non-zero exit code aborts the chain and the create.

The fix

A username-validation pre-hook in three parts:

  1. Write the validator at /usr/local/cpanel/scripts/yourorg-validate-username.pl:
#!/usr/bin/perl
use strict;
use Cpanel::JSON ();
my $data = Cpanel::JSON::Load(do { local $/; <STDIN> });
my $user = $data->{user};
# Corporate convention: lowercase, alphanumeric, 5-12 chars
if ($user !~ /^[a-z][a-z0-9]{4,11}$/) {
  print STDERR "Username '$user' does not match corporate convention (5-12 chars, lowercase alphanum, leading letter)\n";
  exit 1;
}
exit 0;
  1. Register the hook: whmapi1 add_hook category=Whostmgr event=Accounts::Create stage=pre hook=/usr/local/cpanel/scripts/yourorg-validate-username.pl.
  2. Test against a real reseller. Try a known-bad username through both WHM UI and whmapi1 createacct — both should fail with your message, and no account should be created.
  3. Layer multiple pre-hooks for distinct concerns: one for username pattern, one for package eligibility (does this reseller have the right to create on this package), one for IP exhaustion (does the reseller’s dedicated IP pool have a free slot). cPanel runs them in registration order.

When account-creation policy needs to be enforced across multiple resellers and multiple servers, Stack Harbor’s agency partner practice writes the validators once and deploys them to every host so policy is consistent without per-server drift.