IBM Support

AIX: Reading Password History Database

How To


Summary

This article gives some examples of how to use the dbm functions (dbm_open, dbm_fetch, etc) to work with the AIX password history database.

Objective

Provide code examples for working with the AIX password history database.

Steps

If the 'histsize' variable is set for your users, AIX maintains a history of password changes to prevent users from reusing a recent password.

AIX provides functions in order to read/add/delete entries from the password history database (dbm_open, dbm_fetch, dbm_delete, etc), but no ready-made tools that use these functions - they are only used internally during password change operations.

Provided here are two examples of how to use these functions.

First, here is an example that allows you to view the contents of the password history database:


#include <ndbm.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <strings.h>
#include <time.h>

int main(int argc, char * argv[])
{
        DBM *pwdhistDB;
        datum dataKey, dataVal;
        char encrypt[200];
        char dbFile[256];

        if (argc<2) strcpy(dbFile,"/etc/security/pwdhist");
        else strcpy(dbFile,argv[1]);

        pwdhistDB = dbm_open(dbFile, O_RDWR , 600);
        if(pwdhistDB == NULL)
        {
                printf("Could not open %s.  errno: %d.\n",dbFile,errno);
                exit(1);
        }

        /*  Get the first key */
        dataKey = dbm_firstkey(pwdhistDB);
        do
        {
                char * name;

                unsigned long date;
                bzero(encrypt,200);

                dataVal = dbm_fetch(pwdhistDB, dataKey);

                if(dataVal.dptr == NULL)
                {
                        printf("Could not fetch %s\n", (char *)dataKey.dptr);
                }
                else
                {
                        char *dataPtr;
                        char *newstr;

                        name=(char *) malloc (dataKey.dsize+1);
                        bzero(name,dataKey.dsize+1);

                        /*Found a record; store the user's name*/
                        strncpy(name,dataKey.dptr,dataKey.dsize);

                        /*read in the data portion*/
                        dataPtr = (char *) malloc(dataVal.dsize);
                        newstr = dataPtr;
                        bcopy(dataVal.dptr,dataPtr,dataVal.dsize);

                        while(1) {
                                if(newstr[4] == '{' || newstr[4] == '$') {
                                        char * comma;
                                        /* non-crypt passwords - designated with {algorithmname} -
                                         * use a comma separator. 4 bytes for date, and read until
                                         * the comma to get the password. 
                                         * if prefix_dollar option is used, a '$' is used to  
                                         * specify the algorithm type: $5$ or $6$ */
                                 
                                        bcopy(newstr,&date,4);
                                        comma=(char * )strchr( newstr + 4, 44); /* search for comma */

                                        if(comma==NULL) comma=newstr+strlen(newstr); /* this is incase comma isn't there, go to end of newstr */
                                        bcopy(newstr+4, encrypt, (int) (comma-(newstr+4))); /* copy starting past the date, until the comma */
                                        newstr=newstr + (int) (comma-newstr) +1; /* advance the pointer with +1 for the comma */
                                } else {
                                        /* crypt passwords are not stored with a separator
                                         * They are 4 bytes for date and 13 for password */
                                          bcopy(newstr,&date,4);
                                          bcopy(newstr+4,encrypt,13);
                                          newstr=newstr+17;
                                }

                                /* output the data, use ctime to translate epoch time */
                                printf("User '%s': ==> '%lu' '%s'  %s", name,date,encrypt,ctime((void *) &date));
                                bzero(encrypt,200);

                                /*quit if we've reached the end of the dataVal data size */
                                if(newstr - dataPtr >= dataVal.dsize) break;
                        }
                        free(dataPtr);dataPtr=NULL;
                        free(name);name=NULL;
                }
                /* Get the next key     */
                dataKey = dbm_nextkey(pwdhistDB);
        } while (dataKey.dptr != NULL);
        /* Close the DB */
        dbm_close(pwdhistDB);
        return 0;
}

Compile this program with a C compiler (xlC or gcc, for example):

gcc -o pwdhist pwdhist.c

Run this program with no arguments, and it displays the contents of /etc/security/pwdhist.pag:

# ./pwdhist
User 'nick': ==> '1541695509' '{ssha256}06$HlZAg1kbyC0qAhfk$GU/KJw8aKdjYloykppg5uP05hSmT0NekiA8knMC.2p'  Thu Nov  8 10:45:09 2018
User 'nick': ==> '1541695496' '{ssha256}06$c7/rCX5z3SuVjHVR$2ySS4tqVG1phQ2VXWLNNrDSXGO72VbydcmE9Jqk.A/'  Thu Nov  8 10:44:56 2018
User 'bob': ==> '1541695517' '{ssha256}06$U0duZgjkB.JPHSaL$TOXoYUR1ccnqnhKkxgMaXn5PLJ11fHvv2n2XnAw.oj'  Thu Nov  8 10:45:17 2018

That shows that user 'nick' has had two password changes and the times they occurred, and user 'bob' has one.

An argument can be given to this program if you'd like to run it against something other than the default /etc/security/pwdhist.pag database. For example, if you have a copy of pwdhist.pag in the /tmp directory, you can run:

./pwdhist /tmp/pwdhist

Next, this example allows you to delete a user's information from the pwdhist database:

#include <ndbm.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>

int main(int argc, char * argv[])
{
        DBM *pwdhistDB;
        datum dataKey;
        int rc;
        char dbFile[256];

        if(argc==2) strcpy(dbFile,"/etc/security/pwdhist");
        else strcpy(dbFile,argv[2]);

        pwdhistDB = dbm_open(dbFile, O_RDWR , 600);
        if(pwdhistDB == NULL)
        {
                printf("Could not open %s. errno: %d.\n",dbFile,errno);
                exit(1);
        }

        dataKey.dptr=argv[1];
        dataKey.dsize=strlen(argv[1]);
        rc=dbm_delete(pwdhistDB, dataKey);
        if(rc!=0) printf("failed to delete %s from %s\n",argv[1],dbFile);
        else printf("success removing %s from %s\n",argv[1],dbFile);
}

Compile this program with the C compiler of your choice.  This example deletes a user from the /etc/security/pwdhist.pag database. For example, if it looks like there might be corruption in a user's pwdhist entry, you can delete that user:

# ./pwdhistdel nick
success removing nick from /etc/security/pwdhist

Now, if I check again, I see that only 'bob' remains in the pwdhist database:

# ./pwdhist
User 'bob': ==> '1541695517' '{ssha256}06$U0duZgjkB.JPHSaL$TOXoYUR1ccnqnhKkxgMaXn5PLJ11fHvv2n2XnAw.oj'  Thu Nov  8 10:45:17 2018

Like the first example, this program defaults to operating on /etc/security/pwdhist.pag, but you can add a second argument if you want to do some testing on pwdhist.pag in a non-default location.

[{"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG10","label":"AIX"},"Component":"","Platform":[{"code":"PF002","label":"AIX"}],"Version":"All Versions","Edition":"","Line of Business":{"code":"LOB08","label":"Cognitive Systems"}}]

Document Information

Modified date:
07 July 2025

UID

ibm10739337