/****************************************************************************** * (C) 2019 ECKELMANN AG INDUSTRIEAUTOMATION, WIESBADEN ******************************************************************************* * * Author schenk, ECKELMANN AG * Date 20.02.2019 * ******************************************************************************/ #include #include #include #include #include #include #define ERR_NO_ERROR 0 #define ERR_PASSWORD_MISMATCH 1 #define ERR_USER_DOES_NOT_EXIST 2 #define ERR_NO_PERMISSION 3 #define ERR_UNKNOWN 4 int system_authentificate_user (const char*username, const char*password) { struct passwd*pw; struct spwd*sp; char*encrypted, *theGoodHash; int result=ERR_UNKNOWN; pw = getpwnam (username); endpwent(); if (!pw) return ERR_USER_DOES_NOT_EXIST; sp = getspnam (pw->pw_name); endspent(); if (sp) theGoodHash = sp->sp_pwdp; else theGoodHash = pw->pw_passwd; if (NULL == theGoodHash){ return ERR_NO_PERMISSION; } encrypted = crypt (password, theGoodHash); if( 0 == strcmp (encrypted, theGoodHash)){ result = ERR_NO_ERROR; } else{ result = ERR_PASSWORD_MISMATCH; } return result; } int main(int argc, char *argv[]) { int result; if( argc < 3){ printf("prog \n"); return 3; } const char* user= argv[1]; const char* pw= argv[2]; result = system_authentificate_user( user, pw); if( result == ERR_NO_ERROR ){ printf("password for %s ist valid.\n", user); } if( result == ERR_PASSWORD_MISMATCH){ printf("password for %s ist invalid.\n", user); } if( result > ERR_PASSWORD_MISMATCH ){ printf("Error authentificate user %s.\n", user); } return result; }