Add proper error handling
This commit is contained in:
16
fakeid.c
16
fakeid.c
@@ -1,10 +1,11 @@
|
|||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <pwd.h>
|
|
||||||
|
|
||||||
int getlogin_r(char *buf, size_t bufsize) {
|
int getlogin_r(char *buf, size_t bufsize) {
|
||||||
strncpy(buf, getenv("USER"), bufsize);
|
strncpy(buf, getenv("USER"), bufsize);
|
||||||
@@ -13,7 +14,16 @@ int getlogin_r(char *buf, size_t bufsize) {
|
|||||||
|
|
||||||
struct passwd *getpwuid(uid_t uid) {
|
struct passwd *getpwuid(uid_t uid) {
|
||||||
struct passwd *(*getpwuid)(uid_t) = dlsym(RTLD_NEXT, "getpwuid");
|
struct passwd *(*getpwuid)(uid_t) = dlsym(RTLD_NEXT, "getpwuid");
|
||||||
|
if (getpwuid == NULL) {
|
||||||
|
fprintf(stderr, "Unable to find the adress of the getpwuid function: %s\n", dlerror());
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
struct passwd *pass = getpwuid(uid);
|
struct passwd *pass = getpwuid(uid);
|
||||||
pass->pw_name = getenv("USER");
|
char *name = getenv("USER");
|
||||||
|
if (name == NULL) {
|
||||||
|
fprintf(stderr, "Could not find the USER environment variable. Using the real username.\n");
|
||||||
|
return pass;
|
||||||
|
}
|
||||||
|
pass->pw_name = name;
|
||||||
return pass;
|
return pass;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user