Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Shows user info from local pwfile.
- *
- * Usage: userinfo username
- */
- #define _XOPEN_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdbool.h>
- #include "pwdblib.h" /* include header declarations for pwdblib.c */
- /* Define some constants. */
- #define USERNAME_SIZE (32)
- #define PASSWORD_SIZE (32)
- #define NOUSER (-1)
- int print_info(const char *username)
- {
- struct pwdb_passwd *p = pwdb_getpwnam(username);
- if (p != NULL) {
- printf("Name: %s\n", p->pw_name);
- printf("Passwd: %s\n", p->pw_passwd);
- printf("Uid: %u\n", p->pw_uid);
- printf("Gid: %u\n", p->pw_gid);
- printf("Real name: %s\n", p->pw_gecos);
- printf("Home dir: %s\n",p->pw_dir);
- printf("Shell: %s\n", p->pw_shell);
- return 0;
- } else {
- return NOUSER;
- }
- }
- void read_username(char *username)
- {
- printf("login: ");
- fgets(username, USERNAME_SIZE, stdin);
- /* remove the newline included by getline() */
- username[strlen(username) - 1] = '\0';
- }
- void read_password(char *password)
- {
- strcpy(password, getpass("Enter password: "));
- }
- void checkPassword(char *realPassword, const char *username)
- {
- struct pwdb_passwd *p = pwdb_getpwnam(username);
- if (p != NULL) {
- strcpy(realPassword, p->pw_passwd);
- }
- }
- void increaseSuccessfulLogin(const char *username){
- struct pwdb_passwd *p = pwdb_getpwnam(username);
- if (p != NULL) {
- int oldValue = p->pw_age;
- printf("old value: %i \n", oldValue);
- int newValue = oldValue + 1;
- if (newValue > 10) {
- printf("Your password is old, it is recommended that you update it! \n");
- }
- p->pw_age = newValue;
- pwdb_update_user(p);
- }
- }
- void increaseFailedLogin(const char *username){
- struct pwdb_passwd *p = pwdb_getpwnam(username);
- if (p != NULL) {
- int oldValue = p->pw_failed;
- int newValue = oldValue + 1;
- p->pw_failed = newValue;
- pwdb_update_user(p);
- }
- }
- void zeroFails(const char *username){
- struct pwdb_passwd *p = pwdb_getpwnam(username);
- if (p != NULL) {
- int newValue = 0;
- p->pw_failed = newValue;
- pwdb_update_user(p);
- }
- }
- bool isLockedOut(const char *username){
- struct pwdb_passwd *p = pwdb_getpwnam(username);
- if (p != NULL) {
- int failedLogins = p->pw_failed;
- return failedLogins >= 5;
- }
- }
- char* concat(const char *s1, const char *s2)
- {
- char *result = malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
- // in real code you would check for errors in malloc here
- strcpy(result, s1);
- strcat(result, s2);
- return result;
- }
- int main(int argc, char **argv)
- {
- while(1) {
- char username[USERNAME_SIZE];
- char password[PASSWORD_SIZE];
- char realPassword[PASSWORD_SIZE];
- /*
- * Write "login: " and read user input. Copies the username to the
- * username variable.
- */
- read_username(username);
- read_password(password);
- if (isLockedOut(username)) {
- printf("Too many unsuccessful login attempts. Please contact admin.\n");
- return 0;
- }
- checkPassword(realPassword, username);
- if (realPassword!=NULL) {
- char salt[2];
- salt[0] = realPassword[0];
- salt[1] = realPassword[1];
- const char *saltConst = salt;
- const char *passwordConst = password;
- const char *cryptedPw = crypt(passwordConst, saltConst);
- const char *saltAndCryptedPassword = concat(salt, cryptedPw); // username changes somewhere in this if-statement!
- if (strcmp(realPassword,cryptedPw) == 0) {
- increaseSuccessfulLogin(username);
- zeroFails(username);
- printf("User authenticated successfully\n");
- return 0;
- }
- }
- increaseFailedLogin(username);
- printf("Unknown user or incorrect password. \n");
- }
- /* Show user info from our local pwfile.
- if (print_info(username) == NOUSER) {
- /* if there are no user with that usename...
- printf("\nFound no user with name: %s\n", username);
- return 0;
- } */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement