diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-03-23 20:28:57 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-03-23 20:28:57 +0100 |
| commit | dace7dca77e9539b5cb459f79c1154d11b3030fe (patch) | |
| tree | fbd3bc83e15c7b8d7e41dc351a7a50cfc0164c2f /hash.h | |
first version of wlock
Diffstat (limited to 'hash.h')
| -rw-r--r-- | hash.h | 55 |
1 files changed, 55 insertions, 0 deletions
@@ -0,0 +1,55 @@ +#if HAVE_SHADOW_H +#include <shadow.h> +#endif + +#include <unistd.h> +#include <pwd.h> +#include <grp.h> +#include <errno.h> +#include <stdio.h> +#include <string.h> + +#include "util.h" + +static const char *get_hash(); + +const char * +get_hash() +{ + const char *hash; + struct passwd *pw; + + /* Check if the current user has a password entry */ + errno = 0; + if (!(pw = getpwuid(getuid()))) { + if (errno) + die("slock: getpwuid: %s\n", strerror(errno)); + else + die("slock: cannot retrieve password entry\n"); + } + hash = pw->pw_passwd; + +#if HAVE_SHADOW_H + if (!strcmp(hash, "x")) { + struct spwd *sp; + if (!(sp = getspnam(pw->pw_name))) + die("slock: getspnam: cannot retrieve shadow entry. " + "Make sure to suid or sgid slock.\n"); + hash = sp->sp_pwdp; + } +#else + if (!strcmp(hash, "*")) { +#ifdef __OpenBSD__ + if (!(pw = getpwuid_shadow(getuid()))) + die("slock: getpwnam_shadow: cannot retrieve shadow entry. " + "Make sure to suid or sgid slock.\n"); + hash = pw->pw_passwd; +#else + die("slock: getpwuid: cannot retrieve shadow entry. " + "Make sure to suid or sgid slock.\n"); +#endif /* __OpenBSD__ */ + } +#endif /* HAVE_SHADOW_H */ + + return hash; +} |