commit 76ca4acb056a6ffb8b7105526930fc8b03d4f1ad
parent 1d16f22bf280b27c6ce63e14156a791e788f3431
Author: Nihal Jere <nihal@nihaljere.xyz>
Date: Thu, 14 Oct 2021 22:46:02 -0500
extract get_password and xwrite into util.c
Diffstat:
M | Makefile | | | 14 | +++++++------- |
M | npm-agent.c | | | 50 | ++++++++++++-------------------------------------- |
M | npm-core.c | | | 27 | ++++----------------------- |
M | npmc.c | | | 49 | ++++++++++--------------------------------------- |
A | util.c | | | 43 | +++++++++++++++++++++++++++++++++++++++++++ |
A | util.h | | | 2 | ++ |
6 files changed, 78 insertions(+), 107 deletions(-)
diff --git a/Makefile b/Makefile
@@ -2,21 +2,21 @@
.PHONY: all clean install
PREFIX = /usr/local
-SRC = npm-core.c npm-agent.c npmc.c monocypher.c
+SRC = npm-core.c npm-agent.c npmc.c monocypher.c util.c
OBJ = $(SRC:%.c=%.o)
EXE = npm-agent npm-core npmc
NPM_CORE = "npm-core"
all: npm-core npm-agent npmc
-npm-core: $(LIBS) npm-core.o monocypher.o
- $(CC) $(LDFLAGS) npm-core.o monocypher.o -o $@
+npm-core: npm-core.o monocypher.o util.o
+ $(CC) $(LDFLAGS) $+ -o $@
-npm-agent: npm-agent.o
- $(CC) $(LDFLAGS) npm-agent.o -o $@
+npm-agent: npm-agent.o util.o
+ $(CC) $(LDFLAGS) $+ -o $@
-npmc: npmc.o
- $(CC) $(LDFLAGS) npmc.o -o $@
+npmc: npmc.o util.o
+ $(CC) $(LDFLAGS) $+ -o $@
npm-agent.o: npm-agent.c
$(CC) '-DNPM_CORE=$(NPM_CORE)' $(CFLAGS) -c $< -o $@
diff --git a/npm-agent.c b/npm-agent.c
@@ -23,6 +23,7 @@
#include <unistd.h>
#include "common.h"
+#include "util.h"
#ifndef TIMEOUT
#define TIMEOUT 10000
@@ -39,49 +40,13 @@ char *const getpasscmd[] = { "bemenu", "-x", "-p", "Password:", NULL };
bool cached = false;
char inbuf[PATH_MAX];
-char master[PASSWORD_MAX_LEN + 1];
+char master[PASSWORD_MAX_LEN + 2];
ssize_t masterlen;
char *inptr = inbuf;
size_t inlen;
struct pollfd fds[3];
int timerpipe[2];
-int
-xwrite(int fd, char *buf, size_t count)
-{
- ssize_t ret;
- char *ptr = buf;
- while (count) {
- ret = write(fd, ptr, count);
- if (ret == -1)
- return -1;
-
- count -= ret;
- ptr += ret;
- }
-
- return count;
-}
-
-int
-read_to_nl(int fd, char *buf)
-{
- ssize_t ret;
- size_t len = 0;
- char *ptr = buf;
-
- do {
- ret = read(fd, ptr, PASSWORD_MAX_LEN - len);
- if (ret == -1)
- return ret;
-
- len += ret;
- ptr += ret;
- } while (ret && len <= PASSWORD_MAX_LEN && !memchr(ptr, '\n', ret));
-
- return len;
-}
-
void
clear_master()
{
@@ -124,12 +89,21 @@ get_master()
close(stdoutpipe[1]);
close(stdinpipe[0]);
close(stdinpipe[1]);
+ FILE *stdoutfile = fdopen(stdoutpipe[0], "r");
+ if (!stdoutfile) {
+ perror("failed to open npm-core stdout as FILE");
+ goto here;
+ }
- if ((masterlen = read_to_nl(stdoutpipe[0], master)) == -1) {
+ if ((masterlen = get_password(stdoutfile, master)) == -1) {
fprintf(stderr, "failed to read password from pipe\n");
return -1;
}
+ master[masterlen++] = '\n';
+
+here:
+ fclose(stdoutfile);
close(stdoutpipe[0]);
waitpid(pid, &status, 0);
}
diff --git a/npm-core.c b/npm-core.c
@@ -10,6 +10,7 @@
#include "common.h"
#include "monocypher.h"
+#include "util.h"
char *argv0;
@@ -35,26 +36,6 @@ clear()
explicit_bzero(data, sizeof(data));
}
-ssize_t
-get_password(uint8_t *buf)
-{
- int ret;
- uint8_t *ptr = buf;
- while (ptr - buf < PASSWORD_MAX_LEN) {
- ret = fgetc(stdin);
- if (ret == EOF) {
- return -1;
- }
-
- if (ret == '\n')
- return ptr - buf;
-
- *(ptr++) = ret;
- }
-
- return -2;
-}
-
void
error(const char *s)
{
@@ -112,7 +93,7 @@ int main(int argc, char *argv[]) {
goto fail;
}
- switch (len = get_password(master)) {
+ switch (len = get_password(stdin, master)) {
case -1:
error("encountered EOF when reading master password");
goto fail;
@@ -134,7 +115,7 @@ int main(int argc, char *argv[]) {
goto fail;
}
- switch (len = get_password(plain)) {
+ switch (len = get_password(stdin, plain)) {
case -1:
error("encountered EOF when reading password");
goto fail;
@@ -180,7 +161,7 @@ int main(int argc, char *argv[]) {
goto fail;
}
- switch (len = get_password(master)) {
+ switch (len = get_password(stdin, master)) {
case -1:
error("encountered EOF when reading master password");
goto fail;
diff --git a/npmc.c b/npmc.c
@@ -12,6 +12,7 @@
#include <errno.h>
#include "common.h"
+#include "util.h"
char answer[PASSWORD_MAX_LEN + 1];
int answerlen = 0;
@@ -19,45 +20,9 @@ int answerlen = 0;
char abspath[PATH_MAX];
int
-xwrite(int fd, char *buf, size_t count)
-{
- ssize_t ret;
- char *ptr = buf;
- while (count) {
- ret = write(fd, ptr, count);
- if (ret == -1)
- return -1;
-
- count -= ret;
- ptr += ret;
- }
-
- return count;
-}
-
-int
-read_answer(int fd)
-{
- ssize_t ret;
- char *ptr = answer;
- while (answerlen <= PASSWORD_MAX_LEN && !memchr(answer, '\n', answerlen)) {
- ret = read(fd, ptr, PASSWORD_MAX_LEN - answerlen);
- if (ret == -1)
- return ret;
-
- if (ret == 0)
- break;
-
- answerlen += ret;
- ptr += ret;
- }
-
- return answerlen;
-}
-
-int
main(int argc, char *argv[])
{
+ FILE *sockfile;
const struct sockaddr_un sockaddr = {
.sun_family = AF_UNIX,
.sun_path = SOCKPATH
@@ -83,13 +48,19 @@ main(int argc, char *argv[])
xwrite(sock, abspath, strlen(abspath) + 1); // include terminator
- read_answer(sock);
+ sockfile = fdopen(sock, "rw");
+ if (!sockfile) {
+ perror("failed to open socket as FILE");
+ close(sock);
+ goto end;
+ }
+ get_password(sockfile, answer);
if (*answer)
printf("%s", answer);
closesock:
- close(sock);
+ fclose(sockfile);
end:
return !(*answer);
}
diff --git a/util.c b/util.c
@@ -0,0 +1,43 @@
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <unistd.h>
+
+#include "common.h"
+
+int
+xwrite(int fd, char *buf, size_t count)
+{
+ ssize_t ret;
+ char *ptr = buf;
+ while (count) {
+ ret = write(fd, ptr, count);
+ if (ret == -1)
+ return -1;
+
+ count -= ret;
+ ptr += ret;
+ }
+
+ return count;
+}
+
+ssize_t
+get_password(FILE *f, char *buf)
+{
+ int ret;
+ char *ptr = buf;
+ while (ptr - buf < PASSWORD_MAX_LEN) {
+ ret = fgetc(f);
+ if (ret == EOF)
+ return -1;
+
+ if (ret == '\n')
+ return ptr - buf;
+
+ *(ptr++) = ret;
+ }
+
+ return -2;
+}
+
diff --git a/util.h b/util.h
@@ -0,0 +1,2 @@
+int xwrite(int fd, char *buf, size_t count);
+ssize_t get_password(FILE *f, uint8_t *buf);