cl-adams/adams

Diff from adams-0.2 to master


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml
new file mode 100644
index 0000000..f27f198
--- /dev/null
+++ b/.github/FUNDING.yml
@@ -0,0 +1 @@
+github: thodg
diff --git a/Makefile b/Makefile
index 0e0c10c..c507555 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,14 @@
-
+PACKAGE = adams
+VERSION = 0.3.2
+RELEASE_DIR = ${PACKAGE}-${VERSION}
+RELEASE_TARBALL = ${PACKAGE}-${VERSION}.tar.gz
+RELEASE_DEPS_TARBALL = ${PACKAGE}-${VERSION}.deps.tar.gz
 PROGRAM = build/adams
 PREFIX = /usr/local
-LISP = sbcl
+LISP = sbcl --dynamic-space-size 2048
 LISP_LOAD = ${LISP} --load
+CLEANFILES = build/*
+DISTCLEANFILES = ${RELEASE_DIR} ${RELEASE_TARBALL} ${RELEASE_DEPS_TARBALL}
 
 all: ${PROGRAM}
 
@@ -15,10 +21,23 @@ build/systems.lisp: prepare-build.lisp adams.asd
 ${PROGRAM}: build.lisp config.lisp build/systems.lisp toplevel.lisp
 	LANG=C.UTF-8 ${LISP_LOAD} build.lisp --quit
 
+install:
+	install -m 0755 ${PROGRAM} ${PREFIX}/bin
+
+release: ${RELEASE_TARBALL} ${RELEASE_DEPS_TARBALL}
+
+${RELEASE_TARBALL}:
+	mkdir ${RELEASE_DIR}
+	find . -name build -prune -or -name '*.lisp' -or -name '*.asd' -or -name '*.md' | cpio -pd ${RELEASE_DIR}
+	tar czf ${RELEASE_TARBALL} ${RELEASE_DIR}
+
+${RELEASE_DEPS_TARBALL}:
+	tar czf ${RELEASE_DEPS_TARBALL} build/*.lisp
+
 clean:
-	rm -rf build/*
+	rm -rf ${CLEANFILES}
 
-install: ${PROGRAM}
-	install -m 0755 ${PROGRAM} ${PREFIX}/bin
+distclean:
+	rm -rf ${DISTCLEANFILES}
 
-.PHONY: all clean deps install ${PROGRAM}
+.PHONY: all clean deps install ${PROGRAM} release
diff --git a/README.md b/README.md
index d44c2cd..d03793b 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,5 @@
-Adams 0.2
-=========
+Adams 0.3.2
+===========
 
 Adams is a UNIX system administration tool written in Common Lisp.
 
@@ -8,12 +8,6 @@ You describe your systems (hosts) using resources having properties.
 The properties are then probed and synchronized by Adams using only
 `/bin/sh` on the remote host, and `/usr/bin/ssh` on the control host.
 
-More viable alternatives include
-[Ansible](https://www.ansible.com/),
-[Chef](https://www.chef.io/)
-and
-[Puppet](https://puppet.com/).
-
 
 Configuration example
 ---------------------
@@ -148,24 +142,8 @@ In your main script :
 ```
 
 
-ISC License
------------
-```
-;;  Copyright YEAR AUTHOR <EMAIL>
-;;
-;;  Permission to use, copy, modify, and distribute this software for any
-;;  purpose with or without fee is hereby granted, provided that the above
-;;  copyright notice and this permission notice appear in all copies.
-;;
-;;  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-;;  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-;;  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-;;  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-;;  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-;;  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-;;  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-;;
-```
+[License](LICENSE.md)
+---------------------
 
 
 Contribute
diff --git a/adams.asd b/adams.asd
index d6590f8..55be707 100644
--- a/adams.asd
+++ b/adams.asd
@@ -67,6 +67,7 @@
 	     (:file "linux" :depends-on ("commands" "defs"))
 	     (:file "openbsd" :depends-on ("commands" "defs"))
 	     (:file "freebsd" :depends-on ("commands" "defs"))
+	     (:file "darwin" :depends-on ("commands" "defs"))
 	     (:file "operations" :depends-on ("commands" "defs"))
 	     (:file "probes"  :depends-on ("commands" "defs"
                                            "stat" "syntaxes"))
diff --git a/build.lisp b/build.lisp
index 8f03252..074ab9d 100644
--- a/build.lisp
+++ b/build.lisp
@@ -18,6 +18,12 @@
 
 (in-package :common-lisp-user)
 
+(declaim (optimize (speed 1)
+                   (space 1)
+                   (safety 3)
+                   (debug 3)
+                   (compilation-speed 0)))
+
 (defun compile-lisp (path)
   (let* ((fasl (make-pathname :type "fasl" :defaults path))
          (fasl (merge-pathnames fasl)))
diff --git a/core/defs.lisp b/core/defs.lisp
index 064f98f..225e1e8 100644
--- a/core/defs.lisp
+++ b/core/defs.lisp
@@ -195,6 +195,7 @@
    (probe-host-locale :properties (:locale))
    (probe-host-packages :properties (:packages))
    (probe-boot-time :properties (:boot-time))
+   (probe-host-homedir :properties (:homedir))
    (probe-host-user :properties (:user))
    (probe-hostname :properties (:hostname)))
   ((op-host-locale :properties (:locale))
@@ -261,3 +262,7 @@
   `(loop
       (unless (remf ,place ,indicator)
         (return))))
+
+;;  Shebang
+
+(set-dispatch-macro-character #\# #\! #'read-line)
diff --git a/core/host.lisp b/core/host.lisp
index abf5e1d..d74ecc2 100644
--- a/core/host.lisp
+++ b/core/host.lisp
@@ -74,6 +74,23 @@
 (defun hostname (&optional (host (current-host)))
   (resource-id (the host host)))
 
+(defun homedir (user &optional (host (current-host)))
+  (str (etypecase (host-os host)
+         (os-darwin "/Users")
+         (os "/home"))
+       "/"
+       (etypecase user
+         (user (resource-id user))
+         (string user))))
+
+(defgeneric probe-host-homedir (host os))
+
+(defmethod probe-host-homedir (host (os os-unix))
+  '(:homedir "/home"))
+
+(defmethod probe-host-homedir (host (os os-darwin))
+  '(:homedir "/Users"))
+
 ;;  Host shell
 
 (defmethod host-shell ((host host))
diff --git a/package.lisp b/package.lisp
index 5ee0cd2..def9eca 100644
--- a/package.lisp
+++ b/package.lisp
@@ -115,6 +115,7 @@
    #:resource-diff
    ;;  Host
    #:current-host
+   #:homedir
    #:host
    #:host-connect
    #:host-disconnect
@@ -145,6 +146,7 @@
    #:ssh-authorized-key
    #:stat
    #:stat<1>
+   #:symlink
    #:+timestamp-offset+
    #:timestamp-to-universal-time
    #:universal-time-to-timestamp
diff --git a/prepare-build.lisp b/prepare-build.lisp
index 65c2147..e3df078 100644
--- a/prepare-build.lisp
+++ b/prepare-build.lisp
@@ -123,10 +123,17 @@
 
 (defmethod collect-sources :around ((x asdf:component))
   (let ((if-feature (asdf::component-if-feature x)))
-    (if if-feature
-        (when (find (the symbol if-feature) *features*)
-          (call-next-method))
-        (call-next-method))))
+    (etypecase if-feature
+      (null
+       (call-next-method))
+      (symbol
+       (when (find (the symbol if-feature) *features*)
+         (call-next-method)))
+      (cons
+       (cond ((string-equal 'not (first if-feature))
+              (unless (find (the symbol (second if-feature)) *features*)
+                (call-next-method)))
+             (t (error "Bad if-feature")))))))
 
 #+nil (collect-sources :adams)
 
@@ -156,10 +163,11 @@
   (let (dependencies)
     (labels ((dfs (name)
                (let ((sys (asdf:find-system name)))
-                 (when sys
+                 (when (and sys (not (find sys dependencies)))
                    (locally (declare (type asdf:system sys))
+                     (format t "~& ~A" sys) (force-output)
                      (map 'nil #'dfs (asdf:system-depends-on sys))
-                     (pushnew sys dependencies))))))
+                     (push sys dependencies))))))
       (dfs name)
       (nreverse dependencies))))
 
@@ -180,6 +188,7 @@
                (load-form `(load ,build-file)))
           (format t "~& ~A~%" sys) (force-output)
           (print load-form out)))
-      (fresh-line out))))
+      (fresh-line out)
+      (force-output out))))
 
 (write-build-systems-file :adams)
diff --git a/shell/shell.lisp b/shell/shell.lisp
index 3f3e1cc..1d87c61 100644
--- a/shell/shell.lisp
+++ b/shell/shell.lisp
@@ -137,7 +137,7 @@ Error: ~S"
     (shell-in (format nil " ; echo \"~%~A $?\"~%" delim) shell)
     (let* ((prev nil)
            (status
-            (loop (let ((line (the string (shell-out/line shell))))
+            (loop (let ((line (shell-out/line shell)))
                     (when (or (null line)
                               (and (< len (length line))
                                    (string= delim line :end2 len)))
@@ -160,7 +160,7 @@ Error: ~S"
 	  (shell-log shell "| ~A~%" line))
 	(dolist (line err)
 	  (shell-log shell "# ~A~&" line))
-        (unless (= 0 status)
+        (unless (and status (= 0 status))
           (shell-log shell   " ⇒ ~D~%" status)))
       (values status out err))))
 
diff --git a/test.lisp b/test.lisp
index 7da15f0..fb184d0 100644
--- a/test.lisp
+++ b/test.lisp
@@ -32,6 +32,11 @@
 (adams:clear-resources)
 (adams:clear-probed)
 
+(resource 'symlink "/home/dx/test-symlink"
+          :owner "dx"
+          :group "dx"
+          :target "../test")
+
 (resource 'host "ams.kmx.io"
           :user "root"
           :hostname "ams"
diff --git a/unix/darwin.lisp b/unix/darwin.lisp
new file mode 100644
index 0000000..85600b3
--- /dev/null
+++ b/unix/darwin.lisp
@@ -0,0 +1,207 @@
+;;
+;;  adams - system administrator written in Common Lisp
+;;
+;;  Copyright 2013,2014,2018,2021 Thomas de Grivel <thoxdg@gmail.com>
+;;
+;;  Permission to use, copy, modify, and distribute this software for any
+;;  purpose with or without fee is hereby granted, provided that the above
+;;  copyright notice and this permission notice appear in all copies.
+;;
+;;  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+;;  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+;;  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+;;  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+;;  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+;;  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+;;  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+;;
+
+(in-package :adams)
+
+(in-re-readtable)
+
+(defmethod echo-command ((host t) (os os-darwin))
+  "printf %s ")
+
+(defmethod probe-hostname ((host host) (os os-darwin))
+  (list :hostname (run-1 "hostname -s")))
+
+(define-resource-class brew-pkg (pkg)
+  ()
+  ((probe-brew-pkg :properties (:ensure :flavor :version)))
+  ((op-brew-pkg :properties (:ensure :version))))
+
+(defgeneric probe-brew-pkg (resource os))
+
+(defgeneric op-brew-pkg (pkg os &key ensure version))
+
+(defmethod probe-host-packages ((host host) (os os-darwin))
+  (list :packages ()))
+
+(defmethod op-host-packages ((host host) (os os-darwin) &key packages)
+  nil)
+
+(defmethod op-hostname ((host host) (os os-unix) &key hostname)
+  (run-as-root "hostname -s " (sh-quote hostname)))
+
+(defmethod probe-group ((group group) (os os-darwin))
+  (let ((id (resource-id group))
+        (ensure :absent)
+        (gid nil))
+    (multiple-value-bind (out status)
+        (run "dscl . -read /Groups/" (sh-quote id))
+      (when (= 0 status)
+        (setq ensure :present)
+        (dolist (line out)
+          (re-bind "PrimaryGroupID: ([0-9]+)" (n) line
+            (setq gid (parse-number n)))))
+      (properties* ensure gid))))
+
+#+nil
+(probe (resource 'group "dx") :gid)
+
+(defmethod probe-user ((user user) (os os-darwin))
+  (let* ((id (resource-id user))
+         (sh-id (sh-quote id))
+         (ensure :absent)
+         uid
+         gid
+         shell
+         home)
+    (multiple-value-bind (out status)
+        (run "dscl . -read /Users/" sh-id)
+      (when (= 0 status)
+        (setq ensure :present)
+        (dolist (line out)
+          (re-bind #~|^UniqueID: ([0-9]+)| (n) line
+            (setq uid (parse-number n)))
+          (re-bind #~|^PrimaryGroupID: ([0-9]+)| (n) line
+            (setq gid (parse-number n)))
+          (re-bind #~|^UserShell: (/[^ \n]+)| (s) line
+            (setq shell s))
+          (re-bind #~|^NFSHomeDirectory: (/[^ \n]+)| (h) line
+            (setq home h)))))
+    (let ((realname (string-trim
+                     '(#\Space #\Newline)
+                     (second (run "dscl . -read /Users/" sh-id
+                                  " RealName")))))
+      (properties* ensure uid gid shell home realname))))
+
+#+nil
+(probe (resource 'user "root") :gid)
+
+(define-syntax id<1>-tr ((#'parse-integer gid) name)
+    #~|([0-9]+)[(]([^)]+)|)
+
+(defmethod probe-user-groups ((user user) (os os-darwin))
+  (let ((id (resource-id user))
+        (groups nil))
+    (unless (eq :absent (get-probed user :ensure))
+      (let ((user-gid (get-probed user :gid))
+            (user-group nil))
+        (with-id<1>-tr (gid name) (run "id " (sh-quote id)
+                                       " | tr ' ,=' '\\n'")
+          (when gid
+            (when (= user-gid gid)
+              (setq user-group (resource 'group name)))
+            (push (resource 'group name) groups)))
+        (setq groups (sort groups #'string< :key #'resource-id)
+              groups (if user-group
+                         (cons user-group
+                               (remove (resource-id user-group)
+                                       groups :key #'resource-id
+                                              :test #'string=))
+                         groups)
+              groups (remove-if (lambda (x)
+                                  (find (resource-id x)
+                                        '("_lpoperator"
+                                          "com.apple.access_ssh"
+                                          "com.apple.sharepoint.group.1"
+                                          "com.apple.sharepoint.group.2"
+                                          "everyone"
+                                          "localaccounts")
+                                        :test #'string=))
+                                groups))))
+    (properties* groups)))
+
+#+nil
+(probe (resource 'user "root") :groups)
+
+(defmethod op-update-group ((group group) (os os-darwin) &key ensure gid)
+  (let ((id (resource-id group)))
+    (run-as-root
+     (join-str
+      " "
+      "dscl ." 
+      (ecase ensure
+        ((:absent)  "-delete")
+        ((:present) "-create")
+        ((nil)      "-change"))
+      (str "/Groups/" (sh-quote id))
+      (when gid
+        `("PrimaryGroupID"
+          ,(unless ensure
+             (sh-quote (get-probed group :gid)))
+          ,(sh-quote gid)))))))
+
+(defmethod op-update-user ((user user) (os os-darwin)
+                           &key ensure uid gid realname home shell
+                             login-class groups)
+  (declare (ignore login-class))
+  (sync-groups)
+  (let* ((id (resource-id user))
+         (sh-id (sh-quote id)))
+    (when ensure
+      (run-as-root
+       "dscl . "
+       (ecase ensure
+         ((:absent)  "-delete ")
+         ((:present) "-create "))
+       (str "/Users/" sh-id)))
+    (when uid
+      (run-as-root
+       "dscl . -create /Users/" sh-id
+       " UniqueID "
+       (sh-quote uid)))
+    (when gid
+      (run-as-root
+       "dscl . -create /Users/" sh-id
+       " PrimaryGroupID "
+       (sh-quote gid)))
+    (when home
+      (run-as-root
+       "dscl . -create /Users/" sh-id
+       " NFSHomeDirectory "
+       (sh-quote home)))
+    (when shell
+      (run-as-root
+       "dscl . -create /Users/" sh-id
+       " UserShell "
+       (sh-quote shell)))
+    (dolist (group (get-probed user :groups))
+      (unless (find (resource-id group) groups)
+        (run-as-root
+         "dscl . -delete /Groups/" (sh-quote (resource-id group))
+         " GroupMembership " sh-id)))
+    (dolist (group groups)
+      (run-as-root
+       "dscl . -append /Groups/" (sh-quote group)
+       " GroupMembership " sh-id))))
+
+#+nil
+(clear-resources)
+
+#+nil
+(describe-probed (resource 'openbsd-pkg "emacs"))
+
+#+nil
+(probe-host-packages *host* (host-os *host*))
+
+#+nil
+(probe *host* :packages)
+
+#+nil
+(map nil #'describe-probed (probe-installed-packages))
+
+#+nil
+(run "pkg_info -q | grep emacs-")
diff --git a/unix/defs.lisp b/unix/defs.lisp
index fb32d1b..ca7e6db 100644
--- a/unix/defs.lisp
+++ b/unix/defs.lisp
@@ -21,21 +21,21 @@
 ;;  Group
 
 (define-resource-class group () ()
-  ((probe-group-in-/etc/group :properties (:ensure :name :passwd :gid :members)))
+  ((probe-group :properties (:ensure :name :passwd :gid :members)))
   ((op-update-group :properties (:ensure :gid))))
 
-(defgeneric probe-group-in-/etc/group (resource os))
+(defgeneric probe-group (resource os))
 (defgeneric op-update-group (resource os &key ensure gid))
 
 ;;  User
 
 (define-resource-class user (resource-container)
   ()
-  ((probe-user-in-/etc/passwd :properties (:ensure :login :uid :gid :realname
-                                                   :home :shell))
-   (probe-user-groups-in-/etc/group :properties (:groups)))
+  ((probe-user :properties (:ensure :login :uid :gid :realname
+                                    :home :shell))
+   (probe-user-groups :properties (:groups)))
   ((op-update-user :properties (:ensure :uid :gid :realname :home :shell
-                                        :login-class :groups))))
+                                :login-class :groups))))
 
 (defgeneric probe-user-in-/etc/passwd (resource os))
 (defgeneric probe-user-groups-in-/etc/group (resource os))
@@ -94,6 +94,19 @@
 (defgeneric op-file-ensure (resource os &key ensure))
 (defgeneric op-file-content (resource os &key content))
 
+;;  Symbolic link
+
+(define-resource-class symlink (vnode)
+  ()
+  ((probe-symlink-target :properties (:target)))
+  ((op-symlink-ensure :properties (:ensure))
+   (op-symlink-target :properties (:target)))
+  ((:op-properties (:ensure :target))))
+
+(defgeneric probe-symlink-target (resource os))
+(defgeneric op-symlink-ensure (resource os &key ensure))
+(defgeneric op-symlink-target (resource os &key target))
+
 ;;  Directory
 
 (define-resource-class directory (vnode)
diff --git a/unix/openbsd.lisp b/unix/openbsd.lisp
index 6ae8cee..168c49a 100644
--- a/unix/openbsd.lisp
+++ b/unix/openbsd.lisp
@@ -20,6 +20,9 @@
 
 (in-re-readtable)
 
+(defmethod echo-command ((host t) (os os-openbsd))
+  "echo -E -n ")
+
 (defmethod run-as-root-command ((host t) (os os-openbsd))
   "doas ")
 
@@ -29,7 +32,7 @@
 (define-resource-class openbsd-pkg (pkg)
   ()
   ((probe-openbsd-pkg :properties (:ensure :flavor :version)))
-  ((op-openbsd-pkg :properties (:ensure))))
+  ((op-openbsd-pkg :properties (:ensure :version))))
 
 (define-syntax pkg_info<1> (name version flavor installed)
   #~|\s*([^-\s]+(?:-[^-0-9\s][^-\s]+)*)-([0-9][^-\s]*)(?:-([^-\s]+))?( \(installed\))?|
@@ -46,11 +49,11 @@
   (let ((id (resource-id pkg))
         (ensure :absent))
     (with-openbsd-pkg-id (id-name id-version id-flavor) (list id)
-      ;(format t "~&id-name ~S id-flavor ~S~%" id-name id-flavor)
+      (format t "~&id-name ~S id-flavor ~S~%" id-name id-flavor)
       (multiple-value-bind (version flavor)
           (with-pkg_info<1> (name version flavor installed)
               (run "pkg_info | egrep " (sh-quote (str "^" id-name)))
-            ;(format t "~&name ~S version ~S flavor ~S installed ~S~%" name version flavor installed)
+            (format t "~&name ~S version ~S flavor ~S installed ~S~%" name version flavor installed)
             (when (and (string= id-name name)
                        (or (and (null id-flavor) (null flavor))
                            (and id-flavor flavor
@@ -61,34 +64,26 @@
               (return (values version flavor))))
         (return (properties* ensure version flavor))))))
 
-(defmethod merge-property-values ((pkg openbsd-pkg)
-                                  (property (eql :versions))
-                                  (old list)
-                                  (new list))
-  (sort (remove-duplicates (append old new))
-        #'string<))
-
 (defmethod match-specified-value ((res host)
                                   (property (eql :packages))
                                   (specified list)
                                   (probed list)
                                   (os os-openbsd))
-  (format t "~&match-specified-value specified ~S~%" specified)
-  (format t "~&match-specified-value probed ~S~%" probed)
-  (force-output)
-  (with-openbsd-pkg-id (name version flavor) specified
-    (unless (find name probed :test #'string=)
-      (return nil)))
+  (dolist (pkg-id specified)
+    (unless (find pkg-id probed :test #'string=)
+      (return-from match-specified-value nil)))
   t)
 
-(defmethod op-openbsd-pkg ((pkg openbsd-pkg) (os os-openbsd) &key ensure)
+(defmethod op-openbsd-pkg ((pkg openbsd-pkg) (os os-openbsd) &key ensure version)
   (with-openbsd-pkg-id (id-name id-version id-flavor) (list (resource-id pkg))
-    (when (and id-flavor (not id-version))
-      (probe pkg :version))
+    (setq version
+	  (or version
+	      id-version
+	      (progn (probe pkg :version)
+		     (get-probed pkg :version))))
     (let ((pkg-string (str id-name
-                           (when (or id-version id-flavor)
-                             `(#\- ,(or id-version
-                                        (get-probed pkg :version))))
+			   (when version
+                             `(#\- ,version))
                            (when id-flavor
                              `(#\- ,id-flavor)))))
       (cond
@@ -114,16 +109,17 @@
 
 (defmethod op-host-packages ((host host) (os os-openbsd) &key packages)
   (with-host host
-    (with-openbsd-pkg-id (name version flavor) packages
-      (let ((pkg (resource 'openbsd-pkg name
-                           :ensure :installed)))
-        (when version
-          (resource 'openbsd-pkg name
-                    :version version))
-        (when flavor
-          (resource 'openbsd-pkg name
-                    :flavor flavor))
-        (sync pkg)))))
+    (dolist (pkg-id packages)
+      (with-openbsd-pkg-id (name version flavor) (list pkg-id)
+	(let ((pkg (resource 'openbsd-pkg pkg-id
+                             :ensure :installed)))
+          (when version
+            (resource 'openbsd-pkg pkg-id
+                      :version version))
+          (when flavor
+            (resource 'openbsd-pkg pkg-id
+                      :flavor flavor))
+          (sync pkg))))))
 
 #+nil
 (clear-resources)
diff --git a/unix/operations.lisp b/unix/operations.lisp
index 7630fcf..5c89101 100644
--- a/unix/operations.lisp
+++ b/unix/operations.lisp
@@ -18,6 +18,23 @@
 
 (in-package :adams)
 
+;;  Echo
+
+(defgeneric echo-command (host os))
+
+(defmethod echo-command ((host t) (os t))
+  "echo -n ")
+
+(defun echo_ (&rest parts)
+  (let* ((host (current-host))
+         (cmd (echo-command host (host-os host))))
+    (str cmd (sh-quote parts))))
+
+(defun echo (&rest parts)
+  (run (echo_ parts)))
+
+;;  Run as root
+
 (defgeneric run-as-root-command (host os))
 
 (defmethod run-as-root-command ((host t) (os os-unix))
@@ -61,13 +78,19 @@
    (join-str " "
              (ecase ensure
                ((:absent)  "userdel")
-               ((:present) "useradd -m")
+               ((:present) `("useradd"
+                             ,(unless (eq :present
+                                          (get-probed
+                                           (resource 'directory (homedir
+                                                                 user))
+                                           :ensure))
+                                "-m")))
                ((nil)      "usermod"))
              (when realname `("-c" ,(sh-quote realname)))
              (when home `("-d" ,(sh-quote home)))
              (when gid `("-g" ,(sh-quote gid)))
              (when login-class `("-L" ,(sh-quote login-class)))
-             (when groups `("-S" ,(join-str "," (mapcar #'sh-quote
+             (when groups `("-G" ,(join-str "," (mapcar #'sh-quote
                                                         groups))))
              (when shell `("-s" ,(sh-quote shell)))
              (when uid `("-u" ,(sh-quote uid)))
@@ -75,6 +98,30 @@
 
 ;;  VNode
 
+(defgeneric vnode-owner (res))
+
+(defmethod vnode-owner ((res vnode))
+  (let ((owner-spec (get-specified res :owner)))
+    (when owner-spec
+      (resource 'user owner-spec))))
+  
+(defgeneric vnode-group (res))
+
+(defmethod vnode-group ((res vnode))
+  (let ((group-spec (get-specified res :group)))
+    (when group-spec
+      (resource 'user group-spec))))
+  
+(defgeneric sync-owner-and-group (res))
+
+(defmethod sync-owner-and-group ((res vnode))
+  (let ((owner (vnode-owner res))
+        (group (vnode-group res)))
+    (when group
+      (sync group))
+    (when owner
+      (sync owner))))
+
 (defmethod op-chown ((res vnode) (os os-unix) &key uid gid owner group
                                                 &allow-other-keys)
   (when (stringp owner)
@@ -85,10 +132,10 @@
     (assert (= uid (get-probed owner :uid))))
   (when (and gid group)
     (assert (= gid (get-probed group :gid))))
-  (when owner
-    (sync owner))
   (when group
     (sync group))
+  (when owner
+    (sync owner))
   (let ((u (or (when owner (resource-id owner))
                uid))
         (g (or (when group (resource-id group))
@@ -101,6 +148,7 @@
 
 (defmethod op-chmod ((res vnode) (os os-unix) &key mode
                                                 &allow-other-keys)
+  (sync-owner-and-group res)
   (run "chmod " (octal (mode-permissions mode)) " "
        (sh-quote (resource-id res))))
 
@@ -124,6 +172,7 @@
     (resource 'directory parent-path)))
 
 (defmethod op-file-ensure ((res file) (os os-unix) &key ensure)
+  (sync-owner-and-group res)
   (sync (parent-directory res))
   (let* ((id (resource-id res))
          (sh-id (sh-quote id)))
@@ -133,17 +182,42 @@
       ((nil)))))
 
 (defmethod op-file-content ((res file) (os os-unix) &key content)
+  (sync-owner-and-group res)
   (sync (parent-directory res))
   (let ((id (resource-id res)))
-    (run "echo -n " (sh-quote content) " > " (sh-quote id))
+    (run (echo_ content) " > " (sh-quote id))
     (when-let (after (get-specified res :after))
       (funcall (the function after) res os))
     (clear-probed res)))
 
+;;  Symlink
+
+(defmethod op-symlink-ensure ((res symlink) (os os-unix)
+                              &key ensure)
+  (sync-owner-and-group res)
+  (sync (parent-directory res))
+  (let* ((id (resource-id res))
+         (sh-id (sh-quote id))
+         (target (get-specified res :target))
+         (sh-target (sh-quote target)))
+    (ecase ensure
+      ((:absent) (run "rm " sh-id))
+      ((:present) (run "ln -s " sh-target " " sh-id))
+      ((nil)))))
+
+(defmethod op-symlink-target ((res symlink) (os os-unix)
+                              &key target)
+  (let* ((id (resource-id res))
+         (sh-id (sh-quote id))
+         (sh-target (sh-quote target)))
+    (run "ln -sf " sh-target " " sh-id)))
+
 ;;  Directory
 
 (defmethod op-directory-ensure ((res directory) (os os-unix)
                                 &key ensure)
+  (sync-owner-and-group res)
+  (sync (parent-directory res))
   (let* ((id (resource-id res))
          (sh-id (sh-quote id)))
     (ecase ensure
diff --git a/unix/probes.lisp b/unix/probes.lisp
index bf30d72..51f032b 100644
--- a/unix/probes.lisp
+++ b/unix/probes.lisp
@@ -20,7 +20,7 @@
 
 ;;  Group
 
-(defmethod probe-group-in-/etc/group ((group group) (os os-unix))
+(defmethod probe-group ((group group) (os os-unix))
   (let ((id (resource-id group))
         (ensure :absent))
     (multiple-value-bind #1=(name passwd gid members)
@@ -32,7 +32,7 @@
 
 ;;  User
 
-(defmethod probe-user-in-/etc/passwd ((user user) (os os-unix))
+(defmethod probe-user ((user user) (os os-unix))
   (let ((id (resource-id user))
         (ensure :absent))
     (multiple-value-bind #1=(login pass uid gid realname home shell)
@@ -46,7 +46,7 @@
           (return (values* #1#))))
       (properties* (ensure . #1#)))))
 
-(defmethod probe-user-groups-in-/etc/group ((user user) (os os-unix))
+(defmethod probe-user-groups ((user user) (os os-unix))
   (let ((id (resource-id user))
         groups)
     (unless (eq :absent (get-probed user :ensure))
@@ -76,10 +76,10 @@
 (defmethod probe-vnode-using-ls ((vnode vnode) (os os-unix))
   (let ((id (resource-id vnode))
         (ensure :absent))
-    (multiple-value-bind #1=(mode links owner group size mtime)
+    (multiple-value-bind #1=(mode links owner group size mtime target)
         (with-ls<1>-lT #.(cons 'name '#1#)
-            (ls "-ldT" (sh-quote id))
-          (when (string= id name)
+            (ls "-ldT" id)
+          (when (string= id (the string name))
             (setq mode (mode (mode-permissions mode))
                   owner (resource 'user owner)
                   group (resource 'group group)
@@ -144,6 +144,14 @@
                         :file-too-large))))
     (properties* content)))
 
+;;  Symlink
+
+(defmethod probe-symlink-target ((symlink symlink) (os os-unix))
+  (let ((target (string-trim '(#\Newline)
+                             (run "readlink "
+                                  (sh-quote (resource-id symlink))))))
+    (properties* target)))
+
 ;;  Directory
 
 (defmethod probe-directory-content ((dir directory) (os os-unix))
diff --git a/unix/syntaxes.lisp b/unix/syntaxes.lisp
index 0e438ce..5895bbd 100644
--- a/unix/syntaxes.lisp
+++ b/unix/syntaxes.lisp
@@ -41,10 +41,11 @@
 			 group
 			 (#'sh-parse-integer size)
 			 (#'chronicity:parse time)
-			 name)
-  #~|^([-a-zA-Z]{10})\s+([0-9]+)\s+(\S+)\s+(\S+)\s+([0-9]+)\s+(\S+\s+\S+ \S+ \S+)\s+(.+)$|
+			 name
+                         target)
+  #~|^([-a-zA-Z]{10})\s+([0-9]+)\s+(\S+)\s+(\S+)\s+([0-9]+)\s+(\S+\s+\S+ \S+ \S+)\s+(.+?)(?: -> (.*))?$|
   "Syntax for `ls -lT` output. See ls(1)."
-  (values name mode links owner group size time))
+  (values name mode links owner group size time target))
 
 (define-syntax stat<1>-r ((#'sh-parse-integer
 			   dev ino mode links uid gid rdev size)