| 1 |
ninoborges |
8 |
## This program will allow you to add a user as an admin of a specific machine. It will
|
| 2 |
|
|
## keep a record of users who have Admin provilages to certian machines and will create
|
| 3 |
|
|
## a calendar entry 1 day in the future with a reminder to remove the admin rights. It
|
| 4 |
|
|
## will also keep a log of activity under the Log_files dir on anxfnb.
|
| 5 |
|
|
|
| 6 |
|
|
|
| 7 |
|
|
import Pmw
|
| 8 |
|
|
import sys,os,time,win32net
|
| 9 |
|
|
from win32com.client import Dispatch
|
| 10 |
|
|
from win32com.client import constants
|
| 11 |
|
|
from win32com.client import gencache
|
| 12 |
|
|
from tkMessageBox import showerror,showinfo,showwarning
|
| 13 |
|
|
|
| 14 |
|
|
def Main():
|
| 15 |
|
|
root = Pmw.initialise()
|
| 16 |
|
|
MainNb = Pmw.NoteBook(root)
|
| 17 |
|
|
MainNb.pack()
|
| 18 |
|
|
AddPage = MainNb.add('Add Admin')
|
| 19 |
|
|
MainNb.tab('Add Admin').focus_set()
|
| 20 |
|
|
RevokePage = MainNb.add('Revoke Admin')
|
| 21 |
|
|
About = MainNb.add('About')
|
| 22 |
|
|
copyRightText = copyRightInfo()
|
| 23 |
|
|
groupA = Pmw.Group(About, tag_text = 'About Make_Admin')
|
| 24 |
|
|
groupA.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
|
| 25 |
|
|
copyRight = Pmw.LabeledWidget(groupA.interior(), labelpos = 'w',label_text = copyRightText)
|
| 26 |
|
|
copyRight.pack()
|
| 27 |
|
|
group = Pmw.Group(AddPage, tag_text = 'Add Admin Privilages')
|
| 28 |
|
|
group.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
|
| 29 |
|
|
groupR = Pmw.Group(RevokePage, tag_text = 'Revoke Admin Privilages')
|
| 30 |
|
|
groupR.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
|
| 31 |
|
|
UserNameA = Pmw.EntryField(group.interior(), labelpos = 'w',label_text = 'User Name: ')
|
| 32 |
|
|
MachineNameA =Pmw.EntryField(group.interior(), labelpos = 'w',label_text = 'Machine Name: ')
|
| 33 |
|
|
UserNameA.pack(padx = 20, pady = 10, anchor="w")
|
| 34 |
|
|
MachineNameA.pack(padx = 20, pady = 10, anchor="w")
|
| 35 |
|
|
UserNameR = Pmw.EntryField(groupR.interior(), labelpos = 'w',label_text = 'User Name: ')
|
| 36 |
|
|
MachineNameR =Pmw.EntryField(groupR.interior(), labelpos = 'w',label_text = 'Machine Name: ')
|
| 37 |
|
|
UserNameR.pack(padx = 20, pady = 10, anchor="w")
|
| 38 |
|
|
MachineNameR.pack(padx = 20, pady = 10, anchor="w")
|
| 39 |
|
|
NotesA = Pmw.ScrolledText(group.interior(),labelpos = 'nw',label_text = 'Notes...', usehullsize = 1,hull_width = 214,hull_height = 75)
|
| 40 |
|
|
NotesA.pack(padx = 20, pady = 0, anchor="w")
|
| 41 |
|
|
NotesR = Pmw.ScrolledText(groupR.interior(),labelpos = 'nw',label_text = 'Notes...', usehullsize = 1,hull_width = 214,hull_height = 75)
|
| 42 |
|
|
NotesR.pack(padx = 20, pady = 0, anchor="w")
|
| 43 |
|
|
buttonBoxA = Pmw.ButtonBox(group.interior())
|
| 44 |
|
|
buttonBoxA.pack()
|
| 45 |
|
|
buttonBoxA.add('OK',command = (lambda User = UserNameA, Machine = MachineNameA, Option = "+", Notes = NotesA:HandleCallback(User,Machine,Option,Notes)))
|
| 46 |
|
|
buttonBoxA.add('Cancel',command = root.destroy)
|
| 47 |
|
|
buttonBoxR = Pmw.ButtonBox(groupR.interior())
|
| 48 |
|
|
buttonBoxR.pack()
|
| 49 |
|
|
buttonBoxR.add('OK',command = (lambda User = UserNameR, Machine = MachineNameR, Option = "-", Notes = NotesR:HandleCallback(User,Machine,Option,Notes)))
|
| 50 |
|
|
buttonBoxR.add('Cancel',command = root.destroy)
|
| 51 |
|
|
#MainNb.setnaturalsize()
|
| 52 |
|
|
root.title('Make Admin Console')
|
| 53 |
|
|
root.mainloop()
|
| 54 |
|
|
|
| 55 |
|
|
def copyRightInfo():
|
| 56 |
|
|
mainText = """Make_Admin.exe
|
| 57 |
|
|
|
| 58 |
|
|
Version 0.2
|
| 59 |
|
|
Copyright (C) 2002 Emanuel Borges
|
| 60 |
|
|
All Rights Reserved
|
| 61 |
|
|
|
| 62 |
|
|
This software is released under the terms of the GPL. Which states
|
| 63 |
|
|
that you can Copy, Distribute and even modify this code as long as
|
| 64 |
|
|
you never redistribute the original or modified code as closed source.
|
| 65 |
|
|
|
| 66 |
|
|
For information about this program or to report bugs contact:
|
| 67 |
|
|
Emanuel Borges
|
| 68 |
|
|
email: Eaborges@hotmail.com
|
| 69 |
|
|
"""
|
| 70 |
|
|
return mainText
|
| 71 |
|
|
|
| 72 |
|
|
def HandleCallback(UserObj, MachineObj, Option, NotesObj):
|
| 73 |
|
|
User = UserObj.get()
|
| 74 |
|
|
UserObj.clear()
|
| 75 |
|
|
Machine = MachineObj.get()
|
| 76 |
|
|
MachineObj.clear()
|
| 77 |
|
|
Notes = NotesObj.get()
|
| 78 |
|
|
NotesObj.clear()
|
| 79 |
|
|
print User,Machine
|
| 80 |
|
|
(Today,Tomorrow) = ConvertDate()
|
| 81 |
|
|
Today = Today + ", 2003 03:00 pm"
|
| 82 |
|
|
Tomorrow = Tomorrow + ", 2003 03:00 pm"
|
| 83 |
|
|
if Option == "+":
|
| 84 |
|
|
MakeAdmin(User, Machine)
|
| 85 |
|
|
gencache.EnsureModule('{00062FFF-0000-0000-C000-000000000046}', 0, 9, 1)
|
| 86 |
|
|
o=Dispatch("Outlook.Application.10")
|
| 87 |
|
|
notes = "On %s you added %s as an admin of machine %s.\n\nNotes...\n%s\n\n\n"% (Today, User, Machine, Notes)
|
| 88 |
|
|
LogFile(notes)
|
| 89 |
|
|
CalendarEntry(o,"Remove %s 's admin rights of machine %s"% (User,Machine),"Make_Admin.exe",Tomorrow,notes)
|
| 90 |
|
|
else:
|
| 91 |
|
|
RevokeAdmin(User, Machine)
|
| 92 |
|
|
notes = "On %s you removed %s as an admin of machine %s.\n\n\n"% (Today, User, Machine)
|
| 93 |
|
|
LogFile(notes)
|
| 94 |
|
|
|
| 95 |
|
|
def LogFile(notes):
|
| 96 |
|
|
output = open(r'\\ric1\app\log_files\Make_Admin.log','a')
|
| 97 |
|
|
output.write(notes)
|
| 98 |
|
|
output.close()
|
| 99 |
|
|
|
| 100 |
|
|
def CalendarEntry(self,sub,loc,starttime,notes = ""):
|
| 101 |
|
|
appointment = self.CreateItem(constants.olAppointmentItem)
|
| 102 |
|
|
appointment.Subject = sub
|
| 103 |
|
|
appointment.Location = loc
|
| 104 |
|
|
appointment.Start = starttime
|
| 105 |
|
|
appointment.Body = notes
|
| 106 |
|
|
#appointment.End = endtime
|
| 107 |
|
|
appointment.Save()
|
| 108 |
|
|
|
| 109 |
|
|
def ConvertDate():
|
| 110 |
|
|
now = time.localtime()
|
| 111 |
|
|
date = now[1]
|
| 112 |
|
|
date2 = now[2]
|
| 113 |
|
|
date3 = now[0]
|
| 114 |
|
|
tomorrow = date2 + 1
|
| 115 |
|
|
tomorrow = str(date) + " " + str(tomorrow)
|
| 116 |
|
|
today = str(date) + " " + str(date2)
|
| 117 |
|
|
return today, tomorrow
|
| 118 |
|
|
|
| 119 |
|
|
def MakeAdmin(User, Machine):
|
| 120 |
|
|
print "Adding %s as a local administrator of machine %s..."%(User,Machine)
|
| 121 |
|
|
try:
|
| 122 |
|
|
win32net.NetLocalGroupAddMembers(Machine,'Administrators',3,[{'domainandname' : u'MWBB.DOM\\%s'% User}])
|
| 123 |
|
|
print "%s has been added to %s sucessfully!"%(User,Machine)
|
| 124 |
|
|
except:
|
| 125 |
|
|
print "%s is already a member of the administrators group on %s!"%(User,Machine)
|
| 126 |
|
|
# The commented code here has been retired because MS's cusrmgr dosent always work. So I'm rewriting it.
|
| 127 |
|
|
## print "Adding %s as a local administrator of machine %s..."%(User,Machine)
|
| 128 |
|
|
## status = os.system('cusrmgr.exe -u %s -m \\\%s -alg Administrators'%(User,Machine))
|
| 129 |
|
|
## #print 'cusrmgr.exe -u %s -m \\\%s -alg Administrators'%(user,machine)
|
| 130 |
|
|
##
|
| 131 |
|
|
## ## If its sucessful
|
| 132 |
|
|
## if status == 0:
|
| 133 |
|
|
## print "%s has been added to %s sucessfully!"%(User,Machine)
|
| 134 |
|
|
## showinfo('Success!',"%s has been added to %s sucessfully!"%(User,Machine))
|
| 135 |
|
|
##
|
| 136 |
|
|
## ## If it sees that the user account is already a local admin.
|
| 137 |
|
|
## elif status == 1378:
|
| 138 |
|
|
## print "%s is already a member of the administrators group on %s!"%(User,Machine)
|
| 139 |
|
|
## showwarning('Warning!',"%s is already a member of the administrators group on %s!"%(User,Machine))
|
| 140 |
|
|
##
|
| 141 |
|
|
## ## If it fails
|
| 142 |
|
|
## else:
|
| 143 |
|
|
## print "Adding %s as a local administrator of machine %s failed!"%(User,Machine)
|
| 144 |
|
|
## showerror('ERROR!!',"Adding %s as a local administrator of machine %s failed!"%(User,Machine))
|
| 145 |
|
|
|
| 146 |
|
|
def RevokeAdmin(User, Machine):
|
| 147 |
|
|
print "Removing %s as a local administrator of machine %s..."%(User,Machine)
|
| 148 |
|
|
try:
|
| 149 |
|
|
win32net.NetLocalGroupDelMembers(Machine,'Administrators',['MWBB.DOM\\%s'%User])
|
| 150 |
|
|
print "%s has been removed from %s sucessfully!"%(User,Machine)
|
| 151 |
|
|
except:
|
| 152 |
|
|
print "Removing %s as a local administrator of machine %s has failed!"%(User,Machine)
|
| 153 |
|
|
# The commented code here has been retired because MS's cusrmgr dosent always work. So I'm rewriting it.
|
| 154 |
|
|
## status = os.system('cusrmgr.exe -u %s -m \\\%s -dlg Administrators'%(User,Machine))
|
| 155 |
|
|
## print status
|
| 156 |
|
|
## if status == 0:
|
| 157 |
|
|
## print "%s has been removed from %s sucessfully!"%(User,Machine)
|
| 158 |
|
|
## showinfo('Success!',"%s has been removed from %s sucessfully!"%(User,Machine))
|
| 159 |
|
|
## else:
|
| 160 |
|
|
## print "Removing %s as a local administrator of machine %s has failed!"%(User,Machine)
|
| 161 |
|
|
|
| 162 |
|
|
|
| 163 |
|
|
if __name__ == '__main__':
|
| 164 |
|
|
#UserName = sys.argv[1]
|
| 165 |
|
|
#MachineName = sys.argv[2]
|
| 166 |
|
|
#RevokeAdmin(UserName,MachineName)
|
| 167 |
|
|
Main() |