Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Project: gnuser (Gnu Users Manager)
- * File: gnuser.c
- * Version: 1.4.0
- * Copyright (c) 2009 - Jonathan Salwan <jonathan.salwan@gnuser.org>
- *
- * All rights reserved.
- *
- * English:
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- *
- * French:
- * La redistribution et l'utilisation sous forme source et binaire,
- * avec ou sans modification, sont autorisées à condition que les
- * conditions suivantes soient remplies:
- * 1. Les redistributions du code source doivent contenir la notice
- * de copyright ci-dessus, cette liste de conditions et la clause
- * de non responsabilité.
- * 2. Les redistributions sous forme binaire doivent reproduire la
- * notice de copyright ci-dessus, cette liste de conditions et la
- * clause de non responsabilité dans la documentation et/ou d'autres
- * documents fournis avec la distribution.
- *
- */
- #include <pwd.h>
- #include <grp.h>
- #include <stdio.h>
- #include <crypt.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <shadow.h>
- #include <getopt.h>
- #include <sys/types.h>
- /* Files Patach Values */
- #define patch_passwd "/etc/passwd"
- #define patch_shadow "/etc/shadow"
- #define patch_group "/etc/group"
- /* Exit Status Values */
- #define EXIT_SUCCESS 0 /* success */
- #define EXIT_USAGE 1 /* invalid command syntax */
- #define EXIT_ROOT 2 /* Not root */
- #define EXIT_UID 3 /* UID exist */
- #define EXIT_GID 4 /* GID 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);
- static void usage(void);
- static void usage_add_user(void);
- static void usage_del_user(void);
- static void usage_show(void);
- static void usage_group_info(void);
- static void usage_add_group(void);
- static void usage_dell_group(void);
- static int del_passwd(int);
- static int del_shadow(int);
- static int del_group(int);
- static int verif_uid(int);
- static int verif_name(char *);
- static int verif_gid(int);
- static int verif_grname(char *);
- static void root(void)
- {
- if(getuid() != 0)
- {
- fprintf(stderr,"Error: You need privileges root\n");
- exit(EXIT_ROOT);
- }
- }
- static void usage(void)
- {
- fprintf(stderr,
- "gnuser: invalid option\n"
- "Usage: gnuser [option] <login>\n\n"
- "Options:\n"
- "-a Add a new user\n"
- "-A Delete user\n"
- "-s Show user informations\n"
- "-i Group informations\n"
- "-g Add a new group\n"
- "-G Delete group\n"
- "-c Change account informations\n"
- "-v Version informations\n");
- exit(EXIT_USAGE);
- }
- static void usage_add_user(void)
- {
- fprintf(stderr,
- "gnuser: invalid option for add user\n"
- "Usage: gnuser -a <login> <uid> <gid> <home_dir>\n\n"
- "Exemple: gnuser -a jonathan 1001 1001 /home/jonathan\n\n");
- exit(EXIT_USAGE);
- }
- static void usage_del_user(void)
- {
- fprintf(stderr,
- "gnuser: invalid option for del user\n"
- "Usage: gnuser -A <login>\n\n"
- "Exemple: gnuser -A jonathan\n\n");
- exit(EXIT_USAGE);
- }
- static void usage_show(void)
- {
- fprintf(stderr,
- "gnuser: invalid option for show user\n"
- "Usage: gnuser -s <login>\n\n"
- "Exemple: gnuser -s jonathan\n\n");
- exit(EXIT_USAGE);
- }
- static void usage_group_info(void)
- {
- fprintf(stderr,
- "gnuser: invalid option for informations group\n"
- "Usage: gnuser -i <group>\n\n"
- "Exemple: gnuser -i daemon\n\n");
- exit(EXIT_USAGE);
- }
- static void usage_add_group(void)
- {
- fprintf(stderr,
- "gnuser: invalid option for add a new group\n"
- "Usage: gnuser -g <group> <gid>\n\n"
- "Exemple: gnuser -g newgroup 501\n\n");
- exit(EXIT_USAGE);
- }
- static void usage_del_group(void)
- {
- fprintf(stderr,
- "gnuser: invalid option for delete group\n"
- "Usage: gnuser -G <group>\n\n"
- "Exemple: gnuser -G newgroup\n\n");
- exit(EXIT_USAGE);
- }
- static void usage_change(void)
- {
- fprintf(stderr,
- "gnuser: invalid option for change account informations\n"
- "Usage: gnuser -c [option] {arg_option} <login>\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.4.0\n"
- "GPL Licence\n");
- exit(EXIT_SUCCESS);
- }
- static int del_passwd(int login_line)
- {
- FILE *fp1, *fp2;
- char *line = NULL;
- size_t len = 0;
- ssize_t read;
- fp1 = fopen(patch_passwd, "r");
- fp2 = fopen("/etc/passwd2", "w");
- int cpt = 0;
- int line_rease = login_line;
- /* On recopie le fichier /etc/passwd dans /etc/passwd2 sans la ligne du login à effacer */
- while ((read = getline(&line, &len, fp1)) != -1) {
- cpt++;
- if(cpt != line_rease)
- fprintf(fp2,"%s", line);
- }
- fclose(fp1);
- fclose(fp2);
- fp1 = fopen(patch_passwd, "w");
- fp2 = fopen("/etc/passwd2", "r");
- while ((read = getline(&line, &len, fp2)) != -1) {
- fprintf(fp1,"%s", line);
- }
- fclose(fp1);
- fclose(fp2);
- unlink("/etc/passwd2");
- return 0;
- }
- static int del_shadow(int login_line)
- {
- FILE *fp1, *fp2;
- char *line = NULL;
- size_t len = 0;
- ssize_t read;
- fp1 = fopen(patch_shadow, "r");
- fp2 = fopen("/etc/shadow2", "w");
- int cpt = 0;
- int line_rease = login_line;
- /* On recopie le fichier /etc/shadow dans /etc/shadow2 sans la ligne du login à effacer */
- while ((read = getline(&line, &len, fp1)) != -1) {
- cpt++;
- if(cpt != line_rease)
- fprintf(fp2,"%s", line);
- }
- fclose(fp1);
- fclose(fp2);
- fp1 = fopen(patch_shadow, "w");
- fp2 = fopen("/etc/shadow2", "r");
- while ((read = getline(&line, &len, fp2)) != -1) {
- fprintf(fp1,"%s", line);
- }
- fclose(fp1);
- fclose(fp2);
- unlink("/etc/shadow2");
- return 0;
- }
- static int del_group(int group_line)
- {
- FILE *fp1, *fp2;
- char *line = NULL;
- size_t len = 0;
- ssize_t read;
- fp1 = fopen(patch_group, "r");
- fp2 = fopen("/etc/group2", "w");
- int cpt = 0;
- int line_rease = group_line;
- /* On recopie le fichier /etc/group dans /etc/group2 sans la ligne du login à effacer */
- while ((read = getline(&line, &len, fp1)) != -1) {
- cpt++;
- if(cpt != line_rease)
- fprintf(fp2,"%s", line);
- }
- fclose(fp1);
- fclose(fp2);
- /* On remplace /etc/group2 par /etc/group puis on supprimer /etc/group2 */
- unlink(patch_group);
- fp1 = fopen(patch_group, "w");
- fp2 = fopen("/etc/group2", "r");
- while ((read = getline(&line, &len, fp2)) != -1) {
- fprintf(fp1,"%s", line);
- }
- fclose(fp1);
- fclose(fp2);
- unlink("/etc/group2");
- return 0;
- }
- static int verif_uid(int idlogin)
- {
- FILE *fileread;
- fileread = fopen(patch_passwd, "r");
- struct passwd *pswd_struct; /* Structur passwd file entry */
- while ((pswd_struct = fgetpwent(fileread)) != NULL)
- {
- if (pswd_struct->pw_uid == idlogin){ break; }
- }
- /* test if entry exist */
- if (pswd_struct == NULL)
- {return 0;}
- else
- {return 1;}
- }
- static int verif_name(char *namelogin)
- {
- FILE *fileread;
- fileread = fopen(patch_passwd, "r");
- struct passwd *pswd_struct; /* Structur passwd file entry */
- while ((pswd_struct = fgetpwent(fileread)) != NULL)
- {
- if (strcmp(pswd_struct->pw_name, namelogin) == 0){ break; }
- }
- /* test if entry exist */
- if (pswd_struct == NULL)
- {return 0;}
- else
- {return 1;}
- }
- static int verif_gid(int gidname)
- {
- FILE *fileread;
- fileread = fopen(patch_group, "r");
- struct group *group_struct; /* Structur group file entry */
- while ((group_struct = fgetgrent(fileread)) != NULL)
- {
- if (group_struct->gr_gid == gidname){ break; }
- }
- /* test if uid exist */
- if (group_struct == NULL)
- {return 0;}
- else
- {return 1;}
- }
- static int verif_grname(char *grname)
- {
- FILE *fileread;
- fileread = fopen(patch_group, "r");
- struct group *group_struct; /* Structur group file entry */
- while ((group_struct = fgetgrent(fileread)) != NULL)
- {
- if (strcmp(group_struct->gr_name, grname) == 0){ break; }
- }
- /* test if entry exist */
- if (group_struct == NULL)
- {return 0;}
- else
- {return 1;}
- }
- int main(int argc, char **argv)
- {
- /* Are you root ? */
- root();
- if(argc < 2)
- usage();
- 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();
- /* Option add user */
- if(!strcmp(argv[1], "-a"))
- {
- if(argc < 5)
- usage_add_user();
- /* 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);
- }
- /* 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");
- exit(EXIT_LOGIN);
- }
- FILE *fichier;
- /* write in /etc/passwd */
- fichier = fopen(patch_passwd, "a");
- fprintf(fichier,"%s:x:%s:%s::%s:/bin/bash\n", argv[2], argv[3], argv[4], argv[5]);
- fclose(fichier);
- 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];
- /* write in /etc/shadow */
- fichier = fopen(patch_shadow, "a");
- fprintf(fichier,"%s:%s::0:99999:7:::\n", argv[2], crypt(getpass("Password:"), salt));
- fclose(fichier);
- /* create user home dire */
- mkdir(argv[5],0755);
- /* attrib privilege on home dir */
- int uid, gid;
- uid = atoi(argv[3]);
- gid = atoi(argv[4]);
- chown(argv[5], uid, gid);
- } // [option -a]
- if(!strcmp(argv[1], "-A"))
- {
- if(argc < 3)
- usage_del_user();
- int login_line = 0; /* pour connaitre la ligne où ce situe le login*/
- FILE *fileread;
- /* Section /etc/passwd */
- fileread = fopen(patch_passwd, "r");
- struct passwd *pswd_struct; /* Structur passwd file entry */
- /*
- fgetpwent - Lire un fichier de mots de passe.
- La structure passwd est définie dans <pwd.h>
- struct passwd {
- char *pw_name; Nom d'utilisateur
- char *pw_passwd; Mot de passe
- uid_t pw_uid; ID de l'utilisateur
- gid_t pw_gid; ID du groupe de l'utilisateur
- char *pw_gecos; Nom réel de l'utilisateur
- char *pw_dir; Répertoire de connexion
- char *pw_shell; Programme Shell de connexion
- };
- Boucle qui va comparer pw_name du fichier /etc/passwd au login mis en argument 2
- */
- while ((pswd_struct = fgetpwent(fileread)) != NULL)
- {
- login_line++;
- if (strcmp(pswd_struct->pw_name, argv[2]) == 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 (pswd_struct != NULL){
- del_passwd(login_line);
- }
- /* Section /etc/shadow */
- login_line = 0; /* init login line*/
- fileread = fopen(patch_shadow, "r");
- struct spwd *shad_struct; /* Structur shadow file entry */
- while ((shad_struct = fgetspent(fileread)) != NULL)
- {
- login_line++;
- if (strcmp(shad_struct->sp_namp, argv[2]) == 0){ break; }
- }
- /* test if login exist */
- if (shad_struct == NULL){
- fprintf(stderr,"Error: /etc/shadow - login not exist\n");
- exit(EXIT_LOGIN_NOT_EXIST);
- }
- if (shad_struct != NULL){
- del_shadow(login_line);
- }
- } // [option -A]
- if(!strcmp(argv[1], "-s"))
- {
- if(argc < 3)
- usage_show();
- struct passwd *pswd_struct; /* Structur passwd file entry */
- struct spwd *shad_struct; /* Structur shadow file entry */
- /*
- The fgetspent() function, reads and parses the next line from the stream fp,
- which is assumed to have the format of the shadow file.
- struct spwd
- {
- char *sp_namp; login name
- char *sp_pwdp; encrypted password
- sptime sp_lstchg; date of last change
- sptime sp_min; minimum number of days between changes
- sptime sp_max; maximum number of days between changes
- sptime sp_warn; number of days of warning before password expires
- sptime sp_inact; number of days after password expires until the account becomes unusable.
- sptime sp_expire; days since 1/1/70 until account expires
- unsigned long sp_flag; reserved for future use
- };
- */
- /* open file /etc/shadow */
- FILE *fichier;
- fichier = fopen(patch_shadow,"r");
- /* Boucle qui va chercher le login mis en argv[2] puis prendre les informations qui le concerne */
- while ((shad_struct = fgetspent(fichier)) != NULL)
- if (strcmp(shad_struct->sp_namp, argv[2]) == 0){
- fprintf(stdout, "\nUser Informations\n=================\n\n"
- "Login: %s\n"
- "Password: %s\n"
- ,shad_struct->sp_namp,shad_struct->sp_pwdp);
- }
- fclose(fichier);
- /* open file /etc/passwd */
- fichier = fopen(patch_passwd,"r");
- /* Boucle qui va chercher le login mis en argv[2] puis prendre les informations qui le concerne */
- while ((pswd_struct = fgetpwent(fichier)) != NULL)
- if (strcmp(pswd_struct->pw_name, argv[2]) == 0){
- fprintf(stdout, "Uid: %d\n"
- "Gid: %d\n"
- "Home: %s\n"
- "Shell %s\n\n"
- ,pswd_struct->pw_uid,pswd_struct->pw_gid
- ,pswd_struct->pw_dir,pswd_struct->pw_shell);
- }
- fclose(fichier);
- /* test if login exist */
- if (getpwnam(argv[2]) == NULL){
- fprintf(stderr,"Error: login not exist\n");
- exit(EXIT_LOGIN_NOT_EXIST);
- }
- } // [option -show]
- if(!strcmp(argv[1], "-i"))
- {
- if(argc < 3)
- usage_group_info();
- struct group *grou_struct; /* Structur group file entry */
- /*
- fgetgrent() - get group file entry
- struct group
- {
- char *gr_name; group name
- char *gr_passwd; group password
- gid_t gr_gid; group ID
- char **gr_mem; group members
- };
- */
- /* open file /etc/group */
- FILE *fichier;
- fichier = fopen(patch_group,"r");
- while ((grou_struct = fgetgrent(fichier)) != NULL)
- if (strcmp(grou_struct->gr_name, argv[2]) == 0){
- fprintf(stdout, "\nGroup Informations\n==================\n\n"
- "Group: %s\n"
- "Gid: %d\n\n"
- ,grou_struct->gr_name
- ,grou_struct->gr_gid);
- }
- fclose(fichier);
- /* test if group exist */
- if (getgrnam(argv[2]) == NULL){
- fprintf(stderr,"Error: group not exist\n");
- exit(EXIT_GROUP_NOT_EXIST);
- }
- } // [option -i]
- if(!strcmp(argv[1], "-g"))
- {
- if(argc < 4)
- usage_add_group();
- /* Checking if gid exist in /etc/group */
- if(verif_gid(atoi(argv[3])) == 1){
- fprintf(stderr,"Error: Gid exist, please change it.\n");
- exit(EXIT_GID);
- }
- /* Checking if group name exist in /etc/group */
- if(verif_grname(argv[2]) == 1){
- fprintf(stderr,"Error: Group name exist, please change it.\n");
- exit(EXIT_GROUP);
- }
- /* open file /etc/group */
- FILE *fichier;
- fichier = fopen(patch_group,"a");
- fprintf(fichier,"%s:x:%d:\n", argv[2], atoi(argv[3]));
- fclose(fichier);
- } // option [-g]
- if(!strcmp(argv[1], "-G"))
- {
- if(argc < 3)
- usage_del_group();
- int group_line = 0; /* pour connaitre la ligne où ce situe le groupe*/
- FILE *fileread;
- fileread = fopen(patch_group, "r");
- struct group *group_struct; /* Structur group file entry */
- while ((group_struct = fgetgrent(fileread)) != NULL)
- {
- group_line++;
- if (strcmp(group_struct->gr_name, argv[2]) == 0){ break; }
- }
- /* test if login exist */
- if (group_struct == NULL){
- fprintf(stderr,"Error: /etc/group - Group name not exist\n");
- exit(EXIT_GROUP_NOT_EXIST);
- }
- if (group_struct != NULL){
- del_group(group_line);
- }
- } // 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();
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement