diff --git a/.gitignore b/.gitignore index 28e474c..5ac75ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.o +gmon.out miniwebcam config.h diff --git a/Makefile b/Makefile index c35c452..2072e56 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,14 @@ DATAPREFIX=/var/lib RUNPID=/var/run/miniwebcam.pid ETCPREFIX=/etc SYSTEMDUNITDIR=$(ETCPREFIX)/systemd/system +CONFIGDIR=$(ETCPREFIX)/miniwebcam +CONFIGFILE=$(CONFIGDIR)/miniwebcam.conf +LEGACYCONFIG=$(ETCPREFIX)/miniwebcam.conf +SSLKEY=$(CONFIGDIR)/ssl-key.pem +SSLCERT=$(CONFIGDIR)/ssl-cert.pem +SERVICEUSER=minicamuser +INSTALLSTATEDIR=$(DATAPREFIX)/miniwebcam +USERCREATEDMARKER=$(INSTALLSTATEDIR)/.make-created-user DEFAULT_SERVERPORT=20010 ARCH=$(shell uname -m) CXX=g++ @@ -29,18 +37,54 @@ else endif rm -rf assets.tar -miniwebcam: $(OBJFILES) assets.obj +miniwebcam: config $(OBJFILES) assets.obj $(CXX) $(OBJFILES) -o $@ assets.obj -L./ -I./ $(LDFLAGS) rm -rf assets.obj install: miniwebcam - install -d $(DESTDIR)$(PREFIX)/bin $(DESTDIR)$(SYSTEMDUNITDIR) - install -m 755 miniwebcam $(DESTDIR)$(PREFIX)/bin/miniwebcam - sed 's|@PREFIX@|$(PREFIX)|g' miniwebcam.service > $(DESTDIR)$(SYSTEMDUNITDIR)/miniwebcam.service + set -e; \ + if [ -z "$(DESTDIR)" ] && ! getent passwd "$(SERVICEUSER)" >/dev/null; then \ + install -d "$(INSTALLSTATEDIR)"; \ + useradd --system --user-group --no-create-home --home-dir /nonexistent --shell /usr/sbin/nologin "$(SERVICEUSER)"; \ + touch "$(USERCREATEDMARKER)"; \ + fi; \ + install -d "$(DESTDIR)$(PREFIX)/bin" "$(DESTDIR)$(SYSTEMDUNITDIR)" "$(DESTDIR)$(CONFIGDIR)"; \ + chmod 750 "$(DESTDIR)$(CONFIGDIR)"; \ + if [ -z "$(DESTDIR)" ]; then service_group=$$(id -gn "$(SERVICEUSER)"); chown root:"$$service_group" "$(DESTDIR)$(CONFIGDIR)"; fi; \ + install -m 755 miniwebcam "$(DESTDIR)$(PREFIX)/bin/miniwebcam"; \ + if [ ! -e "$(DESTDIR)$(CONFIGFILE)" ]; then \ + if [ -f "$(DESTDIR)$(LEGACYCONFIG)" ]; then \ + install -m 640 "$(DESTDIR)$(LEGACYCONFIG)" "$(DESTDIR)$(CONFIGFILE)"; \ + rm -f "$(DESTDIR)$(LEGACYCONFIG)"; \ + else \ + install -m 640 /dev/null "$(DESTDIR)$(CONFIGFILE)"; printf '{}\n' > "$(DESTDIR)$(CONFIGFILE)"; \ + fi; \ + fi; \ + if [ -z "$(DESTDIR)" ]; then chown root:"$$service_group" "$(DESTDIR)$(CONFIGFILE)"; fi; \ + chmod 640 "$(DESTDIR)$(CONFIGFILE)"; \ + if [ ! -e "$(DESTDIR)$(SSLKEY)" ] && [ ! -e "$(DESTDIR)$(SSLCERT)" ]; then \ + umask 077; openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj "/CN=localhost" -keyout "$(DESTDIR)$(SSLKEY)" -out "$(DESTDIR)$(SSLCERT)"; \ + elif [ ! -f "$(DESTDIR)$(SSLKEY)" ] || [ ! -f "$(DESTDIR)$(SSLCERT)" ]; then \ + echo "Error: SSL key/certificate pair is incomplete; refusing to overwrite either file." >&2; exit 1; \ + fi; \ + chmod 640 "$(DESTDIR)$(SSLKEY)"; chmod 644 "$(DESTDIR)$(SSLCERT)"; \ + if [ -z "$(DESTDIR)" ]; then chown root:"$$service_group" "$(DESTDIR)$(SSLKEY)"; chown root:root "$(DESTDIR)$(SSLCERT)"; fi; \ + sed 's|@PREFIX@|$(PREFIX)|g' miniwebcam.service > "$(DESTDIR)$(SYSTEMDUNITDIR)/miniwebcam.service" uninstall: - rm -f $(DESTDIR)$(PREFIX)/bin/miniwebcam - rm -f $(DESTDIR)$(SYSTEMDUNITDIR)/miniwebcam.service + set -e; \ + if [ -z "$(DESTDIR)" ] && command -v systemctl >/dev/null 2>&1; then \ + if systemctl is-active --quiet miniwebcam.service; then systemctl stop miniwebcam.service; fi; \ + if systemctl is-enabled --quiet miniwebcam.service; then systemctl disable miniwebcam.service; fi; \ + fi; \ + rm -f "$(DESTDIR)$(PREFIX)/bin/miniwebcam" "$(DESTDIR)$(SYSTEMDUNITDIR)/miniwebcam.service" "$(DESTDIR)$(CONFIGFILE)" "$(DESTDIR)$(SSLKEY)" "$(DESTDIR)$(SSLCERT)"; \ + rmdir "$(DESTDIR)$(CONFIGDIR)" 2>/dev/null || true; \ + if [ -z "$(DESTDIR)" ] && [ -f "$(USERCREATEDMARKER)" ]; then \ + userdel "$(SERVICEUSER)"; \ + rm -f "$(USERCREATEDMARKER)"; \ + rmdir "$(INSTALLSTATEDIR)" 2>/dev/null || true; \ + fi; \ + if [ -z "$(DESTDIR)" ] && command -v systemctl >/dev/null 2>&1; then systemctl daemon-reload; fi config: echo "#ifndef _CONFIG_H_" > config.h @@ -59,7 +103,7 @@ config: rebuild: clean all -dep: +dep: config $(CXX) -MM `ls *.cc` $(CXXFLAGS) > $(DEPENDFILE) keygen: diff --git a/README.md b/README.md index a5202d7..9281f4a 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ the following devices: ## Configuration The configuration is loaded in the following order. -- systemwide configuration file [/etc/miniwebcam.conf] +- systemwide configuration file [/etc/miniwebcam/miniwebcam.conf] - program parameters - user defined configurtion file defined in the program parameters @@ -17,15 +17,30 @@ daemon, or `-F` to explicitly run in the foreground. Daemon mode redirects standard input, output, and error to `/dev/null`; use `-debug` only when running in the foreground if you need to see debug output. -The default system configuration file is `/etc/miniwebcam.conf`. `make install` -installs the executable and a systemd service unit. After installing, reload -systemd and enable/start the service with: +The default configuration file is `/etc/miniwebcam/miniwebcam.conf`. `make install` +installs the executable and a systemd system unit named `miniwebcam.service`. +The system service runs as the unprivileged `minicamuser` account (not as root) +and has access to devices in the `video` group. `make install` creates the +system account if it does not exist and creates the configuration directory +with root-owned files readable by the service account. If the configuration is +absent, an existing `/etc/miniwebcam.conf` is moved into the new directory; +otherwise it is initialized with empty JSON defaults. If the SSL key and +certificate are absent, installation generates a self-signed certificate and +stores both in `/etc/miniwebcam/`; existing configuration and certificates are +preserved. The generated certificate is self-signed, so HTTPS clients will not +trust it by default. Run installation as root, for example `sudo make install`. +After installing, enable/start the service with: ```sh sudo systemctl daemon-reload sudo systemctl enable --now miniwebcam.service ``` +`make uninstall` stops and disables the service, removes the installed +executable, unit, configuration, and SSL key/certificate, and removes +`minicamuser` if `make install` created that account. An account that existed +before installation is retained. + # INFORMATION here are some usefull commands, maybe it helps to set the resolution to a readonable value diff --git a/configuration.h b/configuration.h index e649c60..68d8e90 100644 --- a/configuration.h +++ b/configuration.h @@ -10,7 +10,7 @@ #define DEFAULT_HTTP_PORT 8080 #define DEFAULT_HTTPS_PORT 8081 -#define DEFAULT_CONFIG_FILE "/etc/miniwebcam.conf" +#define DEFAULT_CONFIG_FILE "/etc/miniwebcam/miniwebcam.conf" #define DEFAULT_SSL_KEY "/etc/miniwebcam/ssl-key.pem" #define DEFAULT_SSL_CERT "/etc/miniwebcam/ssl-cert.pem" #define DEFAULT_VDEV_IOMODE 1 diff --git a/miniwebcam.service b/miniwebcam.service index e8bb946..260fe61 100644 --- a/miniwebcam.service +++ b/miniwebcam.service @@ -4,6 +4,8 @@ After=network.target [Service] Type=simple +User=minicamuser +SupplementaryGroups=video ExecStart=@PREFIX@/bin/miniwebcam -F Restart=on-failure