Reworked
[hunchentoot.git] / unix-lw.lisp
blobbbcf26317d3d36fbe346b679b2462a76d91fd2e8
1 ;;; -*- Mode: LISP; Syntax: COMMON-LISP; Package: HUNCHENTOOT; Base: 10 -*-
2 ;;; $Header: /usr/local/cvsrep/hunchentoot/unix-lw.lisp,v 1.5 2008/02/13 16:02:19 edi Exp $
4 ;;; Copyright (c) 2004-2009, Dr. Edmund Weitz. All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :hunchentoot)
32 (fli:define-foreign-function (%setuid "setuid")
33 ((uid :int))
34 :result-type :int)
36 (defun setuid (uid)
37 "Sets the effective user ID of the current process to UID - see
38 setuid\(2)."
39 (unless (zerop (%setuid uid))
40 (parameter-error "setuid failed: ~A" (lw:get-unix-error (lw:errno-value)))))
42 (fli:define-foreign-function (%setgid "setgid")
43 ((gid :int))
44 :result-type :int)
46 (defun setgid (gid)
47 "Sets the effective group ID of the current process to GID -
48 see setgid\(2)."
49 (unless (zerop (%setgid gid))
50 (parameter-error "setgid failed: ~A" (lw:get-unix-error (lw:errno-value)))))
52 (fli:define-c-struct passwd
53 (name (:pointer :char))
54 (passwd (:pointer :char))
55 (uid :int)
56 (gid :int)
57 (gecos (:pointer :char))
58 (dir (:pointer :char))
59 (shell (:pointer :char)))
61 (fli:define-foreign-function (getpwnam "getpwnam")
62 ((name (:reference-pass :ef-mb-string)))
63 :result-type (:pointer passwd))
65 (defun get-uid-from-name (name)
66 "Returns the UID for the user named NAME."
67 (let ((passwd (getpwnam name)))
68 (when (fli:null-pointer-p passwd)
69 (let ((errno (lw:errno-value)))
70 (cond ((zerop errno)
71 (parameter-error "User ~S not found." name))
72 (t (parameter-error "getpwnam failed: ~A" (lw:get-unix-error errno))))))
73 (fli:foreign-slot-value passwd 'uid)))
75 (fli:define-c-struct group
76 (name (:pointer :char))
77 (passwd (:pointer :char))
78 (gid :int)
79 (mem (:pointer (:pointer :char))))
81 (fli:define-foreign-function (getgrnam "getgrnam")
82 ((name (:reference-pass :ef-mb-string)))
83 :result-type (:pointer group))
85 (defun get-gid-from-name (name)
86 "Returns the GID for the group named NAME."
87 (let ((group (getgrnam name)))
88 (when (fli:null-pointer-p group)
89 (let ((errno (lw:errno-value)))
90 (cond ((zerop errno)
91 (parameter-error "Group ~S not found." name))
92 (t (parameter-error "getgrnam failed: ~A" (lw:get-unix-error errno))))))
93 (fli:foreign-slot-value group 'gid)))