Skip to content
Snippets Groups Projects
Commit 0780f169 authored by Joel Muaka Nsilulu's avatar Joel Muaka Nsilulu
Browse files

q9

parent 9b4c5c30
Branches master
No related tags found
No related merge requests found
......@@ -9,7 +9,6 @@ int check_pass(char *filename, uid_t user_id, char *password)
{
FILE *f;
char *line = NULL;
char delim[] = ":";
char *pwd;
char *uid;
size_t len = 0;
......@@ -18,8 +17,8 @@ int check_pass(char *filename, uid_t user_id, char *password)
{
while (getline(&line, &len, f) != -1)
{
uid = strtok(line, delim);
pwd = strtok(NULL, delim);
uid = strtok(line, ":");
pwd = strtok(NULL, ":");
if (atoi(uid) == user_id)
{
fclose(f);
......
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pwd.h>
#define SIZE 32
int main(int argc, char *argv[])
{
FILE *fsrc, *fdst;
char *lsrc = NULL;
char *ldst = NULL;
char *pwd;
char *uid;
char not_found = 1;
size_t len = 0;
ssize_t size;
char new_pwd[SIZE];
char old_pwd[SIZE];
uid_t user_id = getuid();
if ((fsrc = fopen("/home/admin/passwd", "r")) != NULL)
{
while (not_found && (size = getline(&lsrc, &len, fsrc)) != -1)
{
uid = strtok(lsrc, ":");
pwd = strtok(NULL, ":");
if (atoi(uid) == user_id)
not_found = 0;
}
fclose(fsrc);
}
if (size != -1)
{
printf("Enter password: ");
fgets(old_pwd, SIZE, stdin);
printf("%s\n", old_pwd);
if (strcmp(pwd, crypt(old_pwd, "salt")))
{
fprintf(stderr, "Bad password\n");
free(lsrc);
return EXIT_FAILURE;
}
printf("Enter the new password: ");
}
else
{
printf("Enter password: ");
}
fgets(new_pwd, SIZE, stdin);
printf("%s\n", new_pwd);
if ((fsrc = fopen("/home/admin/passwd", "r")) != NULL && (fdst = fopen("/home/admin/passwd.tmp", "a")) != NULL)
{
while (getline(&ldst, &len, fsrc) != -1)
{
char *a = strtok(ldst, ":");
char *b = strtok(NULL, ":");
if (atoi(a) != user_id)
fprintf(fdst, "%s:%s:\n", a, b);
}
fprintf(fdst, "%d:%s:\n", user_id, crypt(new_pwd, "salt"));
free(ldst);
fclose(fsrc);
fclose(fdst);
}
printf("\n");
remove("/home/admin/passwd");
rename("/home/admin/passwd.tmp", "/home/admin/passwd");
return EXIT_SUCCESS;
}
......@@ -73,7 +73,7 @@ Pour cette question, on peut se servir de la précédente pour créer les utilis
## Question 9
Le programme et les scripts dans le repertoire *question9*.
Pour cette question, on ajoute aux données de la question précédente le fichier **pwg.c** qqui est une amélioration du fichier **rmg.c**.
## Question 10
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment