2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
14 #include <afsconfig.h>
15 #include <afs/param.h>
20 #include "TaAfsAdmSvrInternal.h"
22 #define c100ns1SECOND ((LONGLONG)10000000)
26 * ROUTINES ___________________________________________________________________
30 BOOL IsValidTime (LPSYSTEMTIME pst)
32 return (pst->wYear > 1970) ? TRUE : FALSE;
36 BOOL AfsAdmSvr_Search_Compare (LPTSTR pszName, LPTSTR pszPattern)
38 // An empty pattern matches everyone
40 if (!pszPattern || !*pszPattern)
43 // Cache the expression so we only have to compile it when it changes.
44 // We also add a little convenience measure: you can prepend "!" to any
45 // regexp to negate the entire sense of the regexp--for instance, "!ri"
46 // matches every word which doesn't have "ri" in it.
48 static LPREGEXP pLastExpr = NULL;
49 static TCHAR szLastExpression[ 1024 ] = TEXT("");
50 static BOOL fLastInclusive = TRUE;
52 if (lstrcmp (pszPattern, szLastExpression))
56 lstrcpy (szLastExpression, pszPattern);
57 if ((fLastInclusive = (*pszPattern != TEXT('!'))) == FALSE)
59 pLastExpr = New2 (REGEXP,(pszPattern));
62 if (!pLastExpr) // this shouldn't happen, but be safe anyway
65 return ((pLastExpr->Matches (pszName)) == fLastInclusive);
69 BOOL AfsAdmSvr_Search_Compare (LPIDENT lpi, LPTSTR pszPattern)
71 if (lpi->GetRefCount() == 0)
74 TCHAR szName[ cchSTRING ];
75 switch (lpi->GetType())
78 lpi->GetCellName (szName);
81 lpi->GetLongServerName (szName);
84 lpi->GetServiceName (szName);
87 lpi->GetAggregateName (szName);
90 lpi->GetFilesetName (szName);
93 lpi->GetUserName (szName);
96 lpi->GetGroupName (szName);
102 return AfsAdmSvr_Search_Compare (szName, pszPattern);
106 BOOL AfsAdmSvr_SearchRefresh (ASID idSearchScope, ASOBJTYPE ObjectType, AFSADMSVR_SEARCH_REFRESH SearchRefresh, ULONG *pStatus)
111 if (SearchRefresh == SEARCH_ALL_OBJECTS)
113 if (GetAsidType (idSearchScope) == itCELL)
116 if ((lpCell = ((LPIDENT)idSearchScope)->OpenCell (&status)) == NULL)
120 lpCell->RefreshAll();
124 else // (GetAsidType (idSearchScope) != itCELL)
127 if ((lpServer = ((LPIDENT)idSearchScope)->OpenServer (&status)) == NULL)
131 // What do we need to refresh on this server?
135 lpServer->RefreshStatus();
136 lpServer->RefreshServices();
141 lpServer->RefreshStatus();
142 lpServer->RefreshAggregates();
146 lpServer->RefreshAll();
160 BOOL AfsAdmSvr_Search_AllInCell (LPASIDLIST *ppList, ASID idCell, LPTSTR pszPattern, ULONG *pStatus)
166 for (LPIDENT lpiFind = IDENT::FindFirst (&hEnum); lpiFind; lpiFind = IDENT::FindNext (&hEnum))
168 if (lpiFind->GetCell() != (LPIDENT)idCell)
170 if (!AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
172 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
174 IDENT::FindClose (&hEnum);
182 BOOL AfsAdmSvr_Search_ServersInCell (LPASIDLIST *ppList, ASID idCell, LPTSTR pszPattern, ULONG *pStatus)
188 if ((lpCell = ((LPIDENT)idCell)->OpenCell (&status)) == NULL)
193 for (LPSERVER lpServer = lpCell->ServerFindFirst (&hEnum, TRUE, &status); lpServer; lpServer = lpCell->ServerFindNext (&hEnum))
195 LPIDENT lpiFind = lpServer->GetIdentifier();
196 if (AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
197 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
200 lpCell->ServerFindClose (&hEnum);
201 if (status && (status != ADMITERATORDONE))
212 BOOL AfsAdmSvr_Search_ServicesInCell (LPASIDLIST *ppList, ASID idCell, LPTSTR pszPattern, ULONG *pStatus)
218 for (LPIDENT lpiFind = IDENT::FindFirst (&hEnum); lpiFind; lpiFind = IDENT::FindNext (&hEnum))
220 if (lpiFind->GetType() != itSERVICE)
222 if (lpiFind->GetCell() != (LPIDENT)idCell)
224 if (!AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
226 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
228 IDENT::FindClose (&hEnum);
236 BOOL AfsAdmSvr_Search_PartitionsInCell (LPASIDLIST *ppList, ASID idCell, LPTSTR pszPattern, ULONG *pStatus)
242 for (LPIDENT lpiFind = IDENT::FindFirst (&hEnum); lpiFind; lpiFind = IDENT::FindNext (&hEnum))
244 if (lpiFind->GetType() != itAGGREGATE)
246 if (lpiFind->GetCell() != (LPIDENT)idCell)
248 if (!AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
250 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
252 IDENT::FindClose (&hEnum);
260 BOOL AfsAdmSvr_Search_VolumesInCell (LPASIDLIST *ppList, ASID idCell, LPTSTR pszPattern, ULONG *pStatus)
266 for (LPIDENT lpiFind = IDENT::FindFirst (&hEnum); lpiFind; lpiFind = IDENT::FindNext (&hEnum))
268 if (lpiFind->GetType() != itFILESET)
270 if (lpiFind->GetCell() != (LPIDENT)idCell)
272 if (!AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
274 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
276 IDENT::FindClose (&hEnum);
284 BOOL AfsAdmSvr_Search_UsersInCell (LPASIDLIST *ppList, ASID idCell, LPTSTR pszPattern, ULONG *pStatus)
290 if ((lpCell = ((LPIDENT)idCell)->OpenCell (&status)) == NULL)
295 for (LPUSER lpUser = lpCell->UserFindFirst (&hEnum, TRUE, &status); lpUser; lpUser = lpCell->UserFindNext (&hEnum))
297 LPIDENT lpiFind = lpUser->GetIdentifier();
298 if (AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
299 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
302 lpCell->UserFindClose (&hEnum);
303 if (status && (status != ADMITERATORDONE))
314 BOOL AfsAdmSvr_Search_GroupsInCell (LPASIDLIST *ppList, ASID idCell, LPTSTR pszPattern, ULONG *pStatus)
320 if ((lpCell = ((LPIDENT)idCell)->OpenCell (&status)) == NULL)
325 for (LPPTSGROUP lpGroup = lpCell->GroupFindFirst (&hEnum, TRUE, &status); lpGroup; lpGroup = lpCell->GroupFindNext (&hEnum))
327 LPIDENT lpiFind = lpGroup->GetIdentifier();
328 if (AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
329 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
332 lpCell->GroupFindClose (&hEnum);
333 if (status && (status != ADMITERATORDONE))
344 BOOL AfsAdmSvr_Search_AllInServer (LPASIDLIST *ppList, ASID idServer, LPTSTR pszPattern, ULONG *pStatus)
350 if ((lpServer = ((LPIDENT)idServer)->OpenServer (&status)) == NULL)
355 for (LPAGGREGATE lpAggregate = lpServer->AggregateFindFirst (&hEnum, TRUE, &status); lpAggregate; lpAggregate = lpServer->AggregateFindNext (&hEnum))
357 LPIDENT lpiFind = lpAggregate->GetIdentifier();
358 if (AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
359 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
360 lpAggregate->Close();
362 lpServer->AggregateFindClose (&hEnum);
363 if (status && (status != ADMITERATORDONE))
367 for (LPSERVICE lpService = lpServer->ServiceFindFirst (&hEnum, TRUE, &status); lpService; lpService = lpServer->ServiceFindNext (&hEnum))
369 LPIDENT lpiFind = lpService->GetIdentifier();
370 if (AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
371 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
374 lpServer->ServiceFindClose (&hEnum);
375 if (status && (status != ADMITERATORDONE))
387 BOOL AfsAdmSvr_Search_ServicesInServer (LPASIDLIST *ppList, ASID idServer, LPTSTR pszPattern, ULONG *pStatus)
393 if ((lpServer = ((LPIDENT)idServer)->OpenServer (&status)) == NULL)
398 for (LPSERVICE lpService = lpServer->ServiceFindFirst (&hEnum, TRUE, &status); lpService; lpService = lpServer->ServiceFindNext (&hEnum))
400 LPIDENT lpiFind = lpService->GetIdentifier();
401 if (AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
402 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
405 lpServer->ServiceFindClose (&hEnum);
406 if (status && (status != ADMITERATORDONE))
417 BOOL AfsAdmSvr_Search_PartitionsInServer (LPASIDLIST *ppList, ASID idServer, LPTSTR pszPattern, ULONG *pStatus)
423 if ((lpServer = ((LPIDENT)idServer)->OpenServer (&status)) == NULL)
428 for (LPAGGREGATE lpAggregate = lpServer->AggregateFindFirst (&hEnum, TRUE, &status); lpAggregate; lpAggregate = lpServer->AggregateFindNext (&hEnum))
430 LPIDENT lpiFind = lpAggregate->GetIdentifier();
431 if (AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
432 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
433 lpAggregate->Close();
435 lpServer->AggregateFindClose (&hEnum);
436 if (status && (status != ADMITERATORDONE))
447 BOOL AfsAdmSvr_Search_VolumesInServer (LPASIDLIST *ppList, ASID idServer, LPTSTR pszPattern, ULONG *pStatus)
453 for (LPIDENT lpiFind = IDENT::FindFirst (&hEnum); lpiFind; lpiFind = IDENT::FindNext (&hEnum))
455 if (lpiFind->GetType() != itFILESET)
457 if (lpiFind->GetServer() != (LPIDENT)idServer)
459 if (!AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
461 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
463 IDENT::FindClose (&hEnum);
471 BOOL AfsAdmSvr_Search_VolumesInPartition (LPASIDLIST *ppList, ASID idPartition, LPTSTR pszPattern, ULONG *pStatus)
476 LPAGGREGATE lpAggregate;
477 if ((lpAggregate = ((LPIDENT)idPartition)->OpenAggregate (&status)) == NULL)
482 for (LPFILESET lpFileset = lpAggregate->FilesetFindFirst (&hEnum, TRUE, &status); lpFileset; lpFileset = lpAggregate->FilesetFindNext (&hEnum))
484 LPIDENT lpiFind = lpFileset->GetIdentifier();
485 if (AfsAdmSvr_Search_Compare (lpiFind, pszPattern))
486 AfsAdmSvr_AddToAsidList (ppList, (ASID)lpiFind, 0);
489 lpAggregate->FilesetFindClose (&hEnum);
490 if (status && (status != ADMITERATORDONE))
492 lpAggregate->Close();
501 BOOL AfsAdmSvr_Search_ServerInCell (ASID *pidObject, ASID idCell, LPTSTR pszName, ULONG *pStatus)
507 if ((lpCell = ((LPIDENT)idCell)->OpenCell (&status)) == NULL)
512 if ((lpServer = lpCell->OpenServer (pszName, &status)) == NULL)
516 *pidObject = (ASID)(lpServer->GetIdentifier());
528 BOOL AfsAdmSvr_Search_ServiceInCell (ASID *pidObject, ASID idCell, LPTSTR pszName, ULONG *pStatus)
531 ULONG status = ERROR_INVALID_PARAMETER;
534 for (LPIDENT lpiFind = IDENT::FindFirst (&hEnum); lpiFind; lpiFind = IDENT::FindNext (&hEnum))
536 if (lpiFind->GetType() != itSERVICE)
538 if (lpiFind->GetCell() != (LPIDENT)idCell)
540 if (lpiFind->GetRefCount() == 0)
543 TCHAR szService[ cchNAME ];
544 lpiFind->GetServiceName (szService);
545 if (!lstrcmpi (pszName, szService))
547 *pidObject = (ASID)lpiFind;
552 IDENT::FindClose (&hEnum);
560 BOOL AfsAdmSvr_Search_PartitionInCell (ASID *pidObject, ASID idCell, LPTSTR pszName, ULONG *pStatus)
563 ULONG status = ERROR_INVALID_PARAMETER;
566 for (LPIDENT lpiFind = IDENT::FindFirst (&hEnum); lpiFind; lpiFind = IDENT::FindNext (&hEnum))
568 if (lpiFind->GetType() != itAGGREGATE)
570 if (lpiFind->GetCell() != (LPIDENT)idCell)
572 if (lpiFind->GetRefCount() == 0)
575 TCHAR szAggregate[ cchNAME ];
576 lpiFind->GetAggregateName (szAggregate);
577 if (!lstrcmpi (pszName, szAggregate))
579 *pidObject = (ASID)lpiFind;
584 IDENT::FindClose (&hEnum);
592 BOOL AfsAdmSvr_Search_VolumeInCell (ASID *pidObject, ASID idCell, LPTSTR pszName, ULONG *pStatus)
595 ULONG status = ERROR_INVALID_PARAMETER;
598 for (LPIDENT lpiFind = IDENT::FindFirst (&hEnum); lpiFind; lpiFind = IDENT::FindNext (&hEnum))
600 if (lpiFind->GetType() != itFILESET)
602 if (lpiFind->GetCell() != (LPIDENT)idCell)
604 if (lpiFind->GetRefCount() == 0)
607 TCHAR szFileset[ cchNAME ];
608 lpiFind->GetFilesetName (szFileset);
609 if (!lstrcmpi (szFileset, pszName))
611 *pidObject = (ASID)lpiFind;
616 IDENT::FindClose (&hEnum);
624 BOOL AfsAdmSvr_Search_UserInCell (ASID *pidObject, ASID idCell, LPTSTR pszName, ULONG *pStatus)
630 if ((lpCell = ((LPIDENT)idCell)->OpenCell (&status)) == NULL)
635 if ((lpUser = lpCell->OpenUser (pszName, NULL, &status)) == NULL)
639 *pidObject = (ASID)(lpUser->GetIdentifier());
651 BOOL AfsAdmSvr_Search_GroupInCell (ASID *pidObject, ASID idCell, LPTSTR pszName, ULONG *pStatus)
657 if ((lpCell = ((LPIDENT)idCell)->OpenCell (&status)) == NULL)
662 if ((lpGroup = lpCell->OpenGroup (pszName, &status)) == NULL)
666 *pidObject = (ASID)(lpGroup->GetIdentifier());
678 BOOL AfsAdmSvr_Search_ServiceInServer (ASID *pidObject, ASID idServer, LPTSTR pszName, ULONG *pStatus)
684 if ((lpServer = ((LPIDENT)idServer)->OpenServer (&status)) == NULL)
689 if ((lpService = lpServer->OpenService (pszName, &status)) == NULL)
693 *pidObject = (ASID)(lpService->GetIdentifier());
705 BOOL AfsAdmSvr_Search_PartitionInServer (ASID *pidObject, ASID idServer, LPTSTR pszName, ULONG *pStatus)
711 if ((lpServer = ((LPIDENT)idServer)->OpenServer (&status)) == NULL)
715 LPAGGREGATE lpAggregate;
716 if ((lpAggregate = lpServer->OpenAggregate (pszName, &status)) == NULL)
720 *pidObject = (ASID)(lpAggregate->GetIdentifier());
721 lpAggregate->Close();
732 BOOL AfsAdmSvr_Search_VolumeInServer (ASID *pidObject, ASID idServer, LPTSTR pszName, ULONG *pStatus)
735 ULONG status = ERROR_INVALID_PARAMETER;
738 for (LPIDENT lpiFind = IDENT::FindFirst (&hEnum); lpiFind; lpiFind = IDENT::FindNext (&hEnum))
740 if (lpiFind->GetType() != itFILESET)
742 if (lpiFind->GetServer() != (LPIDENT)idServer)
744 if (lpiFind->GetRefCount() == 0)
747 TCHAR szFileset[ cchNAME ];
748 lpiFind->GetFilesetName (szFileset);
749 if (!lstrcmpi (pszName, szFileset))
751 *pidObject = (ASID)lpiFind;
756 IDENT::FindClose (&hEnum);
764 BOOL AfsAdmSvr_Search_VolumeInPartition (ASID *pidObject, ASID idPartition, LPTSTR pszName, ULONG *pStatus)
769 LPAGGREGATE lpAggregate;
770 if ((lpAggregate = ((LPIDENT)idPartition)->OpenAggregate (&status)) == NULL)
775 if ((lpFileset = lpAggregate->OpenFileset (pszName, &status)) == NULL)
779 *pidObject = (ASID)(lpFileset->GetIdentifier());
782 lpAggregate->Close();
791 BOOL AfsAdmSvr_Search_OneUser (ASID *pidObject, ASID idScope, LPTSTR pszName, ULONG *pStatus)
797 if ((lpCell = ((LPIDENT)idScope)->OpenCell (&status)) == NULL)
802 if ((lpUser = lpCell->OpenUser (pszName, TEXT(""), &status)) == NULL)
806 *pidObject = (ASID)(lpUser->GetIdentifier());
818 BOOL AfsAdmSvr_Search_OneGroup (ASID *pidObject, ASID idScope, LPTSTR pszName, ULONG *pStatus)
824 if ((lpCell = ((LPIDENT)idScope)->OpenCell (&status)) == NULL)
829 if ((lpGroup = lpCell->OpenGroup (pszName, &status)) == NULL)
833 *pidObject = (ASID)(lpGroup->GetIdentifier());
846 void AfsAdmSvr_Search_Advanced (LPASIDLIST *ppList, LPAFSADMSVR_SEARCH_PARAMS pSearchParams)
850 if (ppList && (*ppList) && pSearchParams && (pSearchParams->SearchType != SEARCH_NO_LIMITATIONS))
852 for (size_t iEntry = 0; iEntry < (*ppList)->cEntries; )
856 // Try to grab the properties for this object. If we succeed,
857 // we can determine if the object matches the necessary criteria
858 // to indicate we should keep it in the list.
860 ASID idObject = (*ppList)->aEntries[ iEntry ].idObject;
862 LPASOBJPROP pProperties;
863 if ((pProperties = AfsAdmSvr_GetCurrentProperties (idObject, &status)) != NULL)
865 if ( (pProperties->verProperties >= verPROP_FIRST_SCAN) ||
866 (AfsAdmSvr_ObtainFullProperties (pProperties, &status)) )
868 // We managed to get the full properties for this object.
869 // Now check its properties against our search criteria--
870 // only if it has exactly the necessary criteria will we
873 switch (pSearchParams->SearchType)
875 case SEARCH_EXPIRES_BEFORE:
876 if ((pProperties->Type == TYPE_USER) && (pProperties->u.UserProperties.fHaveKasInfo))
878 if (IsValidTime (&pProperties->u.UserProperties.KASINFO.timeExpires))
881 SystemTimeToFileTime (&pProperties->u.UserProperties.KASINFO.timeExpires, &ftExpires);
883 FILETIME ftThreshhold;
884 SystemTimeToFileTime (&pSearchParams->SearchTime, &ftThreshhold);
886 if (CompareFileTime (&ftExpires, &ftThreshhold) <= 0)
892 case SEARCH_PASSWORD_EXPIRES_BEFORE:
893 if ((pProperties->Type == TYPE_USER) && (pProperties->u.UserProperties.fHaveKasInfo))
895 if (pProperties->u.UserProperties.KASINFO.cdayPwExpire)
897 FILETIME ftPwExpires;
898 SystemTimeToFileTime (&pProperties->u.UserProperties.KASINFO.timeLastPwChange, &ftPwExpires);
900 // A FILETIME is a count-of-100ns-intervals since 1601.
901 // We need to increase the {ftPwExpires} time by the
902 // number of days in KASINFO.cdayPwExpires, so we'll
903 // be adding a big number to our FILETIME structure.
906 ldw.HighPart = ftPwExpires.dwHighDateTime;
907 ldw.LowPart = ftPwExpires.dwLowDateTime;
908 ldw.QuadPart += c100ns1SECOND * (LONGLONG)csec1DAY * (LONGLONG)pProperties->u.UserProperties.KASINFO.cdayPwExpire;
909 ftPwExpires.dwHighDateTime = (DWORD)ldw.HighPart;
910 ftPwExpires.dwLowDateTime = (DWORD)ldw.LowPart;
912 FILETIME ftThreshhold;
913 SystemTimeToFileTime (&pSearchParams->SearchTime, &ftThreshhold);
915 if (CompareFileTime (&ftPwExpires, &ftThreshhold) <= 0)
924 // Okay, we've made our choice--remove it, or not.
927 AfsAdmSvr_RemoveFromAsidListByIndex (ppList, iEntry);