putspent()

Updated: April 19, 2023

Put an entry into the shadow password database

Synopsis:

#include <sys/types.h>
#include <shadow.h>

int putspent( const struct spwd* p,
              FILE* fp );

Arguments:

p
A pointer to a spwd structure that contains the entry that you want to write.
fp
The stream that you want to write the entry on.

Library:

libc

Use the -l c option to qcc to link against this library. This library is usually included automatically.

Description:

The putspent() function writes a shadow password entry into the specified file. This function is the inverse of getspent().

Given a pointer to a spwd structure created by the getspent() or the getspnam() routine, putspent() writes a line on the stream fp, which matches the format of </etc/shadow>. The spwd structure contains the following members:

        char    *sp_namp;     /* name */
        char    *sp_pwdp;     /* encrypted password */
        long    sp_lstchg;    /* last changed */
        long    sp_max;       /* #days (min) to change */
        long    sp_min;       /* #days (max) to change */
        long    sp_warn;      /* #days to warn */
        long    sp_inact;     /* #days of inactivity */
        long    sp_expire;    /* date to auto-expire */
        long    sp_flag;      /* reserved */

If the sp_min, sp_max, sp_lstchg, sp_warn, sp_inact, or sp_expire field of the structure is -1, or if sp_flag = 0, the corresponding </etc/shadow> field is cleared.

Returns:

Zero.

Errors:

The putspent() function uses the following functions, and as a result errno can be set to an error for any of these calls:

Examples:

#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <shadow.h>

/*
 * This program adds a user and password to
 * a temporary file which can then be used with
 * fgetspent() (of course the password
 * string should be encrypted already etc.)
 */

int main( int argc, char** argv )
{
     FILE* fp;
 struct spwd   sp;
     char              pwbuf[80], nambuf[80];

     memset(&sp, 0, sizeof(sp));
     if (argc < 2) {
           printf("%s filename \n", argv[0]);
           return(EXIT_FAILURE);
     }

     if (!(fp = fopen(argv[1], "w"))) {
           fprintf(stderr, "Can't open file %s \n", argv[1]);
           perror("Problem ");
           return(1);
     }

     printf("Enter a userid: ");
     if (!gets(nambuf)) {
           fprintf(stderr, "Can't get username  string\n");
     }
     sp.sp_namp = nambuf;

     printf("Enter a password: ");
     if (!gets(pwbuf)) {
           fprintf(stderr, "Can't get username  password\n");
     }
     sp.sp_pwdp = pwbuf;

     putspent(&sp, fp);
     fclose(fp);
 return( EXIT_SUCCESS );
}

Classification:

Unix

Safety:  
Cancellation point Yes
Interrupt handler No
Signal handler No
Thread No