2008-03-18 23:30:34 +03:00
/*
Samba Unix / Linux SMB client library
2006-02-04 01:19:41 +03:00
net ads commands for Group Policy
2008-03-18 23:30:34 +03:00
Copyright ( C ) 2005 - 2008 Guenther Deschner ( gd @ samba . org )
2006-02-04 01:19:41 +03:00
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
2007-07-09 23:25:36 +04:00
the Free Software Foundation ; either version 3 of the License , or
2006-02-04 01:19:41 +03:00
( at your option ) any later version .
2008-03-18 23:30:34 +03:00
2006-02-04 01:19:41 +03:00
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 .
2008-03-18 23:30:34 +03:00
2006-02-04 01:19:41 +03:00
You should have received a copy of the GNU General Public License
2008-03-18 23:30:34 +03:00
along with this program . If not , see < http : //www.gnu.org/licenses/>.
2006-02-04 01:19:41 +03:00
*/
# include "includes.h"
# include "utils/net.h"
2010-07-02 02:32:52 +04:00
# include "ads.h"
2010-05-10 02:07:10 +04:00
# include "../libgpo/gpo.h"
# include "libgpo/gpo_proto.h"
2010-07-01 01:38:57 +04:00
# include "../libds/common/flags.h"
2006-02-04 01:19:41 +03:00
# ifdef HAVE_ADS
2008-05-10 01:22:12 +04:00
static int net_ads_gpo_list_all ( struct net_context * c , int argc , const char * * argv )
2006-02-04 01:19:41 +03:00
{
ADS_STRUCT * ads ;
ADS_STATUS status ;
2006-09-06 17:20:06 +04:00
LDAPMessage * res = NULL ;
2006-02-04 01:19:41 +03:00
int num_reply = 0 ;
2006-09-24 01:06:33 +04:00
LDAPMessage * msg = NULL ;
2006-02-04 01:19:41 +03:00
struct GROUP_POLICY_OBJECT gpo ;
TALLOC_CTX * mem_ctx ;
2006-09-06 17:20:06 +04:00
char * dn ;
2006-09-25 03:44:00 +04:00
const char * attrs [ ] = {
" versionNumber " ,
" flags " ,
" gPCFileSysPath " ,
" displayName " ,
" name " ,
" gPCMachineExtensionNames " ,
" gPCUserExtensionNames " ,
2007-07-14 03:38:19 +04:00
" ntSecurityDescriptor " ,
2006-09-25 03:44:00 +04:00
NULL
} ;
2006-02-04 01:19:41 +03:00
2008-05-21 10:28:15 +04:00
if ( c - > display_usage ) {
2010-01-19 13:43:54 +03:00
d_printf ( " %s \n "
2009-07-30 01:45:41 +04:00
" net ads gpo listall \n "
2010-01-19 13:43:54 +03:00
" %s \n " ,
_ ( " Usage: " ) ,
_ ( " List all GPOs on the DC " ) ) ;
2008-05-21 10:28:15 +04:00
return 0 ;
}
2007-07-14 03:38:19 +04:00
mem_ctx = talloc_init ( " net_ads_gpo_list_all " ) ;
2006-02-04 01:19:41 +03:00
if ( mem_ctx = = NULL ) {
return - 1 ;
}
2008-05-10 01:22:12 +04:00
status = ads_startup ( c , false , & ads ) ;
2006-09-06 17:20:06 +04:00
if ( ! ADS_ERR_OK ( status ) ) {
2006-02-04 01:19:41 +03:00
goto out ;
}
2007-07-14 03:38:19 +04:00
status = ads_do_search_all_sd_flags ( ads , ads - > config . bind_path ,
LDAP_SCOPE_SUBTREE ,
" (objectclass=groupPolicyContainer) " ,
attrs ,
2010-06-03 01:35:44 +04:00
SECINFO_DACL ,
2007-07-14 03:38:19 +04:00
& res ) ;
2006-02-04 01:19:41 +03:00
if ( ! ADS_ERR_OK ( status ) ) {
2009-07-30 01:45:41 +04:00
d_printf ( _ ( " search failed: %s \n " ) , ads_errstr ( status ) ) ;
2006-02-04 01:19:41 +03:00
goto out ;
2008-03-18 23:30:34 +03:00
}
2006-02-04 01:19:41 +03:00
num_reply = ads_count_replies ( ads , res ) ;
2008-03-18 23:30:34 +03:00
2009-07-30 01:45:41 +04:00
d_printf ( _ ( " Got %d replies \n \n " ) , num_reply ) ;
2006-02-04 01:19:41 +03:00
/* dump the results */
2008-03-18 23:30:34 +03:00
for ( msg = ads_first_entry ( ads , res ) ;
msg ;
msg = ads_next_entry ( ads , msg ) ) {
2006-09-06 17:20:06 +04:00
2009-03-18 09:35:03 +03:00
if ( ( dn = ads_get_dn ( ads , mem_ctx , msg ) ) = = NULL ) {
2006-09-06 17:20:06 +04:00
goto out ;
}
status = ads_parse_gpo ( ads , mem_ctx , msg , dn , & gpo ) ;
2006-02-04 01:19:41 +03:00
if ( ! ADS_ERR_OK ( status ) ) {
2009-07-30 01:45:41 +04:00
d_printf ( _ ( " ads_parse_gpo failed: %s \n " ) ,
2008-03-18 23:30:34 +03:00
ads_errstr ( status ) ) ;
2006-02-04 01:19:41 +03:00
goto out ;
2008-03-18 23:30:34 +03:00
}
2006-02-04 01:19:41 +03:00
2013-12-18 18:45:58 +04:00
dump_gpo ( & gpo , 0 ) ;
2006-02-04 01:19:41 +03:00
}
out :
ads_msgfree ( ads , res ) ;
2009-03-18 09:35:03 +03:00
TALLOC_FREE ( mem_ctx ) ;
2006-02-04 01:19:41 +03:00
ads_destroy ( & ads ) ;
2008-03-18 23:30:34 +03:00
2006-02-04 01:19:41 +03:00
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_ads_gpo_list ( struct net_context * c , int argc , const char * * argv )
2007-07-14 03:38:19 +04:00
{
2010-06-29 12:07:22 +04:00
ADS_STRUCT * ads = NULL ;
2007-07-14 03:38:19 +04:00
ADS_STATUS status ;
LDAPMessage * res = NULL ;
TALLOC_CTX * mem_ctx ;
const char * dn = NULL ;
2015-05-07 03:00:06 +03:00
uint32_t uac = 0 ;
uint32_t flags = 0 ;
2007-07-14 03:38:19 +04:00
struct GROUP_POLICY_OBJECT * gpo_list ;
2010-08-26 14:04:11 +04:00
struct security_token * token = NULL ;
2006-09-29 21:15:45 +04:00
2008-05-21 10:28:15 +04:00
if ( argc < 1 | | c - > display_usage ) {
2010-01-19 13:43:54 +03:00
d_printf ( " %s \n %s \n %s " ,
_ ( " Usage: " ) ,
_ ( " net ads gpo list <username|machinename> " ) ,
_ ( " Lists all GPOs for machine/user \n "
2009-07-30 01:45:41 +04:00
" username \t User to list GPOs for \n "
" machinename \t Machine to list GPOs for \n " ) ) ;
2007-07-14 03:38:19 +04:00
return - 1 ;
}
mem_ctx = talloc_init ( " net_ads_gpo_list " ) ;
if ( mem_ctx = = NULL ) {
goto out ;
}
2008-05-10 01:22:12 +04:00
status = ads_startup ( c , false , & ads ) ;
2007-07-14 03:38:19 +04:00
if ( ! ADS_ERR_OK ( status ) ) {
goto out ;
}
status = ads_find_samaccount ( ads , mem_ctx , argv [ 0 ] , & uac , & dn ) ;
if ( ! ADS_ERR_OK ( status ) ) {
goto out ;
}
if ( uac & UF_WORKSTATION_TRUST_ACCOUNT ) {
flags | = GPO_LIST_FLAG_MACHINE ;
}
2009-07-30 01:45:41 +04:00
d_printf ( _ ( " %s: '%s' has dn: '%s' \n " ) ,
( uac & UF_WORKSTATION_TRUST_ACCOUNT ) ? _ ( " machine " ) : _ ( " user " ) ,
2007-07-14 03:38:19 +04:00
argv [ 0 ] , dn ) ;
2008-03-18 23:30:34 +03:00
if ( uac & UF_WORKSTATION_TRUST_ACCOUNT ) {
2013-12-10 21:26:51 +04:00
status = gp_get_machine_token ( ads , mem_ctx , dn , & token ) ;
2008-03-18 23:30:34 +03:00
} else {
status = ads_get_sid_token ( ads , mem_ctx , dn , & token ) ;
}
2007-08-14 18:47:08 +04:00
if ( ! ADS_ERR_OK ( status ) ) {
goto out ;
}
status = ads_get_gpo_list ( ads , mem_ctx , dn , flags , token , & gpo_list ) ;
2007-07-14 03:38:19 +04:00
if ( ! ADS_ERR_OK ( status ) ) {
goto out ;
}
2013-12-18 18:45:58 +04:00
dump_gpo_list ( gpo_list , 0 ) ;
2007-07-14 03:38:19 +04:00
out :
ads_msgfree ( ads , res ) ;
talloc_destroy ( mem_ctx ) ;
ads_destroy ( & ads ) ;
2008-03-18 23:30:34 +03:00
2007-07-14 03:38:19 +04:00
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_ads_gpo_link_get ( struct net_context * c , int argc , const char * * argv )
2006-02-04 01:19:41 +03:00
{
ADS_STRUCT * ads ;
ADS_STATUS status ;
TALLOC_CTX * mem_ctx ;
struct GP_LINK gp_link ;
2008-05-21 10:28:15 +04:00
if ( argc < 1 | | c - > display_usage ) {
2010-01-19 13:43:54 +03:00
d_printf ( " %s \n %s \n %s " ,
_ ( " Usage: " ) ,
_ ( " net ads gpo linkget <container> " ) ,
2019-08-29 22:34:23 +03:00
_ ( " Lists gPLink of a container \n "
2009-07-30 01:45:41 +04:00
" container \t Container to get link for \n " ) ) ;
2006-02-04 01:19:41 +03:00
return - 1 ;
}
mem_ctx = talloc_init ( " add_gpo_link " ) ;
if ( mem_ctx = = NULL ) {
return - 1 ;
}
2008-05-10 01:22:12 +04:00
status = ads_startup ( c , false , & ads ) ;
2006-09-06 17:20:06 +04:00
if ( ! ADS_ERR_OK ( status ) ) {
2006-02-04 01:19:41 +03:00
goto out ;
}
status = ads_get_gpo_link ( ads , mem_ctx , argv [ 0 ] , & gp_link ) ;
if ( ! ADS_ERR_OK ( status ) ) {
2009-07-30 01:45:41 +04:00
d_printf ( _ ( " get link for %s failed: %s \n " ) , argv [ 0 ] ,
2008-03-18 23:30:34 +03:00
ads_errstr ( status ) ) ;
2006-02-04 01:19:41 +03:00
goto out ;
2008-03-18 23:30:34 +03:00
}
2006-02-04 01:19:41 +03:00
2013-12-18 18:45:58 +04:00
dump_gplink ( & gp_link ) ;
2006-02-04 01:19:41 +03:00
out :
talloc_destroy ( mem_ctx ) ;
ads_destroy ( & ads ) ;
return 0 ;
}
2008-05-10 01:22:12 +04:00
static int net_ads_gpo_link_add ( struct net_context * c , int argc , const char * * argv )
2006-02-04 01:19:41 +03:00
{
ADS_STRUCT * ads ;
ADS_STATUS status ;
2015-05-07 03:00:06 +03:00
uint32_t gpo_opt = 0 ;
2006-02-04 01:19:41 +03:00
TALLOC_CTX * mem_ctx ;
2008-05-21 10:28:15 +04:00
if ( argc < 2 | | c - > display_usage ) {
2010-01-19 13:43:54 +03:00
d_printf ( " %s \n %s \n %s " ,
_ ( " Usage: " ) ,
_ ( " net ads gpo linkadd <linkdn> <gpodn> [options] " ) ,
_ ( " Link a container to a GPO \n "
2009-07-30 01:45:41 +04:00
" linkdn \t Container to link to a GPO \n "
" gpodn \t GPO to link container to \n " ) ) ;
d_printf ( _ ( " note: DNs must be provided properly escaped. \n "
" See RFC 4514 for details \n " ) ) ;
2006-02-04 01:19:41 +03:00
return - 1 ;
}
mem_ctx = talloc_init ( " add_gpo_link " ) ;
if ( mem_ctx = = NULL ) {
return - 1 ;
}
if ( argc = = 3 ) {
gpo_opt = atoi ( argv [ 2 ] ) ;
}
2008-05-10 01:22:12 +04:00
status = ads_startup ( c , false , & ads ) ;
2006-09-06 17:20:06 +04:00
if ( ! ADS_ERR_OK ( status ) ) {
2006-02-04 01:19:41 +03:00
goto out ;
}
status = ads_add_gpo_link ( ads , mem_ctx , argv [ 0 ] , argv [ 1 ] , gpo_opt ) ;
if ( ! ADS_ERR_OK ( status ) ) {
2009-07-30 01:45:41 +04:00
d_printf ( _ ( " link add failed: %s \n " ) , ads_errstr ( status ) ) ;
2006-02-04 01:19:41 +03:00
goto out ;
}
out :
talloc_destroy ( mem_ctx ) ;
ads_destroy ( & ads ) ;
return 0 ;
}
2006-09-29 21:15:45 +04:00
#if 0 /* broken */
2008-05-10 01:22:12 +04:00
static int net_ads_gpo_link_delete ( struct net_context * c , int argc , const char * * argv )
2006-02-04 01:19:41 +03:00
{
ADS_STRUCT * ads ;
ADS_STATUS status ;
TALLOC_CTX * mem_ctx ;
2008-05-21 10:28:15 +04:00
if ( argc < 2 | | c - > display_usage ) {
d_printf ( " Usage: \n "
" net ads gpo linkdelete <linkdn> <gpodn> \n "
" Delete a GPO link \n "
" <linkdn> \t Container to delete GPO from \n "
" <gpodn> \t GPO to delete from container \n " ) ;
2006-02-04 01:19:41 +03:00
return - 1 ;
}
mem_ctx = talloc_init ( " delete_gpo_link " ) ;
if ( mem_ctx = = NULL ) {
return - 1 ;
}
2008-05-10 01:22:12 +04:00
status = ads_startup ( c , false , & ads ) ;
2006-09-06 17:20:06 +04:00
if ( ! ADS_ERR_OK ( status ) ) {
2006-02-04 01:19:41 +03:00
goto out ;
}
status = ads_delete_gpo_link ( ads , mem_ctx , argv [ 0 ] , argv [ 1 ] ) ;
if ( ! ADS_ERR_OK ( status ) ) {
d_printf ( " delete link failed: %s \n " , ads_errstr ( status ) ) ;
goto out ;
2008-03-18 23:30:34 +03:00
}
2006-02-04 01:19:41 +03:00
out :
talloc_destroy ( mem_ctx ) ;
ads_destroy ( & ads ) ;
return 0 ;
}
2006-09-29 21:15:45 +04:00
# endif
2019-07-15 23:59:44 +03:00
/*
Arguments :
- struct net_context * : Pointer to net_context *
- argc : Number of command line arguments passed to ' net ads gpo getgpo ' command
- * * argv : Command line argument string passed to ' net ads gpo getgpo ' command
This function performs following operations :
1. Create talloc context using talloc_init
2. Preform ads_startup ( )
3. Call ads_get_gpo ( ) to retrieve gpo details inside ' struct GROUP_POLICY_OBJECT '
4. Call dumps_gpo ( ) to dump GPO on stdout
*/
2008-05-10 01:22:12 +04:00
static int net_ads_gpo_get_gpo ( struct net_context * c , int argc , const char * * argv )
2006-02-04 01:19:41 +03:00
{
ADS_STRUCT * ads ;
ADS_STATUS status ;
TALLOC_CTX * mem_ctx ;
struct GROUP_POLICY_OBJECT gpo ;
2008-05-21 10:28:15 +04:00
if ( argc < 1 | | c - > display_usage ) {
2010-01-19 13:43:54 +03:00
d_printf ( " %s \n %s \n %s " ,
_ ( " Usage: " ) ,
_ ( " net ads gpo getgpo <gpo> " ) ,
2014-03-30 06:03:15 +04:00
_ ( " List specified GPO \n "
2009-07-30 01:45:41 +04:00
" gpo \t \t GPO to list \n " ) ) ;
2006-02-04 01:19:41 +03:00
return - 1 ;
}
2008-05-21 10:28:15 +04:00
mem_ctx = talloc_init ( " ads_gpo_get_gpo " ) ;
2006-02-04 01:19:41 +03:00
if ( mem_ctx = = NULL ) {
return - 1 ;
}
2008-05-10 01:22:12 +04:00
status = ads_startup ( c , false , & ads ) ;
2006-09-06 17:20:06 +04:00
if ( ! ADS_ERR_OK ( status ) ) {
2006-02-04 01:19:41 +03:00
goto out ;
}
if ( strnequal ( argv [ 0 ] , " CN={ " , strlen ( " CN={ " ) ) ) {
status = ads_get_gpo ( ads , mem_ctx , argv [ 0 ] , NULL , NULL , & gpo ) ;
} else {
status = ads_get_gpo ( ads , mem_ctx , NULL , argv [ 0 ] , NULL , & gpo ) ;
}
if ( ! ADS_ERR_OK ( status ) ) {
2009-07-30 01:45:41 +04:00
d_printf ( _ ( " get gpo for [%s] failed: %s \n " ) , argv [ 0 ] ,
2008-03-18 23:30:34 +03:00
ads_errstr ( status ) ) ;
2006-02-04 01:19:41 +03:00
goto out ;
2008-03-18 23:30:34 +03:00
}
2006-02-04 01:19:41 +03:00
2019-07-15 23:59:44 +03:00
dump_gpo ( & gpo , 0 ) ;
2006-09-29 05:42:28 +04:00
2006-02-04 01:19:41 +03:00
out :
talloc_destroy ( mem_ctx ) ;
ads_destroy ( & ads ) ;
return 0 ;
}
2008-05-10 01:22:12 +04:00
int net_ads_gpo ( struct net_context * c , int argc , const char * * argv )
2006-02-04 01:19:41 +03:00
{
2008-06-07 04:25:08 +04:00
struct functable func [ ] = {
2008-05-21 10:28:15 +04:00
{
" getgpo " ,
net_ads_gpo_get_gpo ,
NET_TRANSPORT_ADS ,
2009-07-30 01:45:41 +04:00
N_ ( " List specified GPO " ) ,
N_ ( " net ads gpo getgpo \n "
" List specified GPO " )
2008-05-21 10:28:15 +04:00
} ,
{
" linkadd " ,
net_ads_gpo_link_add ,
NET_TRANSPORT_ADS ,
2009-07-30 01:45:41 +04:00
N_ ( " Link a container to a GPO " ) ,
N_ ( " net ads gpo linkadd \n "
" Link a container to a GPO " )
2008-05-21 10:28:15 +04:00
} ,
#if 0
{
" linkdelete " ,
net_ads_gpo_link_delete ,
NET_TRANSPORT_ADS ,
" Delete GPO link from a container " ,
" net ads gpo linkdelete \n "
" Delete GPO link from a container "
} ,
# endif
{
" linkget " ,
net_ads_gpo_link_get ,
NET_TRANSPORT_ADS ,
2019-08-29 22:34:23 +03:00
N_ ( " Lists gPLink of container " ) ,
2009-07-30 01:45:41 +04:00
N_ ( " net ads gpo linkget \n "
2019-08-29 22:34:23 +03:00
" Lists gPLink of container " )
2008-05-21 10:28:15 +04:00
} ,
{
" list " ,
net_ads_gpo_list ,
NET_TRANSPORT_ADS ,
2009-07-30 01:45:41 +04:00
N_ ( " Lists all GPOs for machine/user " ) ,
N_ ( " net ads gpo list \n "
" Lists all GPOs for machine/user " )
2008-05-21 10:28:15 +04:00
} ,
{
" listall " ,
net_ads_gpo_list_all ,
NET_TRANSPORT_ADS ,
2009-07-30 01:45:41 +04:00
N_ ( " Lists all GPOs on a DC " ) ,
N_ ( " net ads gpo listall \n "
" Lists all GPOs on a DC " )
2008-05-21 10:28:15 +04:00
} ,
{ NULL , NULL , 0 , NULL , NULL }
2006-02-04 01:19:41 +03:00
} ;
2008-06-07 04:25:08 +04:00
return net_run_function ( c , argc , argv , " net ads gpo " , func ) ;
2006-02-04 01:19:41 +03:00
}
2006-09-22 13:34:25 +04:00
# endif /* HAVE_ADS */