summaryrefslogtreecommitdiff
path: root/hash.h
diff options
context:
space:
mode:
authorNathan Reiner <nathan@nathanreiner.xyz>2023-03-23 20:28:57 +0100
committerNathan Reiner <nathan@nathanreiner.xyz>2023-03-23 20:28:57 +0100
commitdace7dca77e9539b5cb459f79c1154d11b3030fe (patch)
treefbd3bc83e15c7b8d7e41dc351a7a50cfc0164c2f /hash.h
first version of wlock
Diffstat (limited to 'hash.h')
-rw-r--r--hash.h55
1 files changed, 55 insertions, 0 deletions
diff --git a/hash.h b/hash.h
new file mode 100644
index 0000000..5de5d45
--- /dev/null
+++ b/hash.h
@@ -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;
+}