This repository has been archived by the owner on May 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
common.c
114 lines (103 loc) · 3.05 KB
/
common.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <nss.h>
#include <pwd.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include <glib.h>
#include <glib/gprintf.h>
#ifndef SCRIPTPATH
#define SCRIPTPATH "/usr/libexec/aad-auth"
#endif
gint run_aad_auth(const char *db, const char *name, const uid_t uid, enum nss_status *nss_exit_status, int *errnop, GPtrArray *entries)
{
gchar *stdout = NULL;
gchar *stderr = NULL;
GError *error = NULL;
gchar *cmd;
if (name)
{
// Concatenate name with cmd
cmd = g_strconcat(SCRIPTPATH, " ", "getent", " ", db, " ", name, NULL);
}
else if (uid != 0)
{
gchar *uid_s = NULL;
uid_s = g_strdup_printf(" %u", uid);
cmd = g_strconcat(SCRIPTPATH, " ", "getent", " ", db, " ", uid_s, NULL);
g_free(uid_s);
}
else
{
cmd = g_strconcat(SCRIPTPATH, " ", "getent", " ", db, NULL);
}
gint exit_status;
if (!g_spawn_command_line_sync(cmd, &stdout, &stderr, &exit_status, &error) || exit_status != 0)
{
*errnop = ENOENT;
g_free(cmd);
return exit_status;
}
g_free(cmd);
gchar **lines = g_strsplit(stdout, "\n", -1);
for (gint i = 0; lines[i]; i++)
{
// first line is nss_exit_status:errno
if (i == 0)
{
gchar **statuses = g_strsplit(lines[i], ":", 2);
*nss_exit_status = atoi(statuses[0]);
*errnop = atoi(statuses[1]);
g_strfreev(statuses);
continue;
}
gchar *v = g_strdup(lines[i]);
g_ptr_array_add(entries, (gpointer)v);
}
g_strfreev(lines);
return 0;
}
enum nss_status fetch_info(const char *db, const char *name, const uid_t uid, GPtrArray *all_entries, guint *all_entries_index, gchar **entry, int *errnop)
{
gint nss_exit_status = NSS_STATUS_SUCCESS;
if (name != NULL || uid != 0)
{
GPtrArray *entries = g_ptr_array_new();
gint exit_status = run_aad_auth(db, name, uid, &nss_exit_status, errnop, entries);
if (exit_status != 0)
{
*errnop = ENOENT;
return NSS_STATUS_UNAVAIL;
}
*entry = g_strdup((gchar *)g_ptr_array_index(entries, 0));
g_ptr_array_free(entries, TRUE);
}
else if (all_entries->len == 0)
{
gint exit_status = run_aad_auth(db, name, uid, &nss_exit_status, errnop, all_entries);
if (exit_status != 0)
{
*errnop = ENOENT;
return NSS_STATUS_UNAVAIL;
}
*entry = g_strdup((gchar *)g_ptr_array_index(all_entries, *all_entries_index));
(*all_entries_index)++;
}
else if (*all_entries_index < all_entries->len)
{
*entry = g_strdup((gchar *)g_ptr_array_index(all_entries, *all_entries_index));
(*all_entries_index)++;
}
else
{
// iteration has ended, return our own status
(*all_entries_index) = 0;
*errnop = ENOENT;
return NSS_STATUS_UNAVAIL;
}
return nss_exit_status;
}