Cosmetic: Fix a const warning.
[sfrench/samba-autobuild/.git] / source / nsswitch / wbinfo.c
index f53379937043fb1a80e3a91579196faf809f5b8e..81626998b3b80241f74c6ea160600bc541e450ba 100644 (file)
@@ -3,7 +3,7 @@
 
    Winbind status program.
 
-   Copyright (C) Tim Potter      2000-2002
+   Copyright (C) Tim Potter      2000-2003
    Copyright (C) Andrew Bartlett 2002
    
    This program is free software; you can redistribute it and/or modify
@@ -136,6 +136,37 @@ static BOOL wbinfo_get_usergroups(char *user)
        return True;
 }
 
+
+/* List group SIDs a user SID is a member of */
+static BOOL wbinfo_get_usersids(char *user_sid)
+{
+       struct winbindd_request request;
+       struct winbindd_response response;
+       NSS_STATUS result;
+       int i;
+       const char *s;
+
+       ZERO_STRUCT(response);
+
+       /* Send request */
+       fstrcpy(request.data.sid, user_sid);
+
+       result = winbindd_request(WINBINDD_GETUSERSIDS, &request, &response);
+
+       if (result != NSS_STATUS_SUCCESS)
+               return False;
+
+       s = response.extra_data;
+       for (i = 0; i < response.data.num_entries; i++) {
+               d_printf("%s\n", s);
+               s += strlen(s) + 1;
+       }
+
+       SAFE_FREE(response.extra_data);
+
+       return True;
+}
+
 /* Convert NetBIOS name to IP */
 
 static BOOL wbinfo_wins_byname(char *name)
@@ -219,15 +250,20 @@ static BOOL wbinfo_list_domains(void)
 
 
 /* show sequence numbers */
-static BOOL wbinfo_show_sequence(void)
+static BOOL wbinfo_show_sequence(const char *domain)
 {
+       struct winbindd_request  request;
        struct winbindd_response response;
 
        ZERO_STRUCT(response);
+       ZERO_STRUCT(request);
+
+       if ( domain )
+               fstrcpy( request.domain_name, domain );
 
        /* Send request */
 
-       if (winbindd_request(WINBINDD_SHOW_SEQUENCE, NULL, &response) !=
+       if (winbindd_request(WINBINDD_SHOW_SEQUENCE, &request, &response) !=
            NSS_STATUS_SUCCESS)
                return False;
 
@@ -242,6 +278,44 @@ static BOOL wbinfo_show_sequence(void)
        return True;
 }
 
+/* Show domain info */
+
+static BOOL wbinfo_domain_info(const char *domain_name)
+{
+       struct winbindd_request request;
+       struct winbindd_response response;
+
+       ZERO_STRUCT(request);
+       ZERO_STRUCT(response);
+
+       fstrcpy(request.domain_name, domain_name);
+
+       /* Send request */
+
+       if (winbindd_request(WINBINDD_DOMAIN_INFO, &request, &response) !=
+           NSS_STATUS_SUCCESS)
+               return False;
+
+       /* Display response */
+
+       d_printf("Name              : %s\n", response.data.domain_info.name);
+       d_printf("Alt_Name          : %s\n", response.data.domain_info.alt_name);
+
+       d_printf("SID               : %s\n", response.data.domain_info.sid);
+
+       d_printf("Active Directory  : %s\n",
+                response.data.domain_info.active_directory ? "Yes" : "No");
+       d_printf("Native            : %s\n",
+                response.data.domain_info.native_mode ? "Yes" : "No");
+
+       d_printf("Primary           : %s\n",
+                response.data.domain_info.primary ? "Yes" : "No");
+
+       d_printf("Sequence          : %d\n", response.data.domain_info.sequence_number);
+
+       return True;
+}
+
 /* Check trust account password */
 
 static BOOL wbinfo_check_secret(void)
@@ -410,7 +484,7 @@ static BOOL wbinfo_lookupname(char *name)
 
        /* Display response */
 
-       d_printf("%s %d\n", response.data.sid.sid, response.data.sid.type);
+       d_printf("%s %s (%d)\n", response.data.sid.sid, sid_type_lookup(response.data.sid.type), response.data.sid.type);
 
        return True;
 }
@@ -481,9 +555,18 @@ static BOOL wbinfo_auth_crap(char *username)
                
        parse_wbinfo_domain_user(username, name_domain, name_user);
 
-       fstrcpy(request.data.auth_crap.user, name_user);
+       if (push_utf8_fstring(request.data.auth_crap.user, name_user) == -1) {
+               d_printf("unable to create utf8 string for '%s'\n",
+                        name_user);
+               return False;
+       }
 
-       fstrcpy(request.data.auth_crap.domain, name_domain);
+       if (push_utf8_fstring(request.data.auth_crap.domain, 
+                             name_domain) == -1) {
+               d_printf("unable to create utf8 string for '%s'\n",
+                        name_domain);
+               return False;
+       }
 
        generate_random_buffer(request.data.auth_crap.chal, 8, False);
         
@@ -682,17 +765,27 @@ static BOOL wbinfo_remove_user_from_group(char *string)
 
 /* Print domain users */
 
-static BOOL print_domain_users(void)
+static BOOL print_domain_users(const char *domain)
 {
+       struct winbindd_request request;
        struct winbindd_response response;
        const char *extra_data;
        fstring name;
 
        /* Send request to winbind daemon */
 
+       ZERO_STRUCT(request);
        ZERO_STRUCT(response);
+       
+       if (domain) {
+               /* '.' is the special sign for our own domwin */
+               if ( strequal(domain, ".") )
+                       fstrcpy( request.domain_name, lp_workgroup() );
+               else
+                       fstrcpy( request.domain_name, domain );
+       }
 
-       if (winbindd_request(WINBINDD_LIST_USERS, NULL, &response) !=
+       if (winbindd_request(WINBINDD_LIST_USERS, &request, &response) !=
            NSS_STATUS_SUCCESS)
                return False;
 
@@ -713,15 +806,24 @@ static BOOL print_domain_users(void)
 
 /* Print domain groups */
 
-static BOOL print_domain_groups(void)
+static BOOL print_domain_groups(const char *domain)
 {
+       struct winbindd_request  request;
        struct winbindd_response response;
        const char *extra_data;
        fstring name;
 
+       ZERO_STRUCT(request);
        ZERO_STRUCT(response);
 
-       if (winbindd_request(WINBINDD_LIST_GROUPS, NULL, &response) !=
+       if (domain) {
+               if ( strequal(domain, ".") )
+                       fstrcpy( request.domain_name, lp_workgroup() );
+               else
+                       fstrcpy( request.domain_name, domain );
+       }
+
+       if (winbindd_request(WINBINDD_LIST_GROUPS, &request, &response) !=
            NSS_STATUS_SUCCESS)
                return False;
 
@@ -744,20 +846,26 @@ static BOOL print_domain_groups(void)
 
 static BOOL wbinfo_set_auth_user(char *username)
 {
-       char *password;
+       const char *password;
+       char *p;
        fstring user, domain;
 
        /* Separate into user and password */
 
        parse_wbinfo_domain_user(username, domain, user);
 
-       password = strchr(user, '%');
+       p = strchr(user, '%');
 
-       if (password) {
-               *password = 0;
-               password++;
-       } else
-               password = "";
+       if (p != NULL) {
+               *p = 0;
+               password = p+1;
+       } else {
+               char *thepass = getpass("Password: ");
+               if (thepass) {
+                       password = thepass;     
+               } else
+                       password = "";
+       }
 
        /* Store or remove DOMAIN\username%password in secrets.tdb */
 
@@ -804,21 +912,21 @@ static void wbinfo_get_auth_user(void)
        char *user, *domain, *password;
 
        /* Lift data from secrets file */
+       
+       secrets_fetch_ipc_userpass(&user, &domain, &password);
 
-       secrets_init();
-
-       user = secrets_fetch(SECRETS_AUTH_USER, NULL);
-       domain = secrets_fetch(SECRETS_AUTH_DOMAIN, NULL);
-       password = secrets_fetch(SECRETS_AUTH_PASSWORD, NULL);
+       if ((!user || !*user) && (!domain || !*domain ) && (!password || !*password)){
 
-       if (!user && !domain && !password) {
+               SAFE_FREE(user);
+               SAFE_FREE(domain);
+               SAFE_FREE(password);
                d_printf("No authorised user configured\n");
                return;
        }
 
        /* Pretty print authorised user info */
 
-       d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? "\\" : "",
+       d_printf("%s%s%s%s%s\n", domain ? domain : "", domain ? lp_winbind_separator(): "",
                 user, password ? "%" : "", password ? password : "");
 
        SAFE_FREE(user);
@@ -845,7 +953,9 @@ static BOOL wbinfo_ping(void)
 enum {
        OPT_SET_AUTH_USER = 1000,
        OPT_GET_AUTH_USER,
-       OPT_SEQUENCE
+       OPT_DOMAIN_NAME,
+       OPT_SEQUENCE,
+       OPT_USERSIDS
 };
 
 int main(int argc, char **argv)
@@ -854,8 +964,8 @@ int main(int argc, char **argv)
 
        poptContext pc;
        static char *string_arg;
+       static char *opt_domain_name;
        static int int_arg;
-       BOOL got_command = False;
        int result = 1;
 
        struct poptOption long_options[] = {
@@ -864,8 +974,8 @@ int main(int argc, char **argv)
                /* longName, shortName, argInfo, argPtr, value, descrip, 
                   argDesc */
 
-               { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users"},
-               { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups" },
+               { "domain-users", 'u', POPT_ARG_NONE, 0, 'u', "Lists all domain users", "domain"},
+               { "domain-groups", 'g', POPT_ARG_NONE, 0, 'g', "Lists all domain groups", "domain" },
                { "WINS-by-name", 'N', POPT_ARG_STRING, &string_arg, 'N', "Converts NetBIOS name to IP", "NETBIOS-NAME" },
                { "WINS-by-ip", 'I', POPT_ARG_STRING, &string_arg, 'I', "Converts IP address to NetBIOS name", "IP" },
                { "name-to-sid", 'n', POPT_ARG_STRING, &string_arg, 'n', "Converts name to sid", "NAME" },
@@ -883,11 +993,14 @@ int main(int argc, char **argv)
                { "check-secret", 't', POPT_ARG_NONE, 0, 't', "Check shared secret" },
                { "trusted-domains", 'm', POPT_ARG_NONE, 0, 'm', "List trusted domains" },
                { "sequence", 0, POPT_ARG_NONE, 0, OPT_SEQUENCE, "Show sequence numbers of all domains" },
+               { "domain-info", 'D', POPT_ARG_STRING, &string_arg, 'D', "Show most of the info we have about the domain" },
                { "user-groups", 'r', POPT_ARG_STRING, &string_arg, 'r', "Get user groups", "USER" },
+               { "user-sids", 0, POPT_ARG_STRING, &string_arg, OPT_USERSIDS, "Get user group sids for user SID", "SID" },
                { "authenticate", 'a', POPT_ARG_STRING, &string_arg, 'a', "authenticate user", "user%password" },
                { "set-auth-user", 0, POPT_ARG_STRING, &string_arg, OPT_SET_AUTH_USER, "Store user and password used by winbindd (root only)", "user%password" },
                { "get-auth-user", 0, POPT_ARG_NONE, NULL, OPT_GET_AUTH_USER, "Retrieve user and password used by winbindd (root only)", NULL },
                { "ping", 'p', POPT_ARG_NONE, 0, 'p', "Ping winbindd to see if it is alive" },
+               { "domain", 0, POPT_ARG_STRING, &opt_domain_name, OPT_DOMAIN_NAME, "Define to the domain to restrict operation", "domain" },
                POPT_COMMON_VERSION
                POPT_TABLEEND
        };
@@ -917,11 +1030,7 @@ int main(int argc, char **argv)
        }
 
        while((opt = poptGetNextOpt(pc)) != -1) {
-               if (got_command) {
-                       d_fprintf(stderr, "No more than one command may be specified at once.\n");
-                       exit(1);
-               }
-               got_command = True;
+               /* get the generic configuration parameters like --domain */
        }
 
        poptFreeContext(pc);
@@ -932,13 +1041,13 @@ int main(int argc, char **argv)
        while((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case 'u':
-                       if (!print_domain_users()) {
+                       if (!print_domain_users(opt_domain_name)) {
                                d_printf("Error looking up domain users\n");
                                goto done;
                        }
                        break;
                case 'g':
-                       if (!print_domain_groups()) {
+                       if (!print_domain_groups(opt_domain_name)) {
                                d_printf("Error looking up domain groups\n");
                                goto done;
                        }
@@ -1007,11 +1116,17 @@ int main(int argc, char **argv)
                        }
                        break;
                case OPT_SEQUENCE:
-                       if (!wbinfo_show_sequence()) {
+                       if (!wbinfo_show_sequence(opt_domain_name)) {
                                d_printf("Could not show sequence numbers\n");
                                goto done;
                        }
                        break;
+               case 'D':
+                       if (!wbinfo_domain_info(string_arg)) {
+                               d_printf("Could not get domain info\n");
+                               goto done;
+                       }
+                       break;
                case 'r':
                        if (!wbinfo_get_usergroups(string_arg)) {
                                d_printf("Could not get groups for user %s\n", 
@@ -1019,6 +1134,13 @@ int main(int argc, char **argv)
                                goto done;
                        }
                        break;
+               case OPT_USERSIDS:
+                       if (!wbinfo_get_usersids(string_arg)) {
+                               d_printf("Could not get group SIDs for user SID %s\n", 
+                                      string_arg);
+                               goto done;
+                       }
+                       break;
                case 'a': {
                                BOOL got_error = False;
 
@@ -1058,7 +1180,7 @@ int main(int argc, char **argv)
                        break;
                case 'O':
                        if ( !wbinfo_remove_user_from_group(string_arg) ) {
-                               d_printf("Could not remove user kfrom group\n");
+                               d_printf("Could not remove user from group\n");
                                goto done;
                        }
                        break;
@@ -1074,7 +1196,7 @@ int main(int argc, char **argv)
                                goto done;
                        }
                        break;
-               case 'P':
+               case 'p':
                        if (!wbinfo_ping()) {
                                d_printf("could not ping winbindd!\n");
                                goto done;
@@ -1086,6 +1208,9 @@ int main(int argc, char **argv)
                case OPT_GET_AUTH_USER:
                        wbinfo_get_auth_user();
                        break;
+               /* generic configuration options */
+               case OPT_DOMAIN_NAME:
+                       break;
                default:
                        d_fprintf(stderr, "Invalid option\n");
                        poptPrintHelp(pc, stderr, 0);