Author: jerry
Date: 2007-03-20 15:29:33 +0000 (Tue, 20 Mar 2007)
New Revision: 21888

WebSVN: 
http://websvn.samba.org/cgi-bin/viewcvs.cgi?view=rev&root=samba&rev=21888

Log:
Add the osname and osver options to 'net ads join' as discussed 
on the samba-technical ml.  

I'll add a 'net ads set attribute=value' utility later
rather than the original 'net ads setmachineupn' patch that
was also posted to the tech ml.


Modified:
   branches/SAMBA_3_0/source/utils/net_ads.c
   branches/SAMBA_3_0_25/source/utils/net_ads.c


Changeset:
Modified: branches/SAMBA_3_0/source/utils/net_ads.c
===================================================================
--- branches/SAMBA_3_0/source/utils/net_ads.c   2007-03-20 12:44:40 UTC (rev 
21887)
+++ branches/SAMBA_3_0/source/utils/net_ads.c   2007-03-20 15:29:33 UTC (rev 
21888)
@@ -1118,6 +1118,72 @@
 }
 
 /*******************************************************************
+ Set a machines dNSHostName and servicePrincipalName attributes
+ ********************************************************************/
+
+static ADS_STATUS net_set_os_attributes(TALLOC_CTX *ctx, ADS_STRUCT *ads_s, 
+                                       const char *os_name, const char 
*os_version )
+{
+       ADS_STATUS status = ADS_ERROR(LDAP_SERVER_DOWN);
+       char *new_dn;
+       ADS_MODLIST mods;
+       LDAPMessage *res = NULL;
+       char *dn_string = NULL;
+       const char *machine_name = global_myname();
+       int count;
+       char *os_sp = NULL;
+       
+       if ( !os_name || !os_version ) {
+               return ADS_ERROR(LDAP_NO_MEMORY);
+       }
+       
+       /* Find our DN */
+       
+       status = ads_find_machine_acct(ads_s, &res, machine_name);
+       if (!ADS_ERR_OK(status)) 
+               return status;
+               
+       if ( (count = ads_count_replies(ads_s, res)) != 1 ) {
+               DEBUG(1,("net_set_machine_spn: %d entries returned!\n", count));
+               return ADS_ERROR(LDAP_NO_MEMORY);       
+       }
+       
+       if ( (dn_string = ads_get_dn(ads_s, res)) == NULL ) {
+               DEBUG(1, ("ads_add_machine_acct: ads_get_dn returned NULL 
(malloc failure?)\n"));
+               goto done;
+       }
+       
+       new_dn = talloc_strdup(ctx, dn_string);
+       ads_memfree(ads_s, dn_string);
+       if (!new_dn) {
+               return ADS_ERROR(LDAP_NO_MEMORY);
+       }
+       
+       /* now do the mods */
+       
+       if (!(mods = ads_init_mods(ctx))) {
+               goto done;
+       }
+
+       os_sp = talloc_asprintf( ctx, "Samba %s", SAMBA_VERSION_STRING );
+       
+       /* fields of primary importance */
+       
+       ads_mod_str(ctx, &mods, "operatingSystem", os_name);
+       ads_mod_str(ctx, &mods, "operatingSystemVersion", os_version);
+       if ( os_sp )
+               ads_mod_str(ctx, &mods, "operatingSystemServicePack", os_sp);
+
+       status = ads_gen_mod(ads_s, new_dn, mods);
+
+done:
+       ads_msgfree(ads_s, res);
+       TALLOC_FREE( os_sp );   
+       
+       return status;
+}
+
+/*******************************************************************
   join a domain using ADS (LDAP mods)
  ********************************************************************/
 
@@ -1386,6 +1452,8 @@
        int i;
        fstring dc_name;
        struct in_addr dcip;
+       const char *os_name = NULL;
+       const char *os_version = NULL;
        
        nt_status = check_ads_config();
        if (!NT_STATUS_IS_OK(nt_status)) {
@@ -1427,11 +1495,25 @@
                }
                else if ( !StrnCaseCmp(argv[i], "createcomputer", 
strlen("createcomputer")) ) {
                        if ( (create_in_ou = get_string_param(argv[i])) == NULL 
) {
-                               d_fprintf(stderr, "Please supply a valid OU 
path\n");
+                               d_fprintf(stderr, "Please supply a valid OU 
path.\n");
                                nt_status = NT_STATUS_INVALID_PARAMETER;
                                goto fail;
                        }               
                }
+               else if ( !StrnCaseCmp(argv[i], "osName", strlen("osName")) ) {
+                       if ( (os_name = get_string_param(argv[i])) == NULL ) {
+                               d_fprintf(stderr, "Please supply a operating 
system name.\n");
+                               nt_status = NT_STATUS_INVALID_PARAMETER;
+                               goto fail;
+                       }               
+               }
+               else if ( !StrnCaseCmp(argv[i], "osVer", strlen("osVer")) ) {
+                       if ( (os_version = get_string_param(argv[i])) == NULL ) 
{
+                               d_fprintf(stderr, "Please supply a valid 
operating system version.\n");
+                               nt_status = NT_STATUS_INVALID_PARAMETER;
+                               goto fail;
+                       }               
+               }
                else {
                        d_fprintf(stderr, "Bad option: %s\n", argv[i]);
                        nt_status = NT_STATUS_INVALID_PARAMETER;
@@ -1546,7 +1628,18 @@
                }
        }
 
+       /* Try to set the operatingSystem attributes if asked */
+
+       if ( os_name && os_version ) {
+               status = net_set_os_attributes( ctx, ads, os_name, os_version );
+               if ( !ADS_ERR_OK(status) )  {
+                       d_fprintf(stderr, "Failed to set operatingSystem 
attributes.  "
+                                 "Are you a Domain Admin?\n");
+               }
+       }
+
        /* Now build the keytab, using the same ADS connection */
+
        if (lp_use_kerberos_keytab() && ads_keytab_create_default(ads)) {
                DEBUG(1,("Error creating host keytab!\n"));
        }

Modified: branches/SAMBA_3_0_25/source/utils/net_ads.c
===================================================================
--- branches/SAMBA_3_0_25/source/utils/net_ads.c        2007-03-20 12:44:40 UTC 
(rev 21887)
+++ branches/SAMBA_3_0_25/source/utils/net_ads.c        2007-03-20 15:29:33 UTC 
(rev 21888)
@@ -1118,6 +1118,72 @@
 }
 
 /*******************************************************************
+ Set a machines dNSHostName and servicePrincipalName attributes
+ ********************************************************************/
+
+static ADS_STATUS net_set_os_attributes(TALLOC_CTX *ctx, ADS_STRUCT *ads_s, 
+                                       const char *os_name, const char 
*os_version )
+{
+       ADS_STATUS status = ADS_ERROR(LDAP_SERVER_DOWN);
+       char *new_dn;
+       ADS_MODLIST mods;
+       LDAPMessage *res = NULL;
+       char *dn_string = NULL;
+       const char *machine_name = global_myname();
+       int count;
+       char *os_sp = NULL;
+       
+       if ( !os_name || !os_version ) {
+               return ADS_ERROR(LDAP_NO_MEMORY);
+       }
+       
+       /* Find our DN */
+       
+       status = ads_find_machine_acct(ads_s, &res, machine_name);
+       if (!ADS_ERR_OK(status)) 
+               return status;
+               
+       if ( (count = ads_count_replies(ads_s, res)) != 1 ) {
+               DEBUG(1,("net_set_machine_spn: %d entries returned!\n", count));
+               return ADS_ERROR(LDAP_NO_MEMORY);       
+       }
+       
+       if ( (dn_string = ads_get_dn(ads_s, res)) == NULL ) {
+               DEBUG(1, ("ads_add_machine_acct: ads_get_dn returned NULL 
(malloc failure?)\n"));
+               goto done;
+       }
+       
+       new_dn = talloc_strdup(ctx, dn_string);
+       ads_memfree(ads_s, dn_string);
+       if (!new_dn) {
+               return ADS_ERROR(LDAP_NO_MEMORY);
+       }
+       
+       /* now do the mods */
+       
+       if (!(mods = ads_init_mods(ctx))) {
+               goto done;
+       }
+
+       os_sp = talloc_asprintf( ctx, "Samba %s", SAMBA_VERSION_STRING );
+       
+       /* fields of primary importance */
+       
+       ads_mod_str(ctx, &mods, "operatingSystem", os_name);
+       ads_mod_str(ctx, &mods, "operatingSystemVersion", os_version);
+       if ( os_sp )
+               ads_mod_str(ctx, &mods, "operatingSystemServicePack", os_sp);
+
+       status = ads_gen_mod(ads_s, new_dn, mods);
+
+done:
+       ads_msgfree(ads_s, res);
+       TALLOC_FREE( os_sp );   
+       
+       return status;
+}
+
+/*******************************************************************
   join a domain using ADS (LDAP mods)
  ********************************************************************/
 
@@ -1386,6 +1452,8 @@
        int i;
        fstring dc_name;
        struct in_addr dcip;
+       const char *os_name = NULL;
+       const char *os_version = NULL;
        
        nt_status = check_ads_config();
        if (!NT_STATUS_IS_OK(nt_status)) {
@@ -1427,11 +1495,25 @@
                }
                else if ( !StrnCaseCmp(argv[i], "createcomputer", 
strlen("createcomputer")) ) {
                        if ( (create_in_ou = get_string_param(argv[i])) == NULL 
) {
-                               d_fprintf(stderr, "Please supply a valid OU 
path\n");
+                               d_fprintf(stderr, "Please supply a valid OU 
path.\n");
                                nt_status = NT_STATUS_INVALID_PARAMETER;
                                goto fail;
                        }               
                }
+               else if ( !StrnCaseCmp(argv[i], "osName", strlen("osName")) ) {
+                       if ( (os_name = get_string_param(argv[i])) == NULL ) {
+                               d_fprintf(stderr, "Please supply a operating 
system name.\n");
+                               nt_status = NT_STATUS_INVALID_PARAMETER;
+                               goto fail;
+                       }               
+               }
+               else if ( !StrnCaseCmp(argv[i], "osVer", strlen("osVer")) ) {
+                       if ( (os_version = get_string_param(argv[i])) == NULL ) 
{
+                               d_fprintf(stderr, "Please supply a valid 
operating system version.\n");
+                               nt_status = NT_STATUS_INVALID_PARAMETER;
+                               goto fail;
+                       }               
+               }
                else {
                        d_fprintf(stderr, "Bad option: %s\n", argv[i]);
                        nt_status = NT_STATUS_INVALID_PARAMETER;
@@ -1546,7 +1628,18 @@
                }
        }
 
+       /* Try to set the operatingSystem attributes if asked */
+
+       if ( os_name && os_version ) {
+               status = net_set_os_attributes( ctx, ads, os_name, os_version );
+               if ( !ADS_ERR_OK(status) )  {
+                       d_fprintf(stderr, "Failed to set operatingSystem 
attributes.  "
+                                 "Are you a Domain Admin?\n");
+               }
+       }
+
        /* Now build the keytab, using the same ADS connection */
+
        if (lp_use_kerberos_keytab() && ads_keytab_create_default(ads)) {
                DEBUG(1,("Error creating host keytab!\n"));
        }

Reply via email to