pam-eid/authorized_eid.c

61 lines
1.1 KiB
C
Raw Permalink Normal View History

2024-10-29 13:39:15 +01:00
/*
This software is licensed under GPL-3, as shown in the file LICENSE
Author: Linux Gruppe IRB
Copyright: Linux Gruppe IRB, 2024
*/
#include <sys/stat.h>
#include <dirent.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include "authorized_eid.h"
static int auth_dir(struct passwd *passwd, char path[PATH_MAX], const char *mode)
{
if (!passwd || !passwd->pw_dir)
return 0;
snprintf(path, PATH_MAX, "%s/.eid",
passwd->pw_dir);
if (mode[0] != 'r')
{
DIR *dir = opendir(path);
if (dir)
{
/* Directory exists. */
closedir(dir);
}
else if (errno == ENOENT)
{
if (mkdir(path, 0700) != 0)
{
printf("mkdir() failed: %s\n", strerror(errno));
return 0;
}
}
}
return 1;
}
static int auth_file(struct passwd *passwd, char path[PATH_MAX], const char *mode)
{
if (auth_dir(passwd, path, mode) != 1)
return 0;
strcat(path, "/authorized_eid");
return 1;
}
FILE *auth_fopen(struct passwd *passwd, const char *mode)
{
char path[PATH_MAX];
if (auth_file(passwd, path, mode) != 1)
return NULL;
return fopen(path, mode);
}