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