--- gnuser-1.3.0.c 2009-11-08 12:22:31.000000000 +0100 +++ gnuser-1.4.0.c 2009-11-11 14:48:27.000000000 +0100 @@ -1,7 +1,7 @@ /* * Project: gnuser (Gnu User Manager) * File: gnuser.c - * Version: 1.3.0 + * Version: 1.4.0 * Copyright (c) 2009 - Jonathan Salwan * * All rights reserved. @@ -38,6 +38,7 @@ #include #include #include +#include #include /* Files Patach Values */ @@ -51,10 +52,12 @@ #define EXIT_ROOT 2 /* Not root */ #define EXIT_UID 3 /* UID exist */ #define EXIT_GID 4 /* GID exist */ -#define EXIT_LOGIN 5 /* Login name exist */ -#define EXIT_GROUP 6 /* Group name exist */ -#define EXIT_LOGIN_NOT_EXIST 7 /* Login name not exist */ -#define EXIT_GROUP_NOT_EXIST 8 /* Group name not exist */ +#define EXIT_GID_NOT_EXIST 5 /* GID not exist */ +#define EXIT_GID_ERROR 6 /* GID error */ +#define EXIT_LOGIN 7 /* Login name exist */ +#define EXIT_GROUP 8 /* Group name exist */ +#define EXIT_LOGIN_NOT_EXIST 9 /* Login name not exist */ +#define EXIT_GROUP_NOT_EXIST 10 /* Group name not exist */ /* local function prototypes */ static void root(void); @@ -96,7 +99,8 @@ "-s Show user informations\n" "-i Group informations\n" "-g Add a new group\n" - "-G Delete group\n" + "-G Delete group\n" + "-c Change account informations\n" "-v Version informations\n"); exit(EXIT_USAGE); @@ -168,10 +172,33 @@ exit(EXIT_USAGE); } +static void usage_change(void) +{ + fprintf(stderr, + "gnuser: invalid option for change account informations\n" + "Usage: gnuser -c [option] {arg_option} \n\n" + + "Option:\n" + "=======\n" + "passwd Change password\n" + "home Change home directory\n" + "uid Change uid\n" + "gid Change gid\n" + "shell Change shell\n" + "lock Lock account\n\n" + + "Exemple: gnuser -c uid 1002 jonathan\n" + "======== gnuser -c passwd jonathan\n" + " gnuser -c home /home/jonathan2 jonathan\n" + " gnuser -c lock jonathan\n"); + +exit(EXIT_USAGE); +} + static void version(void) { fprintf(stdout, - "gnuser (Gnu User Manager) - v1.3.0\n" + "gnuser (Gnu User Manager) - v1.4.0\n" "GPL Licence\n"); exit(EXIT_SUCCESS); @@ -199,9 +226,6 @@ fclose(fp1); fclose(fp2); - /* On remplace /etc/passwd2 par /etc/passwd puis on supprimer /etc/passwd2 */ - unlink("passwd"); - fp1 = fopen(patch_passwd, "w"); fp2 = fopen("/etc/passwd2", "r"); @@ -236,14 +260,10 @@ cpt++; if(cpt != line_rease) fprintf(fp2,"%s", line); - } + } fclose(fp1); fclose(fp2); - - /* On remplace /etc/shadow2 par /etc/passwd puis on supprimer /etc/shadow2 */ - unlink(patch_shadow); - fp1 = fopen(patch_shadow, "w"); fp2 = fopen("/etc/shadow2", "r"); @@ -388,12 +408,13 @@ if(argc < 2) usage(); - if(strcmp(argv[1], "-a") && - strcmp(argv[1], "-A") && + if(strcmp(argv[1], "-a")&& + strcmp(argv[1], "-A")&& strcmp(argv[1], "-s")&& strcmp(argv[1], "-g")&& strcmp(argv[1], "-G")&& strcmp(argv[1], "-i")&& + strcmp(argv[1], "-c")&& strcmp(argv[1], "-v")) usage(); @@ -411,6 +432,12 @@ exit(EXIT_UID); } + /* Checking if gid not exist in /etc/group */ + if(verif_gid(atoi(argv[4])) == 0){ + fprintf(stderr,"Error: Gid not exist, please change it.\n"); + exit(EXIT_GID_NOT_EXIST); + } + /* Checking if login name exist in /etc/passwd */ if(verif_name(argv[2]) == 1){ fprintf(stderr,"Error: Login name exist, please change it.\n"); @@ -460,7 +487,6 @@ } // [option -a] - if(!strcmp(argv[1], "-A")) { if(argc < 3) @@ -498,7 +524,7 @@ } - /* test if login exist */ + /* test if login not exist */ if (pswd_struct == NULL){ fprintf(stderr,"Error: /etc/passwd - login not exist\n"); exit(EXIT_LOGIN_NOT_EXIST); @@ -530,6 +556,7 @@ del_shadow(login_line); } + } // [option -A] @@ -630,10 +657,11 @@ fprintf(stdout, "\nGroup Informations\n==================\n\n" "Group: %s\n" - "Gid: %d\n\n" + "Gid: %d\n\n" ,grou_struct->gr_name ,grou_struct->gr_gid); } + fclose(fichier); /* test if group exist */ @@ -645,9 +673,9 @@ } // [option -i] if(!strcmp(argv[1], "-g")) - { - if(argc < 4) - usage_add_group(); + { + if(argc < 4) + usage_add_group(); /* Checking if gid exist in /etc/group */ if(verif_gid(atoi(argv[3])) == 1){ @@ -700,6 +728,256 @@ } // option [-G] + if(!strcmp(argv[1], "-c")) + { + if(argc < 4) + usage_change(); + + if(strcmp(argv[2], "passwd")&& + strcmp(argv[2], "home")&& + strcmp(argv[2], "uid")&& + strcmp(argv[2], "gid")&& + strcmp(argv[2], "shell")&& + strcmp(argv[2], "lock")) + usage_change(); + + if(!strcmp(argv[2], "passwd")) + { + FILE *fileread, *filewrite; + + /* Section /etc/passwd */ + fileread = fopen(patch_shadow, "r"); + filewrite = fopen(patch_shadow, "a"); + struct spwd *shad_struct; /* Structur passwd file entry */ + int login_line = 0; /* pour connaître l'emplacement de login */ + + while ((shad_struct = fgetspent(fileread)) != NULL) + { + login_line++; + if (strcmp(shad_struct->sp_namp, argv[3]) == 0){ break; } + } + + /* test if login not exist */ + if (shad_struct == NULL){ + fprintf(stderr,"Error: /etc/shadow - login not exist\n"); + exit(EXIT_LOGIN_NOT_EXIST); + } + + /* if exist */ + if (shad_struct != NULL){ + unsigned long seed[2]; + char salt[] = "$1$xxxxxxxx"; + + const char *const seedchars = + "./0123456789ABCDEFGHIJKLMNOPQRST" + "UVWXYZabcdefghijklmnopqrstuvwxyz"; + int i; + + /* Generate a (not very) random seed. + You should do it better than this... */ + + seed[0] = time(NULL); + seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000); + + /* Turn it into printable characters from `seedchars'x */ + for (i = 0; i < 8; i++) + salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f]; + + + shad_struct->sp_pwdp = crypt(getpass("Password:"), salt); + putspent(shad_struct, filewrite); + endspent(); + del_shadow(login_line); + } + + } // option [-u] {passwd} + + if(!strcmp(argv[2], "home")) + { + if(argc < 5) + usage_change(); + + FILE *fileread, *filewrite; + + /* Section /etc/passwd */ + fileread = fopen(patch_passwd, "r"); + filewrite = fopen(patch_passwd, "a"); + struct passwd *pswd_struct; /* Structur passwd file entry */ + int login_line = 0; /* pour connaître l'emplacement de login */ + + while ((pswd_struct = fgetpwent(fileread)) != NULL) + { + login_line++; + if (strcmp(pswd_struct->pw_name, argv[4]) == 0){ break; } + } + + /* test if login not exist */ + if (pswd_struct == NULL){ + fprintf(stderr,"Error: /etc/passwd - login not exist\n"); + exit(EXIT_LOGIN_NOT_EXIST); + } + /* if exist */ + if (pswd_struct != NULL){ + pswd_struct->pw_dir = argv[3]; + putpwent(pswd_struct, filewrite); + endpwent(); + del_passwd(login_line); + } + + } // option [-u] {home} + + if(!strcmp(argv[2], "uid")) + { + if(argc < 5) + usage_change(); + + FILE *fileread, *filewrite; + + /* Section /etc/passwd */ + fileread = fopen(patch_passwd, "r"); + filewrite = fopen(patch_passwd, "a"); + struct passwd *pswd_struct; /* Structur passwd file entry */ + int login_line = 0; /* pour connaître l'emplacement de login */ + + /* Checking if uid exist in /etc/passwd */ + if(verif_uid(atoi(argv[3])) == 1){ + fprintf(stderr,"Error: Uid exist, please change it.\n"); + exit(EXIT_UID); + } + + while ((pswd_struct = fgetpwent(fileread)) != NULL) + { + login_line++; + if (strcmp(pswd_struct->pw_name, argv[4]) == 0){ break; } + } + + /* test if login not exist */ + if (pswd_struct == NULL){ + fprintf(stderr,"Error: /etc/passwd - login not exist\n"); + exit(EXIT_LOGIN_NOT_EXIST); + } + /* if exist */ + if (pswd_struct != NULL){ + pswd_struct->pw_uid = atoi(argv[3]); + putpwent(pswd_struct, filewrite); + endpwent(); + del_passwd(login_line); + } + } // option [-u] {uid} + + if(!strcmp(argv[2], "gid")) + { + if(argc < 5) + usage_change(); + + FILE *fileread, *filewrite; + + /* Section /etc/passwd */ + fileread = fopen(patch_passwd, "r"); + filewrite = fopen(patch_passwd, "a"); + struct passwd *pswd_struct; /* Structur passwd file entry */ + int login_line = 0; /* pour connaître l'emplacement de login */ + + /* Checking if gid not exist in /etc/group */ + if(verif_gid(atoi(argv[3])) == 0){ + fprintf(stderr,"Error: Gid not exist, please change it.\n"); + exit(EXIT_GID_NOT_EXIST); + } + + if(!atoi(argv[3]) > 0){ + fprintf(stderr,"Error: Gid not valide\n"); + exit(EXIT_GID_ERROR); + } + + while ((pswd_struct = fgetpwent(fileread)) != NULL) + { + login_line++; + if (strcmp(pswd_struct->pw_name, argv[4]) == 0){ break; } + } + + /* test if login not exist */ + if (pswd_struct == NULL){ + fprintf(stderr,"Error: /etc/passwd - login not exist\n"); + exit(EXIT_LOGIN_NOT_EXIST); + } + /* if exist */ + if (pswd_struct != NULL){ + pswd_struct->pw_gid = atoi(argv[3]); + putpwent(pswd_struct, filewrite); + endpwent(); + del_passwd(login_line); + } + } // option [-u] {gid} + + if(!strcmp(argv[2], "shell")) + { + if(argc < 5) + usage_change(); + + FILE *fileread, *filewrite; + + /* Section /etc/passwd */ + fileread = fopen(patch_passwd, "r"); + filewrite = fopen(patch_passwd, "a"); + struct passwd *pswd_struct; /* Structur passwd file entry */ + int login_line = 0; /* pour connaître l'emplacement de login */ + + while ((pswd_struct = fgetpwent(fileread)) != NULL) + { + login_line++; + if (strcmp(pswd_struct->pw_name, argv[4]) == 0){ break; } + } + + /* test if login not exist */ + if (pswd_struct == NULL){ + fprintf(stderr,"Error: /etc/passwd - login not exist\n"); + exit(EXIT_LOGIN_NOT_EXIST); + } + + /* if exist */ + if (pswd_struct != NULL){ + pswd_struct->pw_shell = argv[3]; + putpwent(pswd_struct, filewrite); + endpwent(); + del_passwd(login_line); + } + } // option [-u] {shell} + + if(!strcmp(argv[2], "lock")) + { + + FILE *fileread, *filewrite; + + /* Section /etc/shadow */ + fileread = fopen(patch_shadow, "r"); + filewrite = fopen(patch_shadow, "a"); + struct spwd *shad_struct; /* Structur passwd file entry */ + int login_line = 0; /* pour connaître l'emplacement de login */ + + while ((shad_struct = fgetspent(fileread)) != NULL) + { + login_line++; + if (strcmp(shad_struct->sp_namp, argv[3]) == 0){ break; } + } + + /* test if login not exist */ + if (shad_struct == NULL){ + fprintf(stderr,"Error: /etc/shadow - login not exist\n"); + exit(EXIT_LOGIN_NOT_EXIST); + } + + /* if exist */ + if (shad_struct != NULL){ + shad_struct->sp_pwdp = "!"; + putspent(shad_struct, filewrite); + endspent(); + del_shadow(login_line); + } + + } // option [-u] {lock} + + } // option [-u] + /* Version informations*/ if(!strcmp(argv[1], "-v")) version();