Skip to content

Commit

Permalink
Import code
Browse files Browse the repository at this point in the history
Signed-off-by: Adrien Gallouët <[email protected]>
  • Loading branch information
angt committed Apr 24, 2020
1 parent 6b0c080 commit 7417bf0
Show file tree
Hide file tree
Showing 9 changed files with 773 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
secret
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "libhydrogen"]
path = libhydrogen
url = https://github.com/jedisct1/libhydrogen
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
CC = cc
CFLAGS = -Wall -O2
prefix = /usr/local

secret:
$(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) secret.c -o secret

install: secret
mkdir -p $(DESTDIR)$(prefix)/bin
cp -f secret $(DESTDIR)$(prefix)/bin

uninstall:
rm -f $(DESTDIR)$(prefix)/bin/secret

clean:
rm -f secret

.PHONY: secret install uninstall clean
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# secret

Keep your little secrets, publicly.

## Features

- Only one file to backup: `~/.secret`.
- No configuration: get back your file and you're done.
- URLs/logins/scopes are encrypted too.
- Secret agent that allows shell completion (only `bash` for now).
- Support many passwords (a visual hash might be required).
- Depends only on the [libhydrogen](https://libhydrogen.org/) library.
- Small, simple and non obfuscated C code.

## Build and install

$ make install prefix=/usr

## Commands

$ secret
Available commands:
init Init secret storage
list List all secrets
add Add a new secret
show Show a secret
change Change a secret
agent Exec in secret zone

## Examples

Initialize secret:

$ secret init

Add a new generated secret called 'test':

$ secret add test
Password:
Secret [random]:
9{6u0ue>5&W2+z#OR:`X<@-#

Show secret 'test':

$ secret show test
Password:
9{6u0ue>5&W2+z#OR:`X<@-#

Start a secret zone:

$ secret agent bash
Password:

You can now manipulate your secrets easily and with completion:

$ ./secret show test
9{6u0ue>5&W2+z#OR:`X<@-#

93 changes: 93 additions & 0 deletions argz/argz.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "argz.h"

static int
argz_help(int argc, char **argv)
{
return argc >= 2 && !strcmp(argv[1], "help");
}

static int
argz_cmp(struct argz *z, char *name)
{
if (!strcmp(z->name, name))
return 0;
if (z->alt) for (unsigned k = 0; z->alt[k]; k++) {
if (!strcmp(z->alt[k], name))
return 0;
}
return 1;
}

static int
argz_is_available(struct argz *z, unsigned i, unsigned *ret)
{
if (z[i].set)
return 0;
if (z[i].grp) for (unsigned k = 0; z[k].name; k++) {
if (z[k].set && z[k].grp == z[i].grp) {
if (ret) *ret = k;
return 0;
}
}
return 1;
}

void
argz_print(struct argz *z)
{
int len = 0;

for (int i = 0; z[i].name; i++) {
if (!argz_is_available(z, i, NULL))
continue;
int nlen = strlen(z[i].name);
if (len < nlen)
len = nlen;
}

for (int i = 0; z[i].name; i++) {
if (!argz_is_available(z, i, NULL))
continue;
printf(" %-*s %s\n", len, z[i].name, z[i].help ?: "");
}
}

int
argz(int argc, char **argv, void *data)
{
struct argz *z = (struct argz *)data;

if (argz_help(argc, argv)) {
argz_print(z);
return 0;
}

if (argc > 1) {
for (unsigned i = 0; z[i].name; i++) {
if (argz_cmp(&z[i], argv[1]))
continue;
unsigned k = 0;
if (!argz_is_available(z, i, &k)) {
fprintf(stderr, "cannot call %s because of %s\n", z[i].name, z[k].name);
return 0;
}
int ret = argc - 1;
if (z[i].f)
ret = z[i].f(ret, argv + argc - ret, z[i].data);
return argz(ret, argv + argc - ret, data);
}
fprintf(stderr, "Unknown: %s\n", argv[1]);
}

return argc;
}
14 changes: 14 additions & 0 deletions argz/argz.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

struct argz {
char *name;
char *help;
int (*f)(int, char **, void *);
void *data;
const char *const *alt;
unsigned grp;
int set;
};

int argz (int, char **, void *);
void argz_print (struct argz *);
13 changes: 13 additions & 0 deletions argz/argz.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
_argz() {
local last opts
last="${COMP_WORDS[COMP_CWORD]}"
COMP_WORDS[COMP_CWORD]="help"
opts="$("${COMP_WORDS[@]}" 2>/dev/null | awk '{print $1}' )"
case "$opts" in
'') ;;
CMD) mapfile -t COMPREPLY < <(compgen -A command -- "$last") ;;
DIR) mapfile -t COMPREPLY < <(compgen -A dir -- "$last") ;;
FILE) mapfile -t COMPREPLY < <(compgen -A file -- "$last") ;;
*) mapfile -t COMPREPLY < <(compgen -W "$opts" -- "$last") ;;
esac
}
1 change: 1 addition & 0 deletions libhydrogen
Submodule libhydrogen added at 3de3ef
Loading

0 comments on commit 7417bf0

Please sign in to comment.