9 #include <json-c/json.h>
12 #define PAM_SM_ACCOUNT
13 #define PAM_SM_SESSION
14 #define PAM_SM_PASSWORD
15 #include <security/pam_modules.h>
16 #include <security/pam_appl.h>
18 #define DATABASE_FILE "/etc/agl/keys.json"
20 #define BLOCK_SIZE 4096
21 typedef struct header_
27 int is_valid_mn(const char* v)
29 return v && v[0] == 'I' && v[1] == 'D' && v[2] == 'K' && v[3] == 'Y';
32 char* get_file_content(const char* path)
41 f = fopen(path, "rb");
44 fseek(f, 0, SEEK_END);
46 fseek(f, 0, SEEK_SET);
47 buffer = malloc(length + 1);
50 if (fread (buffer, 1, length, f) != length)
55 else buffer[length] = 0;
63 int read_device(const char* device, char** idkey)
69 printf("[PAM DEBUG] check_device %s...\n", device);
70 fd = open(device, O_RDONLY);
73 printf("[PAM DEBUG] Failed to open the device %s!\n", device);
74 return PAM_SERVICE_ERR;
77 sz = read(fd, &h, sizeof(header));
78 if (sz != sizeof(header) || !is_valid_mn(h.mn) || h.size < 1) { close(fd); printf("[PAM DEBUG]: bad header!\n"); return PAM_SERVICE_ERR; }
79 printf("[PAM DEBUG]: data size=%lu\n", h.size);
81 *idkey = (char*)malloc(h.size + 1);
82 if (!*idkey) { close(fd); printf("[PAM DEBUG] Bad alloc!\n"); return PAM_SERVICE_ERR; }
84 memset(*idkey, 0, h.size + 1);
85 sz = read(fd, *idkey, h.size);
87 if (sz != h.size) { free(idkey); printf("[PAM DEBUG] Bad data read!\n"); return PAM_SERVICE_ERR; }
91 int authenticate(pam_handle_t* pamh, const char* uuid)
94 struct json_object* database;
95 struct json_object* key;
97 file_content = get_file_content(DATABASE_FILE);
100 printf("[PAM DEBUG] Failed to read database file (%s)\n", DATABASE_FILE);
101 return PAM_SERVICE_ERR;
104 database = json_tokener_parse(file_content);
107 printf("[PAM DEBUG] Failed to parse the database\n");
108 return PAM_SERVICE_ERR;
111 if (json_object_object_get_ex(database, uuid, &key))
113 printf("[PAM] Key found!\n");
114 printf("[PAM DEBUG] pam_set_item(\"%s\")\n", uuid);
115 pam_set_item(pamh, PAM_USER, uuid);
117 const char* pam_authtok;
118 if (pam_get_item(pamh, PAM_AUTHTOK, (const void**)&pam_authtok) == PAM_SUCCESS && !pam_authtok)
119 pam_set_item(pamh, PAM_AUTHTOK, uuid);
121 json_object_put(database);
126 printf("[PAM] Key not found!\n");
128 if (database) json_object_put(database);
132 int check_device(pam_handle_t* pamh, const char* device)
137 ret = read_device(device, &idkey);
138 if (ret != PAM_SUCCESS) return ret;
140 printf("[PAM DEBUG] Data read:\n%s\n", idkey);
142 json_object* idkey_json = json_tokener_parse(idkey);
146 printf("[PAM DEBUG] Failed to parse json data!\n");
147 return PAM_SERVICE_ERR;
150 json_object* uuid_json;
151 if(!json_object_object_get_ex(idkey_json, "uuid", &uuid_json))
154 printf("[PAM DEBUG] The json does not contains a valid uuid\n");
155 return PAM_SERVICE_ERR;
158 const char* uuid = json_object_get_string(uuid_json);
159 printf("[PAM DEBUG] uuid: %s\n", uuid);
161 ret = authenticate(pamh, uuid);
163 json_object_put(idkey_json);
167 void log_pam(const char* fname, int flags, int argc, const char** argv, const char* device)
169 printf("[PAM DEBUG] ---------- %s ----------\n", fname);
170 printf("[PAM DEBUG] flags: %d\n", flags);
171 for(int i = 0; i < argc; ++i)
173 printf("[PAM DEBUG] argv[%d]: %s\n", i, argv[i]);
175 printf("[PAM DEBUG] device: %s\n", device);
176 printf("[PAM DEBUG] ----------------------------------------\n");
180 @brief The pam_sm_authenticate function is the service module's implementation
181 of the pam_authenticate(3) interface.
182 This function performs the task of authenticating the user.
184 @param[in] pamh Unknown.
185 @param[in] flags PAM_SILENT and/or PAM_DISALLOW_NULL_AUTHTOK.
186 @return PAM_SUCCESS if ok.
188 PAM_EXTERN int pam_sm_authenticate(pam_handle_t* pamh, int flags, int argc, const char** argv)
190 const char* device = pam_getenv(pamh, "DEVICE");
191 log_pam("pam_sm_authenticate", flags, argc, argv, device);
192 return check_device(pamh, device);
195 PAM_EXTERN int pam_sm_setcred(pam_handle_t* pamh, int flags, int argc, const char** argv)
197 log_pam("pam_sm_setcred", flags, argc, argv, pam_getenv(pamh, "DEVICE"));
201 PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t* pamh, int flags, int argc, const char** argv)
203 log_pam("pam_sm_acct_mgmt", flags, argc, argv, pam_getenv(pamh, "DEVICE"));
207 PAM_EXTERN int pam_sm_open_session(pam_handle_t* pamh, int flags, int argc, const char** argv)
209 log_pam("pam_sm_open_session", flags, argc, argv, pam_getenv(pamh, "DEVICE"));
213 PAM_EXTERN int pam_sm_close_session(pam_handle_t* pamh, int flags, int argc, const char** argv)
215 log_pam("pam_sm_close_session", flags, argc, argv, pam_getenv(pamh, "DEVICE"));
219 PAM_EXTERN int pam_sm_chauthtok(pam_handle_t* pamh, int flags, int argc, const char** argv)
221 log_pam("pam_sm_chauthtok", flags, argc, argv, pam_getenv(pamh, "DEVICE"));