pastebin - collaborative debugging tool
eckelmann.kpaste.net RSS


authentification example
Posted by Anonymous on Wed 20th Mar 2019 12:52
raw | new post
modification of post by Anonymous (view diff)

  1. /******************************************************************************
  2. * (C) 2019 ECKELMANN AG INDUSTRIEAUTOMATION, WIESBADEN
  3. *******************************************************************************
  4. *
  5. * Author  schenk, ECKELMANN AG
  6. * Date    20.02.2019
  7. *
  8. ******************************************************************************/
  9.  
  10.  
  11. #include <sys/types.h>
  12. #include <pwd.h>
  13. #include <shadow.h>
  14. #include <crypt.h>
  15. #include <string.h>
  16. #include <stdio.h>
  17.  
  18. #define ERR_NO_ERROR 0
  19. #define ERR_PASSWORD_MISMATCH 1
  20. #define ERR_USER_DOES_NOT_EXIST 2
  21. #define ERR_NO_PERMISSION 3
  22. #define ERR_UNKNOWN 4
  23.  
  24.  
  25. int system_authentificate_user (const char*username, const char*password)
  26. {
  27.     struct passwd*pw;
  28.     struct spwd*sp;
  29.     char*encrypted, *theGoodHash;
  30.     int result=ERR_UNKNOWN;
  31.  
  32.     pw = getpwnam (username);
  33.     endpwent();
  34.  
  35.     if (!pw) return ERR_USER_DOES_NOT_EXIST;
  36.  
  37.     sp = getspnam (pw->pw_name);
  38.     endspent();
  39.     if (sp)
  40.         theGoodHash = sp->sp_pwdp;
  41.     else
  42.         theGoodHash = pw->pw_passwd;
  43.  
  44.     if (NULL == theGoodHash){
  45.         return ERR_NO_PERMISSION;
  46.     }
  47.  
  48.     encrypted = crypt (password, theGoodHash);
  49.  
  50.     if( 0 == strcmp (encrypted, theGoodHash)){
  51.         result = ERR_NO_ERROR;
  52.     }
  53.     else{
  54.         result = ERR_PASSWORD_MISMATCH;
  55.     }
  56.     return result;
  57. }
  58.  
  59.  
  60. int main(int argc, char *argv[])
  61. {
  62.     int result;
  63.  
  64.     if( argc < 3){
  65.         printf("prog <USER> <PASSWORD>\n");
  66.         return 3;
  67.     }
  68.  
  69.     const char* user= argv[1];
  70.     const char* pw= argv[2];
  71.  
  72.     result = system_authentificate_user( user, pw);
  73.  
  74.     if( result == ERR_NO_ERROR ){
  75.         printf("password for %s ist valid.\n", user);
  76.     }
  77.  
  78.     if( result == ERR_PASSWORD_MISMATCH){
  79.         printf("password for %s ist invalid.\n", user);
  80.     }
  81.  
  82.     if( result > ERR_PASSWORD_MISMATCH ){
  83.         printf("Error authentificate user %s.\n", user);
  84.     }
  85.  
  86.     return result;
  87.  
  88. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at