Changeset 12534 in project


Ignore:
Timestamp:
11/16/08 21:10:33 (12 years ago)
Author:
sjamaan
Message:

Implement user switching

File:
1 edited

Legend:

Unmodified
Added
Removed
  • release/4/spiffy/trunk/spiffy.scm

    r12529 r12534  
    4343   server-software root-path server-port index-files mime-type-map
    4444   default-mime-type file-extension-handlers default-host vhost-map
    45    access-log error-log debug-log
     45   access-log error-log debug-log spiffy-user spiffy-group
    4646   handle-directory handle-not-found handle-exception handle-access-logging
    4747   restart-request htmlize)
    4848
    4949(import chicken scheme extras ports files data-structures)
    50 (require-extension srfi-1 srfi-18 posix tcp regex
     50(require-extension srfi-1 srfi-18 tcp regex posix
    5151                   intarweb uri-generic sendfile matchable)
    5252
     
    9393(define error-log (make-parameter (current-error-port)))
    9494(define debug-log (make-parameter #f))
     95(define spiffy-user (make-parameter #f))
     96(define spiffy-group (make-parameter #f))
    9597
    9698;;; Custom handlers
     
    345347                           ("\"" . """) ("&" . "&"))))
    346348
     349;; Do we want this here?
     350(unless (eq? (build-platform) 'msvc)
     351  (set-signal-handler! signal/int (lambda (sig) (exit 1))))
     352
     353(define (switch-user/group user group)
     354  (when group    ; group first, since only superuser can switch groups
     355    (let ((ginfo (group-information group)))
     356      (unless ginfo
     357        (error "Group does not exist" group))
     358      (set! (current-group-id) (list-ref ginfo 2))))
     359  (when user
     360    (let ((uinfo (user-information user)))
     361      (unless uinfo
     362        (error "User does not exist" user))
     363      (setenv "HOME" (list-ref uinfo 5))
     364      (initialize-groups user (list-ref uinfo 3))
     365      (unless group ; Already changed to target group?
     366        (set! (current-group-id) (list-ref uinfo 3)))
     367      (set! (current-user-id) (list-ref uinfo 2)))))
     368
    347369(define (start-server #!key (port (server-port)))
    348370  (parameterize ((load-verbose #f))
    349    (letrec ((listener (tcp-listen port))
    350             (accept-loop (lambda ()
    351                            (receive (in out)
    352                              (tcp-accept listener)
    353                              (thread-start!
    354                               (make-thread (lambda ()
    355                                              (handle-incoming-request in out))))
    356                              (accept-loop)))))
    357      (accept-loop))))
     371    (letrec ((listener (tcp-listen port))
     372             (init (lambda ()
     373                     (switch-user/group (spiffy-user) (spiffy-group))))
     374             (accept-loop (lambda ()
     375                            (receive (in out)
     376                              (tcp-accept listener)
     377                              (thread-start!
     378                               (make-thread (lambda ()
     379                                              (handle-incoming-request in out))))
     380                              (accept-loop)))))
     381      (init)
     382      (accept-loop))))
    358383
    359384)
Note: See TracChangeset for help on using the changeset viewer.