Implements password rules.
String
Empty string (no function is called when a password is set).
Can be set for an individual connection, or for PUBLIC. You must have the SET ANY SECURITY OPTION to set this option.
The function specified by this option is called automatically when a non-NULL password is created or set. The function specified is not called for users that have a login policy with the change_password_dual_control option enabled. To prevent a user from overriding the function, set the option value to owner.function-name. A user must have a password to be able to connect to the database.
After validating the statement used to create or set the password, the function is called to verify the password using the specified rules. If the password conforms to the specified rules, the function returns NULL to indicate success, and the invoking statement executes. Otherwise a non-NULL string is returned, and is cited as the reason for the error.
The password verification function takes two parameters: user-name VARCHAR(128) and new-pwd VARCHAR(255). It returns a value of type VARCHAR(255).
You should execute an ALTER FUNCTION function-name SET HIDDEN statement on the password verification function to ensure that it cannot be stepped through using the debugger.
If the verify_password_function option is set, specifying more than one user ID and password with a GRANT CONNECT statement is not allowed.
User IDs cannot:
Passwords are case-sensitive and they cannot:
For more information about password rules, see Use password verification.
The following example defines a table and a function and sets some login policy options. Together they implement advanced password rules that include requiring certain types of characters in the password, disallowing password re-use, and expiring passwords. The function is called by the database server with the verify_password_function option when a user ID is created or a password is changed. The application can call the procedure specified by the post_login_procedure option to report that the password should be changed before it expires.
The code for this sample is also available in the following location: %SQLANYSAMP16%\SQLAnywhere\SQL\verify_password.sql.
-- only DBA should have privileges on this table CREATE TABLE DBA.t_pwd_history( pk INT DEFAULT AUTOINCREMENT PRIMARY KEY, user_name CHAR(128), -- the user whose password is set pwd_hash CHAR(32) ); -- hash of password value to detect -- duplicate passwords -- called whenever a non-NULL password is set -- to verify the password conforms to password rules CREATE FUNCTION DBA.f_verify_pwd( uid VARCHAR(128), new_pwd VARCHAR(255) ) RETURNS VARCHAR(255) BEGIN -- a table with one row per character in new_pwd DECLARE local temporary table pwd_chars( pos INT PRIMARY KEY, -- index of c in new_pwd c CHAR( 1 CHAR ) ); -- character -- new_pwd with non-alpha characters removed DECLARE pwd_alpha_only CHAR(255); DECLARE num_lower_chars INT; -- enforce minimum length (can also be done with -- min_password_length option) IF length( new_pwd ) < 6 THEN RETURN 'password must be at least 6 characters long'; END IF; -- break new_pwd into one row per character INSERT INTO pwd_chars SELECT row_num, substr( new_pwd, row_num, 1 ) FROM dbo.RowGenerator WHERE row_num <= length( new_pwd ); -- copy of new_pwd containing alpha-only characters SELECT list( c, '' ORDER BY pos ) INTO pwd_alpha_only FROM pwd_chars WHERE c BETWEEN 'a' AND 'z' OR c BETWEEN 'A' AND 'Z'; -- number of lowercase characters IN new_pwd SELECT count(*) INTO num_lower_chars FROM pwd_chars WHERE CAST( c AS BINARY ) BETWEEN 'a' AND 'z'; -- enforce rules based on characters contained in new_pwd IF ( SELECT count(*) FROM pwd_chars WHERE c BETWEEN '0' AND '9' ) < 1 THEN RETURN 'password must contain at least one numeric digit'; ELSEIF length( pwd_alpha_only ) < 2 THEN RETURN 'password must contain at least two letters'; ELSEIF num_lower_chars = 0 OR length( pwd_alpha_only ) - num_lower_chars = 0 THEN RETURN 'password must contain both upper- and lowercase characters'; END IF; -- not the same as any user name -- (this could be modified to check against a disallowed words table) IF EXISTS( SELECT * FROM SYS.SYSUSER WHERE lower( user_name ) IN ( lower( pwd_alpha_only ), lower( new_pwd ) ) ) THEN RETURN 'password or only alphabetic characters in password ' || 'must not match any user name'; END IF; -- not the same as any previous password for this user IF EXISTS( SELECT * FROM t_pwd_history WHERE user_name = uid AND pwd_hash = hash( uid || new_pwd, 'md5' ) ) THEN RETURN 'previous passwords cannot be reused'; END IF; -- save the new password INSERT INTO t_pwd_history( user_name, pwd_hash ) VALUES( uid, hash( uid || new_pwd, 'md5' ) ); RETURN( NULL ); END; ALTER FUNCTION DBA.f_verify_pwd SET HIDDEN; GRANT EXECUTE ON DBA.f_verify_pwd TO PUBLIC; SET OPTION PUBLIC.verify_password_function = 'DBA.f_verify_pwd'; -- All passwords expire in 180 days. Expired passwords can be changed -- by the user using the NewPassword connection parameter. ALTER LOGIN POLICY DEFAULT password_life_time = 180; -- If an application calls the procedure specified by the -- post_login_procedure option, then the procedure can be used to -- warn the user that their password is about to expire. In particular, -- Interactive SQL and Sybase Central call the post_login_procedure. ALTER LOGIN POLICY DEFAULT password_grace_time = 30; -- Five consecutive failed login attempts results in a non-DBA -- user ID being locked. ALTER LOGIN POLICY DEFAULT max_failed_login_attempts = 5; |
![]() |
Discuter à propos de cette page dans DocCommentXchange.
|
Copyright © 2013, SAP AG ou société affiliée SAP - SAP Sybase SQL Anywhere 16.0 |