From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id CPOBKJTs9l9rcgAA0tVLHw (envelope-from ) for ; Thu, 07 Jan 2021 11:12:20 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id iFJWJJTs9l/mNgAAbx9fmQ (envelope-from ) for ; Thu, 07 Jan 2021 11:12:20 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id CDCAC94060F for ; Thu, 7 Jan 2021 11:12:19 +0000 (UTC) Received: from localhost ([::1]:48804 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kxTDG-0005T8-B8 for larch@yhetil.org; Thu, 07 Jan 2021 06:12:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:52592) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kxTD1-0005Sj-3X for bug-guix@gnu.org; Thu, 07 Jan 2021 06:12:03 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:35106) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kxTD0-0004TY-1M for bug-guix@gnu.org; Thu, 07 Jan 2021 06:12:02 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kxTCz-0007DJ-Rs for bug-guix@gnu.org; Thu, 07 Jan 2021 06:12:01 -0500 X-Loop: help-debbugs@gnu.org Subject: bug#45570: [PATCH v3] system: Assert, that user and group names are unique. References: In-Reply-To: Resent-From: Leo Prikler Original-Sender: "Debbugs-submit" Resent-CC: bug-guix@gnu.org Resent-Date: Thu, 07 Jan 2021 11:12:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 45570 X-GNU-PR-Package: guix X-GNU-PR-Keywords: To: 45570@debbugs.gnu.org Received: via spool by 45570-submit@debbugs.gnu.org id=B45570.161001786227663 (code B ref 45570); Thu, 07 Jan 2021 11:12:01 +0000 Received: (at 45570) by debbugs.gnu.org; 7 Jan 2021 11:11:02 +0000 Received: from localhost ([127.0.0.1]:46652 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kxTC1-0007Bx-Gn for submit@debbugs.gnu.org; Thu, 07 Jan 2021 06:11:01 -0500 Received: from mailrelay.tugraz.at ([129.27.2.202]:14567) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kxTBz-0007Bj-SA for 45570@debbugs.gnu.org; Thu, 07 Jan 2021 06:11:00 -0500 Received: from localhost.localdomain (217-149-174-13.nat.highway.telekom.at [217.149.174.13]) by mailrelay.tugraz.at (Postfix) with ESMTPSA id 4DBNns12Rqz3x86; Thu, 7 Jan 2021 12:10:57 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tugraz.at; s=mailrelay; t=1610017857; bh=tjv9docNCMZsPkru4y8d/dcwxZhXEcYtbXM57f5iSJg=; h=From:To:Cc:Subject:Date; b=mgEMKW+/xS8VBM+0TUEMDUmaG/4+5LFZEb/IDaBIC/o6qnSok0Z5RI7UO8tQkw91R p7Q00GkhILHDJLsWdq1S68wrvaC96QydgAOeWxZovmrXPF2GEsgqA89ZEZmAioxqSc W6YDvWa00CGH0TCuwlMd4mOE1EH1Ug+l8/bNQvoQ= From: Leo Prikler Date: Thu, 7 Jan 2021 12:10:20 +0100 Message-Id: <20210107111019.7277-1-leo.prikler@student.tugraz.at> X-Mailer: git-send-email 2.30.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-TUG-Backscatter-control: bt4lQm5Tva3SBgCuw0EnZw X-Spam-Scanner: SpamAssassin 3.003001 X-Spam-Score-relay: -1.9 X-Scanned-By: MIMEDefang 2.74 on 129.27.10.117 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guix@gnu.org List-Id: Bug reports for GNU Guix List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: conjaroy@gmail.com Errors-To: bug-guix-bounces+larch=yhetil.org@gnu.org Sender: "bug-Guix" X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: 3.75 Authentication-Results: aspmx1.migadu.com; dkim=fail (headers rsa verify failed) header.d=tugraz.at header.s=mailrelay header.b=mgEMKW+/; dmarc=fail reason="SPF not aligned (relaxed)" header.from=student.tugraz.at (policy=none); spf=pass (aspmx1.migadu.com: domain of bug-guix-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=bug-guix-bounces@gnu.org X-Migadu-Queue-Id: CDCAC94060F X-Spam-Score: 3.75 X-Migadu-Scanner: scn0.migadu.com X-TUID: adkcqmlsuFBi *gnu/system/shadow.scm (find-duplicates): New variable. (assert-unique-account-names, assert-unique-group-names): New variables. (account-activation): Use them here. --- gnu/system/shadow.scm | 44 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/gnu/system/shadow.scm b/gnu/system/shadow.scm index a69339bc07..183b2cd387 100644 --- a/gnu/system/shadow.scm +++ b/gnu/system/shadow.scm @@ -20,6 +20,7 @@ ;;; along with GNU Guix. If not, see . (define-module (gnu system shadow) + #:use-module ((guix diagnostics) #:select (formatted-message)) #:use-module (guix records) #:use-module (guix gexp) #:use-module (guix store) @@ -34,6 +35,7 @@ #:use-module ((gnu packages admin) #:select (shadow)) #:use-module (gnu packages bash) + #:use-module (ice-9 match) #:use-module (srfi srfi-1) #:use-module (srfi srfi-26) #:use-module (srfi srfi-34) @@ -222,6 +224,46 @@ for a colorful Guile experience.\\n\\n\"))))\n")) (rename-file ".nanorc" ".config/nano/nanorc")) #t)))) +(define (find-duplicates list) + "Find duplicate entries in @var{list}. +Two entries are considered duplicates, if they are @code{equal?} to each other. +This implementation is made asymptotically faster than @code{delete-duplicates} +through the internal use of hash tables." + (let loop ((list list) + ;; We actually modify table in-place, but still allocate it here + ;; so that we only need one level of indentation. + (table (make-hash-table))) + (match list + (() + (hash-fold (lambda (key value seed) + (if (> value 1) + (cons key seed) + seed)) + '() + table)) + ((first . rest) + (hash-set! table first + (1+ (hash-ref table first 0))) + (loop rest table))))) + +(define (assert-unique-account-names users) + (match (find-duplicates (map user-account-name users)) + (() *unspecified*) + (duplicates + (raise + (formatted-message + (G_ "the following accounts appear more than once:~{ ~a~}") + duplicates))))) + +(define (assert-unique-group-names groups) + (match (find-duplicates (map user-group-name groups)) + (() *unspecified*) + (duplicates + (raise + (formatted-message + (G_ "the following groups appear more than once:~{ ~a~}") + duplicates))))) + (define (assert-valid-users/groups users groups) "Raise an error if USERS refer to groups not listed in GROUPS." (let ((groups (list->set (map user-group-name groups)))) @@ -292,6 +334,8 @@ group." (define group-specs (map user-group->gexp groups)) + (assert-unique-account-names accounts) + (assert-unique-group-names groups) (assert-valid-users/groups accounts groups) ;; Add users and user groups. -- 2.30.0