# pg initpass.c
#include <stdio.h>
#include <usersec.h>
#include <userpw.h>
#include <pwd.h>
#include <errno.h>
#include <sys/time.h>
#include <sys/types.h>
char usernam[10];
char passwrd[64];
long lastupd;
uid_t usernam_uid;
struct passwd pw;
struct passwd *pwp = &pw;
struct userpw upw;
struct userpw *upwp = &upw;
char msg[16][256];
char **msgp;
char hostnam[256];
time_t T;
time_t *Tp = &T;
main(int argc, char *argv[])
{
/* * *
* usage: initpass user password
* only root may run this program
* * */
if (argc != 3)
{
fprintf (stderr, "Usage: initpass user password\n");
exit (1);
}
strcpy (usernam, argv[1]);
strcpy (passwrd, argv[2]);
lastupd = (long) time (Tp);
gethostname (hostnam, 256);
hostnam[255] = '\0';
if ((getuid()) != 0)
{
fprintf (stderr, "Only accessible to root!\n");
exit (2);
}
if ((setuserdb (S_WRITE)) != 0)
perror ("setuserdb write");
if ((setpwdb (S_WRITE)) != 0)
perror ("setpwdb write");
if ((getuserattr (usernam, S_ID, &usernam_uid, SEC_INT)) != 0)
{
if (errno == ENOENT)
{
fprintf (stderr, "Error: user %s does not exist on %s.\n",
usernam, hostnam);
}
exit (3);
}
if ((putuserattr (usernam, S_PWD, "!", SEC_CHAR)) != 0)
{
perror ("putuserattr");
exit (4);
}
if ((putuserattr (usernam, S_ID, "0", SEC_COMMIT)) != 0)
{
perror ("putuserattr commit");
exit (5);
}
strcpy (upw.upw_name, usernam);
upw.upw_passwd = passwrd;
upw.upw_lastupdate = lastupd;
upw.upw_flags = PW_ADMCHG;
if ((putuserpwhist (upwp, msgp)) != 0)
{
perror ("putuserpwhist");
exit (6);
}
if ((enduserdb ()) != 0)
{
perror ("enduserdb");
exit (7);
}
if ((endpwdb ()) != 0)
{
perror ("endpwdb");
exit (8);
}
}