Page 2 of 53 FirstFirst 12341252 ... LastLast
Results 11 to 20 of 527

Thread: Account Lease time / Content filtering

  1. #11
    Join Date
    Jun 2007
    Location
    Charlotte, NC, USA
    Beans
    3,121
    Distro
    Ubuntu Development Release

    Account Lease time - Timekeeper Developement

    Quote Originally Posted by .nedberg View Post
    Pretty sure I can help with that! But I might not get back to you before after the weekend.
    Thanks, I really appreciate your help.
    Last edited by crjackson; October 1st, 2008 at 10:38 PM.
    MacPro 5,1 6-Core 3.33GHz, 48GB, AMD HD 7970
    Optical- Apple Super Drive & MCE Internal Blu-ray
    512GB Samsung 840 Pro 1TB WD, 1TB Barracuda
    HP RocketU 1144C USB 3.0/SOLO-X2/OS X 10.10

  2. #12
    Join Date
    Nov 2006
    Location
    Norway
    Beans
    795
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: Account Lease time / Content filtering

    I am at work now, so this is my verion of the script as I remember it from last night. You are right in editing both occurences of 30000 to 10000. This should not have anything to do with the script not working.

    Code:
    #!/bin/bash
    ###
    # timekpr.sh - simple 
    # watches gnome sessions and logs them out once the user has exceeded a set, per day limit
    # /var/lib/timekpr/$username.time hold a count of seconds user has had a gnome session
    # /var/lib/timekpr/$username hold the daily allowed seconds for the user
    #
    # you may need to install notify-send with: $apt-get install libnotify-bin
    #
    # Copyright 2008 Chris Jackson <chris@91courtstreet.net>
    #
    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # See <http://www.gnu.org/licenses/>. 
    #
    
    default_limit=87000 #all day
    grace_period=50
    poll_time=20
    
    #Ubuntu uses alternatives so we look for x-session-manager instead of gnome-session
    SESSION_MANAGER=x-session-manager
    
    # get the usernames and PIDs of sessions
    
    while(true); do
        sleep $poll_time
        pidlists=$( ps --no-heading -fC $SESSION_MANAGER | awk 'BEGIN{ FS=" " } { print $1 "," $2 }' )
        for pidlist in $pidlists; do
            # split username and pid - FIXME - I bet this would be faster with bash arrays and substitution 
            username=$( echo $pidlist | awk 'BEGIN{ FS=","} { print $1}' )
            pid=$( echo $pidlist | awk 'BEGIN{ FS=","} { print $2}' )
            if [[ ! -e "/var/lib/timekpr/$username" ]]
            then 
                echo $default_limit > /var/lib/timekpr/$username
            fi
            
            # if the time file is missing or was last touched yesterday, start over
            if [[ -e "/var/lib/timekpr/$username.time" && `( stat -c '%z' /var/lib/timekpr/$username.time|cut -c9,10 )` == `date +%d` ]]
            then 
                #add $poll_time seconds to it
                timekpr=$(( `cat /var/lib/timekpr/$username.time` + $poll_time ))
                echo $timekpr > /var/lib/timekpr/$username.time
            else
            	timekpr=$poll_time
                echo $timekpr > /var/lib/timekpr/$username.time
            fi
    
            echo $username, $pid, $timekpr
            
            if [[ $timekpr -gt `cat /var/lib/timekpr/$username` ]]
            then
                ## get the display and xauthority used by out session manager
                UDISPLAY=`grep -z DISPLAY \
                    /proc/$pid/environ | sed -e 's/DISPLAY=//'`
                XAUTHORITY=`grep -z XAUTHORITY \
                    /proc/$pid/environ | sed -e 's/XAUTHORITY=//'`
                
                # find DBUS session bus for this session
                DBUS_SESSION_BUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS \
                    /proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'`
                # use it - give a warning, then another one 1/2 way through grace_period
                XAUTHORITY="$XAUTHORITY" DISPLAY="$UDISPLAY" DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
                    notify-send --icon=gtk-dialog-warning --urgency=critical -t 10000 "Daily Time Limit" "Your session time is about to expire! You have $grace_period sec. to save your work and logout."
                sleep $(($grace_period/2))   # FIXME: this gives other sessions a free grace_period added to their accounting
    
                XAUTHORITY="$XAUTHORITY" DISPLAY="$UDISPLAY" DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
                    notify-send --icon=gtk-dialog-warning --urgency=critical -t 10000 "Daily Time Limit" "Your session time is about to expire! You have $(($grace_period/2)) sec. to save your work and logout."
                sleep $(($grace_period/2))   # FIXME: this gives other sessions a free grace_period added to their accounting
    
                XAUTHORITY="$XAUTHORITY" DISPLAY="$UDISPLAY" DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
                    notify-send --icon=gtk-dialog-warning --urgency=critical -t 10000 "Shutting Down" "Shutting down session ($pid) now!" 
                # FIXME - should really check to see if user has logged out yet 
                sleep 10
                kill -HUP $pid    #this is a pretty bad way of killing a gnome-session, but we warned 'em
                
                ## uncomment the following to brutally kill all of the users processes
                sleep 10
                pkill -u $username  
                
                ## killing gnome-session should be more like:
                #DISPLAY=":0" XAUTHORITY="/tmp/.gdmEQ0V5T" SESSION_MANAGER="local/wretched:/tmp/.ICE-unix/$pid" su -c 'gnome-session-save --kill --silent' $username
                ## but this can still leave processes to cleanup - plus it's not easy to get SESSION_MANAGER
            fi
        done
    done
    Open mind, open source!
    Keep control of computer usage with timekpr
    Screensaver for Amarok? AmarokScreenSaver
    My (Norwegian) blog about motorcycles and MotoGP

  3. #13
    Join Date
    Jun 2007
    Location
    Charlotte, NC, USA
    Beans
    3,121
    Distro
    Ubuntu Development Release

    Account Lease time - Timekeeper Developement

    Great. I've already made the modifications. Now I need a method of blocking the login between specific times. Idealy, I would be able to do a different setting for each user, something that would check the date/time stamp in their timer file.

    If not that, then somehow insert a variable that I could adjust to prevent the scrip from resetting at 0000, and use 0600 instead.

    This wouldn't be as effective as being able to lock out certain users between 0000-0600 but it would be an improvement.
    Last edited by crjackson; October 1st, 2008 at 10:38 PM.
    MacPro 5,1 6-Core 3.33GHz, 48GB, AMD HD 7970
    Optical- Apple Super Drive & MCE Internal Blu-ray
    512GB Samsung 840 Pro 1TB WD, 1TB Barracuda
    HP RocketU 1144C USB 3.0/SOLO-X2/OS X 10.10

  4. #14
    Join Date
    Nov 2006
    Location
    Norway
    Beans
    795
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: Account Lease time / Content filtering

    I will have the script ready probably on Sunday or Monday (CET). It will limit login to between, say, 0700 and 2100 (configurable). I might also make it on a "per user" basis, I will have to see what I can manage by then. I am going to my brothers wedding tomorrow and therefore I don't have the time to finish it earlier. I am however close to a solution!
    Open mind, open source!
    Keep control of computer usage with timekpr
    Screensaver for Amarok? AmarokScreenSaver
    My (Norwegian) blog about motorcycles and MotoGP

  5. #15
    Join Date
    Jun 2007
    Location
    Charlotte, NC, USA
    Beans
    3,121
    Distro
    Ubuntu Development Release

    Account Lease time - Timekeeper Developement

    Quote Originally Posted by .nedberg View Post
    I will have the script ready probably on Sunday or Monday (CET). It will limit login to between, say, 0700 and 2100 (configurable). I might also make it on a "per user" basis, I will have to see what I can manage by then. I am going to my brothers wedding tomorrow and therefore I don't have the time to finish it earlier. I am however close to a solution!
    Great! If this works out I really owe you. Send me a PM with your paypal information and I'll make a donation.

    Thanks...
    Last edited by crjackson; October 1st, 2008 at 10:39 PM.
    MacPro 5,1 6-Core 3.33GHz, 48GB, AMD HD 7970
    Optical- Apple Super Drive & MCE Internal Blu-ray
    512GB Samsung 840 Pro 1TB WD, 1TB Barracuda
    HP RocketU 1144C USB 3.0/SOLO-X2/OS X 10.10

  6. #16
    Join Date
    Nov 2006
    Location
    Norway
    Beans
    795
    Distro
    Kubuntu 10.04 Lucid Lynx

    Re: Account Lease time / Content filtering

    OK,

    here is the script:

    /usr/local/bin/timekpr.sh
    Code:
    #!/bin/bash
    ###
    # timekpr.sh - simple 
    # watches gnome sessions and logs them out once the user has exceeded a set, per day limit
    # /var/lib/timekpr/$username.time hold a count of seconds user has had a gnome session
    # /var/lib/timekpr/$username hold the daily allowed seconds for the user
    #
    # you may need to install notify-send with: $apt-get install libnotify-bin
    #
    # Copyright 2008 Chris Jackson <chris@91courtstreet.net>
    #
    # This program is free software: you can redistribute it and/or modify
    # it under the terms of the GNU General Public License as published by
    # the Free Software Foundation, either version 3 of the License, or
    # (at your option) any later version.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    # GNU General Public License for more details.
    #
    # See <http://www.gnu.org/licenses/>. 
    #
    
    default_limit=86400 # All day, new users is given this value
    grace_period=60 # how much time do we give to let the user log out?
    poll_time=20 # How often the script should check the timelogs
    from=7 # When is the users allowed to log in
    to=22 # When is the users no longer allowed to be loged in
    
    
    #Ubuntu uses alternatives so we look for x-session-manager instead of gnome-session
    SESSION_MANAGER=x-session-manager
    
    function logOut() {
    ## get the display and xauthority used by out session manager
                UDISPLAY=`grep -z DISPLAY \
                    /proc/$pid/environ | sed -e 's/DISPLAY=//'`
                XAUTHORITY=`grep -z XAUTHORITY \
                    /proc/$pid/environ | sed -e 's/XAUTHORITY=//'`
                
                echo -ne "\007"
                # find DBUS session bus for this session
                DBUS_SESSION_BUS_ADDRESS=`grep -z DBUS_SESSION_BUS_ADDRESS \
                    /proc/$pid/environ | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//'`
                # use it - give a warning, then another one 1/2 way through grace_period
                XAUTHORITY="$XAUTHORITY" DISPLAY="$UDISPLAY" DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
                    notify-send --icon=gtk-dialog-warning --urgency=critical -t 10000 "$1" "Your session is about to expire! You have $grace_period sec to save your work and logout."
                sleep $(($grace_period/2))   # FIXME: this gives other sessions a free grace_period added to their accounting
                echo -ne "\007"
                XAUTHORITY="$XAUTHORITY" DISPLAY="$UDISPLAY" DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
                    notify-send --icon=gtk-dialog-warning --urgency=critical -t 10000 "$1" "Your session is about to expire! You have $(($grace_period/2)) sec to save your work and logout."
                sleep $(($grace_period/2))   # FIXME: this gives other sessions a free grace_period added to their accounting
    
                XAUTHORITY="$XAUTHORITY" DISPLAY="$UDISPLAY" DBUS_SESSION_BUS_ADDRESS="$DBUS_SESSION_BUS_ADDRESS" \
                    notify-send --icon=gtk-dialog-warning --urgency=critical -t 10 "Shutting Down" "Shutting down session ($pid) now!" 
                # FIXME - should really check to see if user has logged out yet 
                sleep 10
                kill -HUP $pid    #this is a pretty bad way of killing a gnome-session, but we warned 'em
                
                ## uncomment the following to brutally kill all of the users processes
                sleep 10
                pkill -u $username  
                
                ## killing gnome-session should be more like:
                #DISPLAY=":0" XAUTHORITY="/tmp/.gdmEQ0V5T" SESSION_MANAGER="local/wretched:/tmp/.ICE-unix/$pid" su -c 'gnome-session-save --kill --silent' $username
                ## but this can still leave processes to cleanup - plus it's not easy to get SESSION_MANAGER
            
    }
    
    
    	# get the usernames and PIDs of sessions
    
    while(true); do
        pidlists=$( ps --no-heading -fC $SESSION_MANAGER | awk 'BEGIN{ FS=" " } { print $1 "," $2 }' )
        for pidlist in $pidlists; do
    		
    		# split username and pid - FIXME - I bet this would be faster with bash arrays and substitution 
    		username=$( echo $pidlist | awk 'BEGIN{ FS=","} { print $1}' )
    		pid=$( echo $pidlist | awk 'BEGIN{ FS=","} { print $2}' )
    	      
    	      # If the user is not enabled, give him $default_limit
    	      if [[ ! -e "/var/lib/timekpr/$username" ]]; then
    	      	echo $default_limit > /var/lib/timekpr/$username
    	      fi
    	      
    	      if [[ -e "/var/lib/timekpr/$username.time" && `( stat -c '%z' /var/lib/timekpr/$username.time | cut -c9,10 )` == `date +%d` ]]; then
    	      	# the time file exists and was last touched today, add $poll_time seconds to it
    	            timekpr=$(( `cat /var/lib/timekpr/$username.time` + $poll_time ))
    			echo $timekpr > /var/lib/timekpr/$username.time
    		else  # the time file is missing or was last touched yesterday
    			timekpr=$poll_time
    			echo $timekpr > /var/lib/timekpr/$username.time
    	      fi
    
    	      echo $username, $pid, $timekpr
    	      
    	      # Is the user allowed to be loged in at this time?
    	      # We take it for granted that if they are allowed to log in all day ($default_limit) then
    	      # they can log in whenever they want, ie they are normal users
    	      if [ $(cat /var/lib/timekpr/$username) -ne $default_limit ]; then
    	      	# Are we out of hours?
    		      if ( [ $(date +%k) -lt $from ] || [ $(date +%k) -ge $to ] ); then
    		      	# Has the user been given extended log in hours?
    		      	if [[ -e "/var/lib/timekpr/$username.allow" ]]; then
    		      		# Was the extended log in hours given today?
    		      		if [[ $(stat -c '%z' /var/lib/timekpr/$username.allow | cut -c9,10) != $(date +%d) ]]; then
    						logOut "Only log in between $from and $to"
    						rm /var/lib/timekpr/$username.allow
    					fi
    				else
    					# User has not been given extended log in hours
    					logOut "Only log in between $from and $to"
    				fi
    		      fi
    		fi
    		
    		# Is the limit exeeded
    	      if [[ $timekpr -ge `cat /var/lib/timekpr/$username` ]]
    	      then
    			logOut "Daily time limit"
    		fi
    	done
    	# Wait a while before we check again
    	sleep $poll_time
    done
    Limited users can only log in between 7 and 22 (configurable, see line 28 and 29).

    In addition I created a script for "rewards". Run it with 'sudo addtime.sh <username> <minutes>' to add <minutes> to <username>s allowed log in time this day (does not expand log in hours (can't log in after 22)).

    /usr/local/bin/addtime.sh
    Code:
    #!/bin/bash
    
    if [ $# -ne 2 ]; then
    	echo "Usage $(basename $0) <username> <time to add in minutes>"
    	exit
    fi  
    
    adj=$(($2*60))
    now=$(cat /var/lib/timekpr/$1.time)
    new=$(($now-$adj))
    echo $new > /var/lib/timekpr/$1.time
    limit=$(cat /var/lib/timekpr/$1)
    left=$(($limit-$new))
    echo
    echo "$2 minute(s) added to ${1}s account."
    echo "$1 now has $(($left/60)) minute(s) left."
    echo


    And to expand log in hours we have the following script:
    Run it with 'sudo extendlimits.sh <username>' to allow <username> to log in outside of limits (eg before 7 or after 22).

    /usr/local/bin/extendlimits.sh
    Code:
    #!/bin/bash
    
    if [ $# -ne 1 ]; then
    	echo "Usage $(basename $0) <username>"
    	exit
    fi
    
    if [[ -e "/var/lib/timekpr/$1.allow" ]]; then
    	rm "/var/lib/timekpr/$1.allow"
    	echo
    	echo "$1 has been disallowed to log in outside of normal logon hours"
    	echo
    else
    	touch /var/lib/timekpr/$1.allow
    	echo
    	echo "$1 has been allowed to log in outside of normal logon hours"
    	echo
    fi

    Install the script as stated by the original author. I have not tested it this way, but it works when I run it from console. remember to 'chmod 744' all the scripts.

    I recommend you keep $grace_period at least 60 seconds, so that the user has time to save their work and log out.

    Richard Stallman once said: "A developer can either deserve a reward or demand it, but not both." You decide if you want to donate or not, I will not demand it . I had fun making this, and I might get to use it when my daughters grow up (the oldes is only two years old at the moment).

    Any way, my PayPal information is even [at] nedberg.net.
    Last edited by .nedberg; August 18th, 2008 at 06:46 PM. Reason: Added functionality
    Open mind, open source!
    Keep control of computer usage with timekpr
    Screensaver for Amarok? AmarokScreenSaver
    My (Norwegian) blog about motorcycles and MotoGP

  7. #17
    Join Date
    Jun 2007
    Location
    Charlotte, NC, USA
    Beans
    3,121
    Distro
    Ubuntu Development Release

    Account Lease time - Timekeeper Developement

    GREAT! I'll install the new script tonight and see how it goes. I really thank you for your help.
    Attached Files Attached Files
    Last edited by crjackson; October 1st, 2008 at 10:39 PM. Reason: To add attachments of scripts for anyone who wants it.
    MacPro 5,1 6-Core 3.33GHz, 48GB, AMD HD 7970
    Optical- Apple Super Drive & MCE Internal Blu-ray
    512GB Samsung 840 Pro 1TB WD, 1TB Barracuda
    HP RocketU 1144C USB 3.0/SOLO-X2/OS X 10.10

  8. #18
    Join Date
    Jun 2007
    Location
    Charlotte, NC, USA
    Beans
    3,121
    Distro
    Ubuntu Development Release

    Account Lease time - Timekeeper Developement

    .nedberg

    I have a problem. The script won't restart on it's own after a reboot. My youngest has already discovered that she can reboot when she runs out of time and she then gets unlimited time again (unless I run the scrip from a terminal myself).

    I added the line /usr/local/bin/timekpr & to /etc/rc.local but it's not working.

    What am I doing wrong here?
    Last edited by crjackson; October 1st, 2008 at 10:40 PM.
    MacPro 5,1 6-Core 3.33GHz, 48GB, AMD HD 7970
    Optical- Apple Super Drive & MCE Internal Blu-ray
    512GB Samsung 840 Pro 1TB WD, 1TB Barracuda
    HP RocketU 1144C USB 3.0/SOLO-X2/OS X 10.10

  9. #19
    Join Date
    Apr 2007
    Location
    South Carolina, USA
    Beans
    44
    Distro
    Hardy Heron (Ubuntu Development)

    Re: Account Lease time / Content filtering

    This is a great script. I needed something like this. Thanks.

  10. #20
    Join Date
    Jun 2007
    Location
    Charlotte, NC, USA
    Beans
    3,121
    Distro
    Ubuntu Development Release

    Account Lease time - Timekeeper Developement

    Quote Originally Posted by rpainter View Post
    This is a great script. I needed something like this. Thanks.
    If I knew how to do a feature request to Ubuntu I ask for something like this to built in to the GUI where you set up new user accounts. It would be great if you could just include some time limits and time restrictions on the user accounts them selves when setting up the account.
    Last edited by crjackson; October 1st, 2008 at 10:41 PM.
    MacPro 5,1 6-Core 3.33GHz, 48GB, AMD HD 7970
    Optical- Apple Super Drive & MCE Internal Blu-ray
    512GB Samsung 840 Pro 1TB WD, 1TB Barracuda
    HP RocketU 1144C USB 3.0/SOLO-X2/OS X 10.10

Page 2 of 53 FirstFirst 12341252 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •