Advertisement
Guest User

Untitled

a guest
May 5th, 2017
594
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 25.09 KB | None | 0 0
  1. /*
  2.  * Project: gnuser (Gnu Users Manager)
  3.  * File: gnuser.c
  4.  * Version: 1.4.0
  5.  * Copyright (c) 2009 - Jonathan Salwan <jonathan.salwan@gnuser.org>
  6.  *
  7.  * All rights reserved.
  8.  *
  9.  * English:
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions
  12.  * are met:
  13.  * 1. Redistributions of source code must retain the above copyright
  14.  *    notice, this list of conditions and the following disclaimer.
  15.  * 2. Redistributions in binary form must reproduce the above copyright
  16.  *    notice, this list of conditions and the following disclaimer in the
  17.  *    documentation and/or other materials provided with the distribution.
  18.  *
  19.  *
  20.  * French:
  21.  * La redistribution et l'utilisation sous forme source et binaire,
  22.  * avec ou sans modification, sont autorisées à condition que les
  23.  * conditions suivantes soient remplies:
  24.  * 1. Les redistributions du code source doivent contenir la notice
  25.  *    de copyright ci-dessus, cette liste de conditions et la clause
  26.  *    de non responsabilité.
  27.  * 2. Les redistributions sous forme binaire doivent reproduire la
  28.  *    notice de copyright ci-dessus, cette liste de conditions et la
  29.  *    clause de non responsabilité dans la documentation et/ou d'autres
  30.  *    documents fournis avec la distribution.
  31.  *
  32.  */
  33.  
  34. #include <pwd.h>
  35. #include <grp.h>
  36. #include <stdio.h>
  37. #include <crypt.h>
  38. #include <stdlib.h>
  39. #include <unistd.h>
  40. #include <shadow.h>
  41. #include <getopt.h>
  42. #include <sys/types.h>
  43.  
  44. /* Files Patach Values */
  45. #define patch_passwd "/etc/passwd"
  46. #define patch_shadow "/etc/shadow"
  47. #define patch_group  "/etc/group"
  48.  
  49. /* Exit Status Values */
  50. #define EXIT_SUCCESS        0   /* success */
  51. #define EXIT_USAGE      1   /* invalid command syntax */
  52. #define EXIT_ROOT       2   /* Not root  */
  53. #define EXIT_UID        3   /* UID exist */
  54. #define EXIT_GID        4   /* GID exist */
  55. #define EXIT_GID_NOT_EXIST  5   /* GID not exist */
  56. #define EXIT_GID_ERROR      6   /* GID error */
  57. #define EXIT_LOGIN      7   /* Login name exist */
  58. #define EXIT_GROUP      8   /* Group name exist */
  59. #define EXIT_LOGIN_NOT_EXIST    9   /* Login name not exist */
  60. #define EXIT_GROUP_NOT_EXIST    10  /* Group name not exist */
  61.  
  62. /* local function prototypes */
  63. static void root(void);
  64.  
  65. static void usage(void);
  66. static void usage_add_user(void);
  67. static void usage_del_user(void);
  68. static void usage_show(void);
  69. static void usage_group_info(void);
  70. static void usage_add_group(void);
  71. static void usage_dell_group(void);
  72.  
  73. static int del_passwd(int);
  74. static int del_shadow(int);
  75. static int del_group(int);
  76. static int verif_uid(int);
  77. static int verif_name(char *);
  78. static int verif_gid(int);
  79. static int verif_grname(char *);
  80.  
  81.  
  82. static void root(void)
  83. {
  84.     if(getuid() != 0)
  85.     {
  86.         fprintf(stderr,"Error: You need privileges root\n");
  87.         exit(EXIT_ROOT);
  88.     }
  89. }
  90.  
  91. static void usage(void)
  92. {
  93.     fprintf(stderr,
  94.             "gnuser: invalid option\n"
  95.             "Usage: gnuser [option] <login>\n\n"
  96.             "Options:\n"
  97.             "-a     Add a new user\n"
  98.             "-A     Delete user\n"
  99.             "-s     Show user informations\n"
  100.             "-i     Group informations\n"
  101.             "-g     Add a new group\n"
  102.             "-G     Delete group\n"
  103.             "-c     Change account informations\n" 
  104.             "-v     Version informations\n");
  105.    
  106. exit(EXIT_USAGE);
  107. }
  108.  
  109. static void usage_add_user(void)
  110. {
  111.         fprintf(stderr,
  112.             "gnuser: invalid option for add user\n"
  113.                 "Usage: gnuser -a <login> <uid> <gid> <home_dir>\n\n"
  114.            
  115.             "Exemple: gnuser -a jonathan 1001 1001 /home/jonathan\n\n");
  116.  
  117. exit(EXIT_USAGE);
  118. }
  119.  
  120. static void usage_del_user(void)
  121. {
  122.         fprintf(stderr,
  123.             "gnuser: invalid option for del user\n"
  124.                 "Usage: gnuser -A <login>\n\n"
  125.            
  126.             "Exemple: gnuser -A jonathan\n\n");
  127.  
  128. exit(EXIT_USAGE);
  129. }
  130.  
  131. static void usage_show(void)
  132. {
  133.         fprintf(stderr,
  134.             "gnuser: invalid option for show user\n"
  135.                 "Usage: gnuser -s <login>\n\n"
  136.            
  137.             "Exemple: gnuser -s jonathan\n\n");
  138.  
  139. exit(EXIT_USAGE);
  140. }
  141.  
  142. static void usage_group_info(void)
  143. {
  144.         fprintf(stderr,
  145.             "gnuser: invalid option for informations group\n"
  146.                 "Usage: gnuser -i <group>\n\n"
  147.            
  148.             "Exemple: gnuser -i daemon\n\n");
  149.  
  150. exit(EXIT_USAGE);
  151. }
  152.  
  153. static void usage_add_group(void)
  154. {
  155.         fprintf(stderr,
  156.             "gnuser: invalid option for add a new group\n"
  157.                 "Usage: gnuser -g <group> <gid>\n\n"
  158.            
  159.             "Exemple: gnuser -g newgroup 501\n\n");
  160.  
  161. exit(EXIT_USAGE);
  162. }
  163.  
  164. static void usage_del_group(void)
  165. {
  166.         fprintf(stderr,
  167.             "gnuser: invalid option for delete group\n"
  168.                 "Usage: gnuser -G <group>\n\n"
  169.            
  170.             "Exemple: gnuser -G newgroup\n\n");
  171.  
  172. exit(EXIT_USAGE);
  173. }
  174.  
  175. static void usage_change(void)
  176. {
  177.         fprintf(stderr,
  178.             "gnuser: invalid option for change account informations\n"
  179.                 "Usage: gnuser -c [option] {arg_option} <login>\n\n"
  180.            
  181.             "Option:\n"
  182.             "=======\n"
  183.             "passwd     Change password\n"
  184.             "home       Change home directory\n"
  185.             "uid        Change uid\n"
  186.             "gid        Change gid\n"
  187.             "shell      Change shell\n"
  188.             "lock       Lock account\n\n"
  189.  
  190.             "Exemple:   gnuser -c uid 1002 jonathan\n"
  191.             "========   gnuser -c passwd jonathan\n"
  192.             "       gnuser -c home /home/jonathan2 jonathan\n"
  193.             "       gnuser -c lock jonathan\n");
  194.  
  195. exit(EXIT_USAGE);
  196. }
  197.  
  198. static void version(void)
  199. {
  200.         fprintf(stdout,
  201.             "gnuser (Gnu User Manager) - v1.4.0\n"
  202.             "GPL Licence\n");
  203.  
  204. exit(EXIT_SUCCESS);
  205. }
  206.  
  207. static int del_passwd(int login_line)
  208. {
  209.     FILE *fp1, *fp2;
  210.     char *line = NULL;
  211.     size_t len = 0;
  212.     ssize_t read;
  213.  
  214.     fp1 = fopen(patch_passwd, "r");
  215.     fp2 = fopen("/etc/passwd2", "w");
  216.  
  217.     int cpt = 0;
  218.     int line_rease = login_line;   
  219.  
  220.     /* On recopie le fichier /etc/passwd dans /etc/passwd2 sans la ligne du login à effacer */
  221.     while ((read = getline(&line, &len, fp1)) != -1) {
  222.         cpt++;
  223.         if(cpt != line_rease)
  224.         fprintf(fp2,"%s", line);
  225.         }
  226.     fclose(fp1);
  227.     fclose(fp2);
  228.  
  229.     fp1 = fopen(patch_passwd, "w");
  230.     fp2 = fopen("/etc/passwd2", "r");
  231.  
  232. while ((read = getline(&line, &len, fp2)) != -1) {
  233.         fprintf(fp1,"%s", line);
  234.         }  
  235.     fclose(fp1);
  236.     fclose(fp2);
  237.  
  238.         unlink("/etc/passwd2");
  239.  
  240. return 0;
  241. }
  242.  
  243.  
  244. static int del_shadow(int login_line)
  245. {
  246.     FILE *fp1, *fp2;
  247.     char *line = NULL;
  248.     size_t len = 0;
  249.     ssize_t read;
  250.  
  251.     fp1 = fopen(patch_shadow, "r");
  252.     fp2 = fopen("/etc/shadow2", "w");  
  253.        
  254.  
  255.     int cpt = 0;
  256.     int line_rease = login_line;   
  257.  
  258.     /* On recopie le fichier /etc/shadow dans /etc/shadow2 sans la ligne du login à effacer */
  259.         while ((read = getline(&line, &len, fp1)) != -1) {
  260.         cpt++;
  261.         if(cpt != line_rease)
  262.         fprintf(fp2,"%s", line);
  263.         }
  264.     fclose(fp1);
  265.     fclose(fp2);
  266.  
  267.     fp1 = fopen(patch_shadow, "w");
  268.     fp2 = fopen("/etc/shadow2", "r");
  269.  
  270. while ((read = getline(&line, &len, fp2)) != -1) {
  271.         fprintf(fp1,"%s", line);
  272.         }  
  273.     fclose(fp1);
  274.     fclose(fp2);
  275.  
  276.         unlink("/etc/shadow2");
  277.  
  278. return 0;
  279. }
  280.  
  281. static int del_group(int group_line)
  282. {
  283.     FILE *fp1, *fp2;
  284.     char *line = NULL;
  285.     size_t len = 0;
  286.     ssize_t read;
  287.  
  288.     fp1 = fopen(patch_group, "r");
  289.     fp2 = fopen("/etc/group2", "w");   
  290.        
  291.  
  292.     int cpt = 0;
  293.     int line_rease = group_line;   
  294.  
  295.     /* On recopie le fichier /etc/group dans /etc/group2 sans la ligne du login à effacer */
  296.         while ((read = getline(&line, &len, fp1)) != -1) {
  297.         cpt++;
  298.         if(cpt != line_rease)
  299.         fprintf(fp2,"%s", line);
  300.         }  
  301.     fclose(fp1);
  302.     fclose(fp2);
  303.  
  304.  
  305.     /* On remplace /etc/group2 par /etc/group puis on supprimer /etc/group2 */
  306.         unlink(patch_group);
  307.  
  308.     fp1 = fopen(patch_group, "w");
  309.     fp2 = fopen("/etc/group2", "r");
  310.  
  311. while ((read = getline(&line, &len, fp2)) != -1) {
  312.         fprintf(fp1,"%s", line);
  313.         }  
  314.     fclose(fp1);
  315.     fclose(fp2);
  316.  
  317.         unlink("/etc/group2");
  318.  
  319. return 0;
  320. }
  321.  
  322. static int verif_uid(int idlogin)
  323. {
  324.  
  325.     FILE *fileread;
  326.  
  327.     fileread  = fopen(patch_passwd, "r");
  328.     struct passwd *pswd_struct; /* Structur passwd file entry  */
  329.  
  330.     while ((pswd_struct = fgetpwent(fileread)) != NULL)
  331.         {          
  332.         if (pswd_struct->pw_uid == idlogin){ break; }
  333.             }
  334.  
  335.         /* test if entry exist */
  336.         if (pswd_struct == NULL)
  337.             {return 0;}
  338.             else
  339.             {return 1;}
  340. }
  341.  
  342. static int verif_name(char *namelogin)
  343. {
  344.  
  345.     FILE *fileread;
  346.  
  347.     fileread  = fopen(patch_passwd, "r");
  348.     struct passwd *pswd_struct; /* Structur passwd file entry  */
  349.  
  350.     while ((pswd_struct = fgetpwent(fileread)) != NULL)
  351.         {          
  352.         if (strcmp(pswd_struct->pw_name, namelogin) == 0){ break; }
  353.             }
  354.  
  355.         /* test if entry exist */
  356.         if (pswd_struct == NULL)
  357.             {return 0;}
  358.             else
  359.             {return 1;}
  360. }
  361.  
  362. static int verif_gid(int gidname)
  363. {
  364.  
  365.     FILE *fileread;
  366.  
  367.     fileread  = fopen(patch_group, "r");
  368.     struct group *group_struct; /* Structur group file entry  */
  369.  
  370.     while ((group_struct = fgetgrent(fileread)) != NULL)
  371.         {          
  372.         if (group_struct->gr_gid == gidname){ break; }
  373.             }
  374.  
  375.         /* test if uid exist */
  376.         if (group_struct == NULL)
  377.             {return 0;}
  378.             else
  379.             {return 1;}
  380. }
  381.  
  382. static int verif_grname(char *grname)
  383. {
  384.  
  385.     FILE *fileread;
  386.  
  387.     fileread  = fopen(patch_group, "r");
  388.     struct group *group_struct; /* Structur group file entry  */
  389.  
  390.     while ((group_struct = fgetgrent(fileread)) != NULL)
  391.         {          
  392.         if (strcmp(group_struct->gr_name, grname) == 0){ break; }
  393.             }
  394.  
  395.         /* test if entry exist */
  396.         if (group_struct == NULL)
  397.             {return 0;}
  398.             else
  399.             {return 1;}
  400. }
  401.  
  402. int main(int argc, char **argv)
  403. {
  404.  
  405.     /* Are you root ? */
  406.     root();
  407.  
  408.     if(argc < 2)
  409.         usage();
  410.    
  411.     if(strcmp(argv[1], "-a")&&
  412.        strcmp(argv[1], "-A")&&
  413.        strcmp(argv[1], "-s")&&
  414.        strcmp(argv[1], "-g")&&
  415.        strcmp(argv[1], "-G")&&
  416.        strcmp(argv[1], "-i")&&
  417.        strcmp(argv[1], "-c")&&
  418.        strcmp(argv[1], "-v"))
  419.         usage();
  420.  
  421.    
  422.     /* Option add user */
  423.     if(!strcmp(argv[1], "-a"))
  424.         {
  425.  
  426.         if(argc < 5)
  427.             usage_add_user();
  428.  
  429.             /* Checking if uid exist in /etc/passwd */
  430.             if(verif_uid(atoi(argv[3])) == 1){
  431.                 fprintf(stderr,"Error: Uid exist, please change it.\n");
  432.                 exit(EXIT_UID);
  433.                 }
  434.  
  435.             /* Checking if gid not exist in /etc/group */
  436.             if(verif_gid(atoi(argv[4])) == 0){
  437.                 fprintf(stderr,"Error: Gid not exist, please change it.\n");
  438.                 exit(EXIT_GID_NOT_EXIST);
  439.                 }
  440.  
  441.             /* Checking if login name exist in /etc/passwd */
  442.             if(verif_name(argv[2]) == 1){
  443.                 fprintf(stderr,"Error: Login name exist, please change it.\n");
  444.                 exit(EXIT_LOGIN);
  445.                 }
  446.                
  447.             FILE *fichier;
  448.  
  449.             /* write in /etc/passwd */
  450.             fichier = fopen(patch_passwd, "a");
  451.             fprintf(fichier,"%s:x:%s:%s::%s:/bin/bash\n", argv[2], argv[3], argv[4], argv[5]);
  452.             fclose(fichier);
  453.  
  454.  
  455.                 unsigned long seed[2];
  456.                     char salt[] = "$1$xxxxxxxx";
  457.                    
  458.                 const char *const seedchars =
  459.                                     "./0123456789ABCDEFGHIJKLMNOPQRST"
  460.                                     "UVWXYZabcdefghijklmnopqrstuvwxyz";
  461.             int i;
  462.      
  463.                 /* Generate a (not very) random seed.
  464.                 You should do it better than this... */
  465.        
  466.             seed[0] = time(NULL);
  467.                 seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
  468.      
  469.                     /* Turn it into printable characters from `seedchars'x */
  470.                     for (i = 0; i < 8; i++)
  471.                         salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
  472.      
  473.             /* write in /etc/shadow */
  474.             fichier = fopen(patch_shadow, "a");
  475.             fprintf(fichier,"%s:%s::0:99999:7:::\n", argv[2], crypt(getpass("Password:"), salt));
  476.             fclose(fichier);
  477.  
  478.         /* create user home dire */
  479.         mkdir(argv[5],0755);
  480.  
  481.         /* attrib privilege on home dir */
  482.         int uid, gid;
  483.         uid = atoi(argv[3]);
  484.         gid = atoi(argv[4]);
  485.  
  486.         chown(argv[5], uid, gid);
  487.         } // [option -a]
  488.  
  489.  
  490.     if(!strcmp(argv[1], "-A"))
  491.         {
  492.             if(argc < 3)
  493.                 usage_del_user();
  494.        
  495.        
  496.         int login_line = 0; /* pour connaitre la ligne où ce situe le login*/
  497.         FILE *fileread;
  498.  
  499.             /* Section /etc/passwd */
  500.             fileread  = fopen(patch_passwd, "r");
  501.             struct passwd   *pswd_struct; /* Structur passwd file entry  */        
  502.  
  503.             /*
  504.             fgetpwent - Lire un fichier de mots de passe.
  505.             La structure passwd est définie dans <pwd.h>
  506.  
  507.             struct passwd {
  508.               char   *pw_name;      Nom d'utilisateur            
  509.               char   *pw_passwd;    Mot de passe                  
  510.               uid_t   pw_uid;       ID de l'utilisateur          
  511.               gid_t   pw_gid;       ID du groupe de l'utilisateur
  512.               char   *pw_gecos;     Nom réel de l'utilisateur    
  513.               char   *pw_dir;       Répertoire de connexion      
  514.               char   *pw_shell;     Programme Shell de connexion  
  515.             };
  516.  
  517.             Boucle qui va comparer pw_name du fichier /etc/passwd au login mis en argument 2
  518.             */
  519.            
  520.             while ((pswd_struct = fgetpwent(fileread)) != NULL)
  521.                 {
  522.                 login_line++;                  
  523.                 if (strcmp(pswd_struct->pw_name, argv[2]) == 0){ break; }
  524.                     }
  525.  
  526.  
  527.                 /* test if login not exist */
  528.                 if (pswd_struct == NULL){
  529.                     fprintf(stderr,"Error: /etc/passwd - login not exist\n");
  530.                         exit(EXIT_LOGIN_NOT_EXIST);
  531.                     }
  532.  
  533.                 if (pswd_struct != NULL){      
  534.                         del_passwd(login_line);                
  535.                     }
  536.  
  537.             /* Section /etc/shadow */
  538.             login_line = 0; /* init login line*/
  539.             fileread  = fopen(patch_shadow, "r");
  540.             struct spwd *shad_struct; /* Structur shadow file entry  */
  541.  
  542.             while ((shad_struct = fgetspent(fileread)) != NULL)
  543.                 {
  544.                 login_line++;                  
  545.                 if (strcmp(shad_struct->sp_namp, argv[2]) == 0){ break; }
  546.                     }
  547.  
  548.  
  549.                 /* test if login exist */
  550.                 if (shad_struct == NULL){
  551.                     fprintf(stderr,"Error: /etc/shadow - login not exist\n");
  552.                         exit(EXIT_LOGIN_NOT_EXIST);
  553.                     }
  554.  
  555.                 if (shad_struct != NULL){      
  556.                         del_shadow(login_line);                
  557.                     }
  558.  
  559.  
  560.         } // [option -A]
  561.  
  562.        
  563.         if(!strcmp(argv[1], "-s"))
  564.         {
  565.             if(argc < 3)
  566.                 usage_show();
  567.  
  568.         struct passwd   *pswd_struct; /* Structur passwd file entry  */
  569.         struct spwd *shad_struct; /* Structur shadow file entry  */
  570.        
  571.  
  572.         /*
  573.         The fgetspent() function, reads and parses the next line from the stream fp,
  574.         which is assumed to have the format of the shadow file.
  575.  
  576.         struct spwd
  577.         {
  578.           char *sp_namp;                 login name
  579.           char *sp_pwdp;                 encrypted password
  580.           sptime sp_lstchg;              date of last change
  581.           sptime sp_min;                 minimum number of days between changes
  582.           sptime sp_max;                 maximum number of days between changes
  583.           sptime sp_warn;                number of days of warning before password expires
  584.           sptime sp_inact;               number of days after password expires until the account becomes unusable.
  585.           sptime sp_expire;              days since 1/1/70 until account expires
  586.           unsigned long sp_flag;         reserved for future use
  587.         };
  588.         */
  589.    
  590.             /* open file /etc/shadow */
  591.             FILE *fichier;
  592.             fichier = fopen(patch_shadow,"r");
  593.        
  594.             /* Boucle qui va chercher le login mis en argv[2] puis prendre les informations qui le concerne */
  595.             while ((shad_struct = fgetspent(fichier)) != NULL)
  596.                     if (strcmp(shad_struct->sp_namp, argv[2]) == 0){
  597.  
  598.                     fprintf(stdout, "\nUser Informations\n=================\n\n"
  599.                             "Login:         %s\n"
  600.                             "Password:  %s\n"
  601.                             ,shad_struct->sp_namp,shad_struct->sp_pwdp);
  602.  
  603.                 }
  604.             fclose(fichier);
  605.  
  606.             /* open file /etc/passwd */
  607.             fichier = fopen(patch_passwd,"r");
  608.        
  609.             /* Boucle qui va chercher le login mis en argv[2] puis prendre les informations qui le concerne */
  610.             while ((pswd_struct = fgetpwent(fichier)) != NULL)
  611.                     if (strcmp(pswd_struct->pw_name, argv[2]) == 0){
  612.  
  613.                     fprintf(stdout, "Uid:       %d\n"
  614.                             "Gid:       %d\n"
  615.                             "Home:      %s\n"
  616.                             "Shell      %s\n\n"
  617.                             ,pswd_struct->pw_uid,pswd_struct->pw_gid
  618.                             ,pswd_struct->pw_dir,pswd_struct->pw_shell);
  619.                 }
  620.                 fclose(fichier);
  621.  
  622.                 /* test if login exist */
  623.                 if (getpwnam(argv[2]) == NULL){
  624.                     fprintf(stderr,"Error: login not exist\n");
  625.                         exit(EXIT_LOGIN_NOT_EXIST);
  626.                     }
  627.  
  628.         } // [option -show]
  629.  
  630.        
  631.  
  632.         if(!strcmp(argv[1], "-i"))
  633.         {
  634.             if(argc < 3)
  635.                 usage_group_info();
  636.    
  637.         struct group    *grou_struct; /* Structur group file entry   */
  638.  
  639.         /*
  640.         fgetgrent() - get group file entry
  641.  
  642.         struct group
  643.         {
  644.             char   *gr_name;        group name
  645.             char   *gr_passwd;      group password
  646.             gid_t   gr_gid;         group ID
  647.             char  **gr_mem;         group members
  648.         };
  649.         */
  650.  
  651.             /* open file /etc/group */
  652.             FILE *fichier;
  653.             fichier = fopen(patch_group,"r");
  654.  
  655.             while ((grou_struct = fgetgrent(fichier)) != NULL)
  656.                     if (strcmp(grou_struct->gr_name, argv[2]) == 0){
  657.  
  658.                     fprintf(stdout, "\nGroup Informations\n==================\n\n"
  659.                             "Group:     %s\n"
  660.                             "Gid:       %d\n\n"                    
  661.                             ,grou_struct->gr_name
  662.                             ,grou_struct->gr_gid);
  663.                 }
  664.            
  665.             fclose(fichier);
  666.  
  667.                 /* test if group exist */
  668.                 if (getgrnam(argv[2]) == NULL){
  669.                     fprintf(stderr,"Error: group not exist\n");
  670.                         exit(EXIT_GROUP_NOT_EXIST);
  671.                     }
  672.  
  673.         } // [option -i]
  674.  
  675.         if(!strcmp(argv[1], "-g"))
  676.         {
  677.             if(argc < 4)
  678.                 usage_add_group();
  679.  
  680.             /* Checking if gid exist in /etc/group */
  681.             if(verif_gid(atoi(argv[3])) == 1){
  682.                 fprintf(stderr,"Error: Gid exist, please change it.\n");
  683.                 exit(EXIT_GID);
  684.                 }
  685.  
  686.             /* Checking if group name exist in /etc/group */
  687.             if(verif_grname(argv[2]) == 1){
  688.                 fprintf(stderr,"Error: Group name exist, please change it.\n");
  689.                 exit(EXIT_GROUP);
  690.                 }
  691.  
  692.             /* open file /etc/group */
  693.             FILE *fichier;
  694.             fichier = fopen(patch_group,"a");
  695.            
  696.             fprintf(fichier,"%s:x:%d:\n", argv[2], atoi(argv[3]));
  697.             fclose(fichier);
  698.  
  699.             } // option [-g]
  700.  
  701.         if(!strcmp(argv[1], "-G"))
  702.             {
  703.                 if(argc < 3)
  704.                     usage_del_group();
  705.  
  706.         int group_line = 0; /* pour connaitre la ligne où ce situe le groupe*/
  707.         FILE *fileread;
  708.  
  709.             fileread  = fopen(patch_group, "r");
  710.             struct group    *group_struct; /* Structur group file entry  */        
  711.            
  712.             while ((group_struct = fgetgrent(fileread)) != NULL)
  713.                 {
  714.                 group_line++;                  
  715.                 if (strcmp(group_struct->gr_name, argv[2]) == 0){ break; }
  716.                     }
  717.  
  718.  
  719.                 /* test if login exist */
  720.                 if (group_struct == NULL){
  721.                     fprintf(stderr,"Error: /etc/group - Group name not exist\n");
  722.                         exit(EXIT_GROUP_NOT_EXIST);
  723.                     }
  724.  
  725.                 if (group_struct != NULL){     
  726.                         del_group(group_line);                 
  727.                     }
  728.  
  729.             } // option [-G]
  730.  
  731.         if(!strcmp(argv[1], "-c"))
  732.         {
  733.             if(argc < 4)
  734.                 usage_change();
  735.  
  736.             if(strcmp(argv[2], "passwd")&&
  737.                strcmp(argv[2], "home")&&
  738.                strcmp(argv[2], "uid")&&
  739.                strcmp(argv[2], "gid")&&
  740.                strcmp(argv[2], "shell")&&
  741.                strcmp(argv[2], "lock"))
  742.                 usage_change();
  743.  
  744.             if(!strcmp(argv[2], "passwd"))
  745.                 {
  746.                     FILE *fileread, *filewrite;
  747.  
  748.                     /* Section /etc/passwd */
  749.                     fileread   = fopen(patch_shadow, "r");
  750.                     filewrite  = fopen(patch_shadow, "a");
  751.                     struct spwd     *shad_struct; /* Structur passwd file entry  */
  752.                     int login_line = 0; /* pour connaître l'emplacement de login */
  753.  
  754.                     while ((shad_struct = fgetspent(fileread)) != NULL)
  755.                         {
  756.                         login_line++;          
  757.                         if (strcmp(shad_struct->sp_namp, argv[3]) == 0){ break; }
  758.                             }
  759.  
  760.                         /* test if login not exist */
  761.                         if (shad_struct == NULL){
  762.                             fprintf(stderr,"Error: /etc/shadow - login not exist\n");
  763.                                 exit(EXIT_LOGIN_NOT_EXIST);
  764.                             }
  765.  
  766.                         /* if exist */
  767.                         if (shad_struct != NULL){
  768.                             unsigned long seed[2];
  769.                                 char salt[] = "$1$xxxxxxxx";
  770.                                
  771.                             const char *const seedchars =
  772.                                             "./0123456789ABCDEFGHIJKLMNOPQRST"
  773.                                             "UVWXYZabcdefghijklmnopqrstuvwxyz";
  774.                             int i;
  775.                  
  776.                                 /* Generate a (not very) random seed.
  777.                             You should do it better than this... */
  778.                        
  779.                             seed[0] = time(NULL);
  780.                                 seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
  781.                  
  782.                                     /* Turn it into printable characters from `seedchars'x */
  783.                                     for (i = 0; i < 8; i++)
  784.                                     salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
  785.        
  786.  
  787.                             shad_struct->sp_pwdp = crypt(getpass("Password:"), salt);
  788.                             putspent(shad_struct, filewrite);
  789.                             endspent();
  790.                             del_shadow(login_line);
  791.                             }
  792.  
  793.                 } // option [-u] {passwd}
  794.  
  795.             if(!strcmp(argv[2], "home"))
  796.                 {
  797.                 if(argc < 5)
  798.                     usage_change();
  799.  
  800.                     FILE *fileread, *filewrite;
  801.  
  802.                     /* Section /etc/passwd */
  803.                     fileread   = fopen(patch_passwd, "r");
  804.                     filewrite  = fopen(patch_passwd, "a");
  805.                     struct passwd   *pswd_struct; /* Structur passwd file entry  */
  806.                     int login_line = 0; /* pour connaître l'emplacement de login */
  807.  
  808.                     while ((pswd_struct = fgetpwent(fileread)) != NULL)
  809.                         {
  810.                         login_line++;          
  811.                         if (strcmp(pswd_struct->pw_name, argv[4]) == 0){ break; }
  812.                             }
  813.  
  814.                         /* test if login not exist */
  815.                         if (pswd_struct == NULL){
  816.                             fprintf(stderr,"Error: /etc/passwd - login not exist\n");
  817.                                 exit(EXIT_LOGIN_NOT_EXIST);
  818.                             }
  819.                         /* if exist */
  820.                         if (pswd_struct != NULL){      
  821.                             pswd_struct->pw_dir = argv[3];
  822.                             putpwent(pswd_struct, filewrite);
  823.                             endpwent();
  824.                             del_passwd(login_line);
  825.                             }
  826.  
  827.                 } // option [-u] {home}
  828.  
  829.             if(!strcmp(argv[2], "uid"))
  830.                 {
  831.                 if(argc < 5)
  832.                     usage_change();
  833.  
  834.                     FILE *fileread, *filewrite;
  835.  
  836.                     /* Section /etc/passwd */
  837.                     fileread   = fopen(patch_passwd, "r");
  838.                     filewrite  = fopen(patch_passwd, "a");
  839.                     struct passwd   *pswd_struct; /* Structur passwd file entry  */
  840.                     int login_line = 0; /* pour connaître l'emplacement de login */
  841.  
  842.                     /* Checking if uid exist in /etc/passwd */
  843.                     if(verif_uid(atoi(argv[3])) == 1){
  844.                         fprintf(stderr,"Error: Uid exist, please change it.\n");
  845.                         exit(EXIT_UID);
  846.                         }
  847.  
  848.                     while ((pswd_struct = fgetpwent(fileread)) != NULL)
  849.                         {
  850.                         login_line++;          
  851.                         if (strcmp(pswd_struct->pw_name, argv[4]) == 0){ break; }
  852.                             }
  853.  
  854.                         /* test if login not exist */
  855.                         if (pswd_struct == NULL){
  856.                             fprintf(stderr,"Error: /etc/passwd - login not exist\n");
  857.                                 exit(EXIT_LOGIN_NOT_EXIST);
  858.                             }
  859.                         /* if exist */
  860.                         if (pswd_struct != NULL){      
  861.                             pswd_struct->pw_uid = atoi(argv[3]);
  862.                             putpwent(pswd_struct, filewrite);
  863.                             endpwent();
  864.                             del_passwd(login_line);
  865.                             }
  866.                 } // option [-u] {uid}
  867.  
  868.             if(!strcmp(argv[2], "gid"))
  869.                 {
  870.                 if(argc < 5)
  871.                     usage_change();
  872.  
  873.                     FILE *fileread, *filewrite;
  874.  
  875.                     /* Section /etc/passwd */
  876.                     fileread   = fopen(patch_passwd, "r");
  877.                     filewrite  = fopen(patch_passwd, "a");
  878.                     struct passwd   *pswd_struct; /* Structur passwd file entry  */
  879.                     int login_line = 0; /* pour connaître l'emplacement de login */
  880.                    
  881.                     /* Checking if gid not exist in /etc/group */
  882.                     if(verif_gid(atoi(argv[3])) == 0){
  883.                         fprintf(stderr,"Error: Gid not exist, please change it.\n");
  884.                         exit(EXIT_GID_NOT_EXIST);
  885.                         }
  886.  
  887.                     if(!atoi(argv[3]) > 0){
  888.                         fprintf(stderr,"Error: Gid not valide\n");
  889.                         exit(EXIT_GID_ERROR);
  890.                         }
  891.  
  892.                     while ((pswd_struct = fgetpwent(fileread)) != NULL)
  893.                         {
  894.                         login_line++;          
  895.                         if (strcmp(pswd_struct->pw_name, argv[4]) == 0){ break; }
  896.                             }
  897.  
  898.                         /* test if login not exist */
  899.                         if (pswd_struct == NULL){
  900.                             fprintf(stderr,"Error: /etc/passwd - login not exist\n");
  901.                                 exit(EXIT_LOGIN_NOT_EXIST);
  902.                             }
  903.                         /* if exist */
  904.                         if (pswd_struct != NULL){      
  905.                             pswd_struct->pw_gid = atoi(argv[3]);
  906.                             putpwent(pswd_struct, filewrite);
  907.                             endpwent();
  908.                             del_passwd(login_line);
  909.                             }
  910.                 } // option [-u] {gid}
  911.  
  912.             if(!strcmp(argv[2], "shell"))
  913.                 {
  914.                 if(argc < 5)
  915.                     usage_change();
  916.  
  917.                     FILE *fileread, *filewrite;
  918.  
  919.                     /* Section /etc/passwd */
  920.                     fileread   = fopen(patch_passwd, "r");
  921.                     filewrite  = fopen(patch_passwd, "a");
  922.                     struct passwd   *pswd_struct; /* Structur passwd file entry  */
  923.                     int login_line = 0; /* pour connaître l'emplacement de login */
  924.  
  925.                     while ((pswd_struct = fgetpwent(fileread)) != NULL)
  926.                         {
  927.                         login_line++;          
  928.                         if (strcmp(pswd_struct->pw_name, argv[4]) == 0){ break; }
  929.                             }
  930.  
  931.                         /* test if login not exist */
  932.                         if (pswd_struct == NULL){
  933.                             fprintf(stderr,"Error: /etc/passwd - login not exist\n");
  934.                                 exit(EXIT_LOGIN_NOT_EXIST);
  935.                             }
  936.  
  937.                         /* if exist */
  938.                         if (pswd_struct != NULL){      
  939.                             pswd_struct->pw_shell = argv[3];
  940.                             putpwent(pswd_struct, filewrite);
  941.                             endpwent();
  942.                             del_passwd(login_line);
  943.                             }
  944.                 } // option [-u] {shell}
  945.  
  946.             if(!strcmp(argv[2], "lock"))
  947.                 {
  948.  
  949.                     FILE *fileread, *filewrite;
  950.  
  951.                     /* Section /etc/shadow */
  952.                     fileread   = fopen(patch_shadow, "r");
  953.                     filewrite  = fopen(patch_shadow, "a");
  954.                     struct spwd     *shad_struct; /* Structur passwd file entry  */
  955.                     int login_line = 0; /* pour connaître l'emplacement de login */
  956.  
  957.                     while ((shad_struct = fgetspent(fileread)) != NULL)
  958.                         {
  959.                         login_line++;          
  960.                         if (strcmp(shad_struct->sp_namp, argv[3]) == 0){ break; }
  961.                             }
  962.  
  963.                         /* test if login not exist */
  964.                         if (shad_struct == NULL){
  965.                             fprintf(stderr,"Error: /etc/shadow - login not exist\n");
  966.                                 exit(EXIT_LOGIN_NOT_EXIST);
  967.                             }
  968.  
  969.                         /* if exist */
  970.                         if (shad_struct != NULL){      
  971.                             shad_struct->sp_pwdp = "!";
  972.                             putspent(shad_struct, filewrite);
  973.                             endspent();
  974.                             del_shadow(login_line);
  975.                             }
  976.                            
  977.                 } // option [-u] {lock}
  978.  
  979.         } // option [-u]
  980.  
  981.         /* Version informations*/
  982.         if(!strcmp(argv[1], "-v"))
  983.             version();
  984.            
  985.  
  986. exit(EXIT_SUCCESS);
  987. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement