unix - Login simulator in c, dereferencing incomplete types errors -
as preporatory task computer lab in school, asked write c programme simulates login process in unix. programme should read username , password terminal, compare hashed values in local file supposed resemble /etc/passwd.
here's i've got:
/* * programme mylogin.c * * programme prompts user login name , password * */ #define _xopen_source #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <pwd.h> #include <sys/types.h> #include <string.h> /* define error constants */ #define nouser -1 /* define max size of username */ #define username_size 32 #define password_size 32 #define hash_size 32 #define failed_limit 5 #define age_limit 10 int read_username(char *username) { printf("login: "); fgets(username,username_size,stdin); /* remove cr included getline() */ username[strlen(username)-1]='\0'; return(0); } int read_password(char *password) { printf("password: "); fgets(password,password_size,stdin); //getpass(password); /* remove cr included getline() */ password[strlen(password)-1]='\0'; return(0); } int user_exists(const char *username) { struct pwdb_passwd *pw_entry; pw_entry=getpwnam(username); return((pw_entry)!=null); } int main(int argc,char **argv) { char username[username_size]; char* password; /* write "login:" , read user input */ read_username(username); read_password(password); if (!user_exists(username)) { printf("unknown user or authentication\n"); main(argc, argv); } struct pwdb_passwd *pw_entry = getpwnam(username); char* hashed_password = crypt(password,pw_entry->pw_passwd); if(strcmp(hashed_password, pw_entry->pw_passwd)==0) { if((pw_entry->pw_failed)<failed_limit) { printf("user authenticated successfully\n"); pw_entry->pw_age++; pw_entry->pw_failed = 0; pwdb_update_user(pw_entry); }else{ printf("user business relationship locked\n"); main(argc, argv); } } else { printf("unknown user or authentication\n"); pw_entry->pw_failed++; if(pw_entry->pw_failed>5){ printf("too many failed attempts. username locked\n"); } pwdb_update_user(pw_entry); main(argc, argv); } return(0); } the struct pwdb_passwd defined in files pwdb_lib.c , pwdb_lib.h, written.
when compile program, couple of errors. illustration on line 73, get: "error: dereferencing pointer incomplete type"
i don't understand why. doesn't seem pw_entry->pw_passwd , things that. more point, different errors when compiling under windows code::blocks (using gcc) under ubuntu gcc. find pretty strange. suspect because import pwd.h , exists on linux , not windows. right? tried creating own pwd.h file , save in same directory, still didn't work. moving ubuntu computer, dont errors pwd.h thing, instead errors on: "dereferencing pointer incomplete type"
what's wrong code?
i suspect memory leak in user_exists function, i'm not sure if affects overall program.
even though pwdb_lib.c written, need include in source file.
add
#include "pwdb_lib.h" to source , create sure compile/link against pwdb_lib.c
by #includeing file, allow source file know definitions within without providing implementation. @ end, when compile programme pwdb_lib.c (or link object file, if that's you're doing), allow source included these definitions know implemented (and thus, give them ability utilize them).
c unix pointers login
No comments:
Post a Comment