Skip to content
Snippets Groups Projects
Commit 15c4154a authored by Nicolas Fernandes's avatar Nicolas Fernandes
Browse files

question 9

parent 2e0fdc9a
No related branches found
No related tags found
No related merge requests found
pwg: pwg.c
gcc -Wall -Wextra -D_XOPEN_SOURCE=500 -g pwg.c check_pass.c -o pwg -lcrypt
#include<stdio.h>
#include <unistd.h>
int removeFile(char * f){
if (remove(f) == 0){
return 0;
}
else
{
printf("Impossible de supprimer le fichier\n");
perror("Erreur");
return 1;
}
}
int insertNewPaswd(int fisrtPwd, char* username, char* newPwd,FILE* temp){
do
{
printf("saisir votre nouveau mot de passe: ");
fgets(newPwd, 20, stdin);
newPwd[strcspn(newPwd, "\n")] = 0;
if(strlen(newPwd)<1)
printf("le mot de passe doit faire au moins 1 caractere \n");
} while (strlen(newPwd)<1);
if(fisrtPwd==1)
fputs(strcat(username,"\n"), temp);
// l'enregistrer en chiffrant
// encrypt(newPwd, pwdCrypted);
//TODO : crypt new pwd
fputs(strcat(newPwd,"\n"), temp);
printf("new mdp: %s", newPwd);
return 0;
}
#ifndef PWG_H_
#define PWG_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PASSWORD_MAX_SIZE 1024
#define BUFFER_MAX_SIZE 1024
#define PATH_TEMP_FILE "/etc/admini/temp"
#define PATH_PWD_FILE "/etc/admini/passwd"
int removeFile(char * f);
int insertNewPaswd(int fisrtPwd, char *username, char *newPwd, FILE *temp);
#endif
nicolasfernandes
kk
File added
#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <grp.h>
#include "check_pass.h"
char* encrypter(char *password) {
unsigned long seed[2];
char salt[] = "$1$ierfdjef";
const char *const seedchars =
"./0123456789ABCDEFGHIJKLMNOPQRST"
"UVWXYZabcdefghijklmnopqrstuvwxyz";
int i;
seed[0] = time(NULL);
seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);
for (i = 0; i < 8; i++)
salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];
crypt(password, salt);
return password;
}
void swapPwdFile(){
//remove /etc/admini/passwd
removeFile(PATH_PWD_FILE);
//rename temp in /etc/admini/temp
rename(PATH_TEMP_FILE,PATH_PWD_FILE);
}
int main(int argc, char *argv[])
{
char * username = getenv("USER");
char str[PASSWORD_MAX_SIZE];
FILE* f = fopen(PATH_PWD_FILE, "r");
FILE* temp = fopen(PATH_TEMP_FILE, "w");
char *oldPwd = NULL;
char str2[BUFFER_MAX_SIZE];
char newPwd[PASSWORD_MAX_SIZE];
int firstPwd = 1;
if(f==NULL){
printf("Impossible d'acceder au passwords");
exit(1);
}
// verifier si l'uilisateur a un mdp
while (fgets(str, 20, f) != NULL )
{
fputs(str, temp);
//on retire le caractere \n de fgets
str[strcspn(str, "\n")] = 0;
if(strcmp(username,str)==0){
firstPwd = 0;
fgets(str, 20, f);
str[strcspn(str, "\n")] = 0;
oldPwd = str;
if(oldPwd!=NULL){
//TODO : compare crypt pwd
do
{
printf("saisir votre ancien mot de passe: ");
fgets(str2, 20, stdin);
str2[strcspn(str2, "\n")] = 0;
if (strcmp(oldPwd, str2) != 0)
printf("mot de passe incorrect\n");
} while (strcmp(oldPwd, str2) != 0);
}
insertNewPaswd(firstPwd, username, newPwd, temp);
}
}
if(firstPwd==1)
insertNewPaswd(firstPwd, username, newPwd, temp);
swapPwdFile();
fclose(f);
return 0;
}
File added
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <grp.h>
#include <sys/types.h>
#include <sys/stat.h>
int sameGroup(int gid, gid_t groups[], int ngroups){
int i;
printf("%o",ngroups);
for (int i = 0; i < ngroups; i++){
printf("fuck");
if (groups[i] == gid)
return 0;
}
return -1;
}
int check_groupID(char pathname, char name) {
struct stat buf;
int userGID;
int ngroups = 0;
lstat(pathname, &buf);
userGID = getegid();
getgrouplist(name, userGID, NULL, &ngroups);
gid_t groups[ngroups];
getgrouplist(name, userGID, groups, &ngroups);
return sameGroup(buf.st_gid, groups, ngroups);
}
int main(int argc, char *argv[])
{
// nom du fichier a effacer
char* file = argv[1];
char str[20];
// verifier que l'utilisateur et du meme groupe que le fichier
char * username = getenv("USER");
FILE* f;
// if(check_groupID(file, username)==-1){
// printf("Vous n'avez pas les droits pour supprimer ce fichier !");
// } else {
//printf("veuillez entrer votre mot de passe : ");
// demander a l'utilisateur son mdp
//fgets(str, 20, stdin);
//verifi du mdp
f = fopen("test.txt", "r");
if(f==NULL){
printf("Impossible d'acceder au PWD");
exit(1);
}
char *oldPwd = NULL;
while (fgets(str, 20, f) != NULL )
{
//on retire le caractere \n de fgets
str[strcspn(str, "\n")] = 0;
if(strcmp("nico",str)==0){
oldPwd = str;
break;
}
}
if(oldPwd==NULL){
printf("pas trouvé");
} else {
printf("saisir votre ancien mot de passe: ");
fgets(str, 20, stdin);
}
fclose(f);
// supprimer le fichier
// appel a check_pass.c
// }
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment