|
| 1 | +/* |
| 2 | + * auth_cache.h - Authorization cache for offline mode |
| 3 | + * |
| 4 | + * Caches authorization responses from LLNG server for offline use. |
| 5 | + * Uses AES-256-GCM encryption with machine-id derived key. |
| 6 | + * |
| 7 | + * Copyright (C) 2024 Linagora |
| 8 | + * License: AGPL-3.0 |
| 9 | + */ |
| 10 | + |
| 11 | +#ifndef AUTH_CACHE_H |
| 12 | +#define AUTH_CACHE_H |
| 13 | + |
| 14 | +#include <stdbool.h> |
| 15 | +#include <time.h> |
| 16 | + |
| 17 | +/* Authorization cache entry */ |
| 18 | +typedef struct { |
| 19 | + int version; /* Cache format version (3) */ |
| 20 | + time_t expires_at; /* Expiration timestamp */ |
| 21 | + char *user; /* Username */ |
| 22 | + bool authorized; /* Authorization result */ |
| 23 | + char **groups; /* User groups */ |
| 24 | + size_t groups_count; /* Number of groups */ |
| 25 | + bool sudo_allowed; /* Sudo permission */ |
| 26 | + bool sudo_nopasswd; /* Sudo without password */ |
| 27 | + char *gecos; /* Full name / GECOS */ |
| 28 | + char *shell; /* Login shell */ |
| 29 | + char *home; /* Home directory */ |
| 30 | +} auth_cache_entry_t; |
| 31 | + |
| 32 | +/* Authorization cache handle */ |
| 33 | +typedef struct auth_cache auth_cache_t; |
| 34 | + |
| 35 | +/* |
| 36 | + * Initialize authorization cache |
| 37 | + * cache_dir: Directory for cache files |
| 38 | + * Returns NULL on failure |
| 39 | + */ |
| 40 | +auth_cache_t *auth_cache_init(const char *cache_dir); |
| 41 | + |
| 42 | +/* |
| 43 | + * Destroy cache and free resources |
| 44 | + */ |
| 45 | +void auth_cache_destroy(auth_cache_t *cache); |
| 46 | + |
| 47 | +/* |
| 48 | + * Look up cached authorization for a user |
| 49 | + * user: Username to look up |
| 50 | + * server_group: Server group (for cache key) |
| 51 | + * host: Hostname (for cache key binding) |
| 52 | + * entry: Output parameter for cached entry (caller must free with auth_cache_entry_free) |
| 53 | + * Returns true if valid cache entry found, false otherwise |
| 54 | + */ |
| 55 | +bool auth_cache_lookup(auth_cache_t *cache, |
| 56 | + const char *user, |
| 57 | + const char *server_group, |
| 58 | + const char *host, |
| 59 | + auth_cache_entry_t *entry); |
| 60 | + |
| 61 | +/* |
| 62 | + * Store authorization result in cache |
| 63 | + * user: Username |
| 64 | + * server_group: Server group (for cache key) |
| 65 | + * host: Hostname (for cache key binding) |
| 66 | + * entry: Entry to store |
| 67 | + * ttl: Time-to-live in seconds |
| 68 | + * Returns 0 on success, -1 on error |
| 69 | + */ |
| 70 | +int auth_cache_store(auth_cache_t *cache, |
| 71 | + const char *user, |
| 72 | + const char *server_group, |
| 73 | + const char *host, |
| 74 | + const auth_cache_entry_t *entry, |
| 75 | + int ttl); |
| 76 | + |
| 77 | +/* |
| 78 | + * Invalidate cache entry for a user |
| 79 | + */ |
| 80 | +void auth_cache_invalidate(auth_cache_t *cache, |
| 81 | + const char *user, |
| 82 | + const char *server_group, |
| 83 | + const char *host); |
| 84 | + |
| 85 | +/* |
| 86 | + * Check if force-online file exists |
| 87 | + * If file contains usernames, returns true only if user is listed |
| 88 | + * force_online_file: Path to force-online file |
| 89 | + * user: Username to check |
| 90 | + * Returns true if user should skip cache |
| 91 | + */ |
| 92 | +bool auth_cache_force_online(const char *force_online_file, const char *user); |
| 93 | + |
| 94 | +/* |
| 95 | + * Free cache entry contents |
| 96 | + */ |
| 97 | +void auth_cache_entry_free(auth_cache_entry_t *entry); |
| 98 | + |
| 99 | +/* |
| 100 | + * Clean up expired entries |
| 101 | + * Returns number of entries removed |
| 102 | + */ |
| 103 | +int auth_cache_cleanup(auth_cache_t *cache); |
| 104 | + |
| 105 | +#endif /* AUTH_CACHE_H */ |
0 commit comments