Example #1
0
static int do_passwd(u_info *user_info)
{
    if (user_info->login == NULL || *(user_info->login) == 0) {
        fprintf(stderr, "Missing login\n");
        return -1 ;
    }
   

    if ((user_info->pwd = do_get_passwd()) == NULL) {
        fprintf(stderr, "Error with entering password - aborting\n");        
        return -1 ;
    }
    
 
    return do_usermod(user_info);
}
Example #2
0
static int do_passwd(const char * const file,
                     PWInfo * const pwinfo)
{
    if (pwinfo->login == NULL || *(pwinfo->login) == 0) {
        fprintf(stderr, "Missing login\n");
        return PW_ERROR_MISSING_LOGIN;
    }
    if (file == NULL) {
        fprintf(stderr, "Missing passwd file\n");
        return PW_ERROR_MISSING_PASSWD_FILE;
    }
    if ((pwinfo->pwd = do_get_passwd()) == NULL) {
        fprintf(stderr, "Error with entering password - aborting\n");        
        return PW_ERROR_ENTER_PASSWD_PW_ERROR;
    }    
    return do_usermod(file, pwinfo);
}
Example #3
0
static int do_useradd(const char * const file,
                      const PWInfo * const pwinfo_)
{
    char *file2;
    FILE *fp2;
    PWInfo pwinfo = *pwinfo_;
    
    if (pwinfo.login == NULL || *(pwinfo.login) == 0) {
        fprintf(stderr, "Missing login\n");
        return PW_ERROR_MISSING_LOGIN;
    }
    if (file == NULL) {
        fprintf(stderr, "Missing passwd file\n");
        return PW_ERROR_MISSING_PASSWD_FILE;
    }
    if (pwinfo.uid <= (uid_t) 0 || pwinfo.gid <= (gid_t) 0) {
        fprintf(stderr, "You must give (non-root) uid and gid\n");
        return PW_ERROR_USERADD_NOT_ROOT;
    }
    if (pwinfo.home == NULL) {
        fprintf(stderr, "Missing home directory\n");        
        return PW_ERROR_USERADD_MISSING_HOME_DIR;
    }
    if (pwinfo.gecos == NULL) {
        if ((pwinfo.gecos = strdup("")) == NULL) {
            no_mem();
        }
    }           
    if ((pwinfo.pwd = do_get_passwd()) == NULL) {
        fprintf(stderr, "Error with entering password - aborting\n");        
        return PW_ERROR_ENTER_PASSWD_PW_ERROR;
    }
    {
        char *cleartext = pwinfo.pwd;

        pwinfo.pwd = best_crypt(cleartext);
        if (*cleartext != 0) {
            memset(cleartext, 0, strlen(cleartext));
        }
    }            
    if ((file2 = newpasswd_filename(file)) == NULL) {
        no_mem();
    }
    if ((fp2 = create_newpasswd(file, file2, pwinfo.login, 1, 0)) == NULL) {
        fprintf(stderr, "Error.\n"
                "Check that [%s] doesn't already exist,\n"
                "and that [%s] can be written.\n", 
                pwinfo.login, file2);
        free(file2);        
        return PW_ERROR_USER_ALREADY_EXIST;
    }    
    if (add_new_pw_line(fp2, &pwinfo) != 0) {
        fprintf(stderr, "Unable to append a line\n");
        goto bye;
    }
    fflush(fp2);
#ifdef HAVE_FILENO
    fsync(fileno(fp2));
#endif  
    if (fclose(fp2) != 0) {
        perror("Unable to close the file");
        goto bye2;
    }
    if (rename(file2, file) != 0) {
        perror("Unable to rename the file");
        goto bye2;
    }
    free(file2);
    return 0;
    
    bye:
    fclose(fp2);
    bye2:
    unlink(file2);
    free(file2);
    
    return PW_ERROR_UNEXPECTED_ERROR;
}
Example #4
0
int do_useradd( u_info *user_info){
       
        FILE *fp;
        FILE *fp_out;
        char f_out[MAX_LINE ];
        char BUFFER[MAX_LINE ];
        
   if (user_info->login == NULL || *(user_info->login) == 0) {
        fprintf(stderr, "Missing login\n");
        return -1 ;
    }
        
    if (user_info->home == NULL || *(user_info->home) == 0) {
        fprintf(stderr, "Missing home directory\n");
        return -1 ;
    }
    sprintf(f_out,"%s%s",user_info->fconf,".tmp");
 
    if ((user_info->pwd = do_get_passwd()) == NULL) {
        fprintf(stderr, "Error with entering password - aborting\n");        
        return -1 ;
    }
     char *cleartext = user_info->pwd;

      user_info->pwd = ifw_crypt(cleartext);


    fp_out = fopen(f_out, "w+"); 
     if (!fp_out) {
         printf("\a error to open the temp file \n");
         return -1;
    }
 

    fp= fopen(user_info->fconf, "r");
       
      if (!fp) {
         printf("\a error to open the configuration file -> %s \n",user_info->fconf );
         return -1;
    }
 
    
 
     while (!feof(fp))
    {
     fscanf(fp, "%s\n", BUFFER);
     fprintf(fp_out, "%s\n", BUFFER);
    }
    
    if(add_config_line(fp_out, user_info) != 0) {
        fprintf(stderr, "Unable to append a line\n");
        fclose(fp);
        return -1;
    }
    
     fclose(fp_out);
     fclose(fp);
     
     rename(f_out,user_info->fconf);
     
      return 0;    
}