ref: e3d3899438ba1b2c899d46d6476e53c823b6b575
author: mkf <mkf@x230>
date: Wed Sep 6 10:07:47 EDT 2023
newuser: import
--- /dev/null
+++ b/newuser.c
@@ -1,0 +1,102 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+#include <libsec.h>
+#include <authsrv.h>
+#include "authcmdlib.h"
+
+void install(char*, char*, Authkey*, long, int);
+int exists(char*, char*);
+
+void
+usage(void)
+{
+ fprint(2, "usage: newuser [-p] user\n");
+ exits("usage");
+}
+
+void
+main(int argc, char *argv[])
+{
+ char *u, pass[32];
+ int newbio, dosecret;
+ long t;
+ Authkey key;
+ Acctbio a;
+ Fs *f;
+
+ fmtinstall('K', deskeyfmt);
+
+ ARGBEGIN{
+ case 'p':
+ pass = EARGF(usage());
+ break;
+ default:
+ usage();
+ }ARGEND
+ argv0 = "newuser";
+
+ if(argc != 1)
+ usage();
+ u = *argv;
+ if(memchr(u, '\0', ANAMELEN) == 0)
+ error("bad user name");
+
+ private();
+ newbio = 0;
+ t = 0;
+ a.user = 0;
+ memset(&key, 0, sizeof(key));
+ f = &fs[Plan9];
+ getpass(&key, pass, 1, 1);
+
+ install(f->keys, u, &key, t, newkey);
+
+ if(setsecret(KEYDB, u, pass) == 0)
+ error("error writing Inferno/POP secret");
+
+ if(querybio(f->who, u, &a))
+ wrbio(f->who, &a);
+
+ print("user %s installed for Plan 9\n", u);
+ syslog(0, AUTHLOG, "user %s installed for plan 9", u);
+
+ exits(0);
+}
+
+void
+install(char *db, char *u, Authkey *key, long t, int newkey)
+{
+ char buf[KEYDBBUF+ANAMELEN+20];
+ int fd;
+
+ if(!exists(db, u)){
+ snprint(buf, sizeof(buf), "%s/%s", db, u);
+ fd = create(buf, OREAD, 0777|DMDIR);
+ if(fd < 0)
+ error("can't create user %s: %r", u);
+ close(fd);
+ }
+
+ if(newkey && !setkey(db, u, key))
+ error("can't set key: %r");
+
+ if(t == -1)
+ return;
+ snprint(buf, sizeof(buf), "%s/%s/expire", db, u);
+ fd = open(buf, OWRITE);
+ if(fd < 0 || fprint(fd, "%ld", t) < 0)
+ error("can't write expiration time");
+ close(fd);
+}
+
+int
+exists(char *db, char *u)
+{
+ char buf[KEYDBBUF+ANAMELEN+6];
+
+ snprint(buf, sizeof(buf), "%s/%s/expire", db, u);
+ if(access(buf, 0) < 0)
+ return 0;
+ return 1;
+}