LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 09-09-2010, 11:22 AM   #1
Felicia1326
LQ Newbie
 
Registered: Jul 2010
Posts: 8
Blog Entries: 1

Rep: Reputation: 0
Question The script from HELL.


I honestly don't have a lot of scripting knowledge, but would really like some at this moment in my career.

So there are several parts
1) look at the /etc/passwd file for users, excluding the default system users. I know how to do this the long hard irritating way (cat /etc/passwd | grep -v for 30 different strings) looking for a shorter more friendly method using an exclude list.

2) Perform an ldap query that cross references with AD to see if the users are currently employed. No clue where to even start on this one.

3) lock the users that do not match accross...I can handle the locking out of the returned users.



Uh help?

Sunshine...
 
Old 09-09-2010, 01:06 PM   #2
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
#s 1 and 3 are trivial:

$ grep 'bash' /etc/passwd (<-- this assumes no system accounts have a bash shell)

# passwd -l <some_user> (<-- see the passwd(1) manpages)

#2: Dunno. Who configured authentication on the system you're managing? Did s/he provide any documentation?
 
Old 09-09-2010, 05:51 PM   #3
Eduardo Nunes
LQ Newbie
 
Registered: Aug 2010
Location: /root/SouthAmerica/Brazil/SaoPaulo/SP
Distribution: Slackware
Posts: 24

Rep: Reputation: 2
Post

Hi Felicia,

I don't know how to perform the ldap lookup neither what does it return.. but I hope this script block can give you a direction..

Code:
#!/bin/bash
# Picks up users that have an encrypted password, and for each do a ldap query.
for user in `egrep -e ".*:..+:.*:.*:.*:.*:.*:.*:$" /etc/shadow | awk -F: '{ print $1 }'`; do
  if [ ldapquery if $user is not employed ]; then
    # Lazy or Unemployed! Lock the user!
    passwd -l $user
  else
    # I can see clearly now the rain is gone... ;)
    passwd -u $user
  fi
done
Do you have a command which produces output only when a user is employed? As my imagination would name "ldapquery $user --check-employed" would return "employed" or nothing if its not employed anymore. Then the If [ -n "`ldapquery $user`" ]; would be the key you are looking for.. On the other side "ldapquery $user --show-unemployed" would turn the if to use -z instead of -n; -n proceed if the string is not empty; and -z proceeds when the string is empty;

You could supress the else to enable user access.. however I think keeping it makes the script synchronize shell logons with ldap correctly all the time it runs (if someone was re-employed, or by a mistake was marked as unemployed).

Regards!

Last edited by Eduardo Nunes; 09-09-2010 at 08:56 PM.
 
Old 09-09-2010, 06:48 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Felicia1326 View Post
I honestly don't have a lot of scripting knowledge, but would really like some at this moment in my career.

So there are several parts
1) look at the /etc/passwd file for users, excluding the default system users. I know how to do this the long hard irritating way (cat /etc/passwd | grep -v for 30 different strings) looking for a shorter more friendly method using an exclude list.

2) Perform an ldap query that cross references with AD to see if the users are currently employed. No clue where to even start on this one.

3) lock the users that do not match accross...I can handle the locking out of the returned users.
if you have Python

Code:
import commands
exclude=['root','bin'] #add more here
users=[]
# store all users required
for line in open("/etc/passwd"):
    s=line.split()
    if not s[0] in users:
        users.append([s[0])

# do ldap query here
....

# do locking here
commands.getstatusoutput('passwd -l %s' %( user_to_lock ) )
For ldap, you can download Python wrapper here. Then read this document to see how its used.
 
Old 09-10-2010, 07:23 PM   #5
ComputerErik
Member
 
Registered: Apr 2005
Location: NYC
Distribution: Debian, RHEL
Posts: 269

Rep: Reputation: 54
It sounds like your ultimate goal is to lock any local account in /etc/passwd (on some Linux machine(s)) if the user doesn't have an active account in AD. Why not just authenticate the users in both environments directly against Active Directory (LDAP) instead? This would greatly simplify management.

All of the included LDAP connection mechanisms included with Linux will work against AD, but can be a pain to setup. However there are some third party solutions (Likewise and Centrify) which make the task a breeze.
 
Old 09-15-2010, 12:05 PM   #6
Felicia1326
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Original Poster
Blog Entries: 1

Rep: Reputation: 0
Thank you all for your input, I am currently working on the ldap query to check the users on our Solaris, Linux and HP-UX boxes against our users in AD. Once I've finished the script I will post it here.

ALthought the grep bash command worked and I only had to add a grep -v root, in order to lock the account I only need the username and '{print $1}' against /etc/passwd returns the user name the uid gid and the First name, guess that's all in column 1

Anyway I decided to just perform an ls on the /home directory and pull in my users this way using -v greps for oracle and a few other system users that would not need to be referenced. This gives me the username only so I can do the passwd -l <username here>
 
Old 09-21-2010, 12:01 PM   #7
Felicia1326
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Original Poster
Blog Entries: 1

Rep: Reputation: 0
Here's what I'm testing and modifying currently. In case anyone has any suggestions...


#!/bin/bash

for i in `awk -F: '$7 ~ /\/bin\/bash/ || $7 ~ /\/bin\/sh/ {print $1}'
/etc/passwd`
do
echo "checking account: $i"
# Does account ($i) exist? set DNE (Does not exist) to 1 if
nothing is found.
DNE= `ldapsearch -LLL -x -H ldap://okcldap1.global.tronox.com:389 -b 'ou=Users,ou=Tronox,dc=Global,dc=Tronox,dc=com' -D 'Domain\username' -w 'pA$$w0rd' (sAMAccountName=$i)' | grep "No such object" | wc -l | awk '{print $1}'`
if [ $DNE = 1]
then
echo "$i should be locked"
fi
done
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What the hell did I do? Tux-Slack Slackware 6 06-03-2007 05:45 AM
A cool script to search within a hell of TXT files guarriman Linux - General 7 12-08-2004 07:21 AM
What the hell?? face_master Linux - General 3 01-28-2003 08:29 AM
Why the hell not... X11 General 8 04-17-2002 11:20 PM
hell with it.... Chijtska Linux From Scratch 5 02-15-2002 08:01 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 05:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration