/* Unix SMB/CIFS implementation. Winbind status program. Copyright (C) Tim Potter 2000-2003 Copyright (C) Andrew Bartlett 2002 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 the Free Software Foundation; either version 2 of the License, or (at your option) any later version. 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. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "includes.h" #include "winbindd.h" #include "debug.h" #undef DBGC_CLASS #define DBGC_CLASS DBGC_WINBIND extern int winbindd_fd; static char winbind_separator_int(BOOL strict) { struct winbindd_response response; static BOOL got_sep; static char sep; if (got_sep) return sep; ZERO_STRUCT(response); /* Send off request */ if (winbindd_request_response(WINBINDD_INFO, NULL, &response) != NSS_STATUS_SUCCESS) { d_fprintf(stderr, "could not obtain winbind separator!\n"); if (strict) { return 0; } /* HACK: (this module should not call lp_ funtions) */ return *lp_winbind_separator(); } sep = response.data.info.winbind_separator; got_sep = True; if (!sep) { d_fprintf(stderr, "winbind separator was NULL!\n"); if (strict) { return 0; } /* HACK: (this module should not call lp_ funtions) */ sep = *lp_winbind_separator(); } return sep; } static char winbind_separator(void) { return winbind_separator_int(False); } static const char *get_winbind_domain(void) { struct winbindd_response response; static fstring winbind_domain; ZERO_STRUCT(response); /* Send off request */ if (winbindd_request_response(WINBINDD_DOMAIN_NAME, NULL, &response) != NSS_STATUS_SUCCESS) { d_fprintf(stderr, "could not obtain winbind domain name!\n"); /* HACK: (this module should not call lp_ funtions) */ return lp_workgroup(); } fstrcpy(winbind_domain, response.data.domain_name); return winbind_domain; } /* Copy of parse_domain_user from winbindd_util.c. Parse a string of the form DOMAIN/user into a domain and a user */ static BOOL parse_wbinfo_domain_user(const char *domuser, fstring domain, fstring user) { char *p = strchr(domuser,winbind_separator()); if (!p) { fstrcpy(user, domuser); fstrcpy(domain, get_winbind_domain()); return True; } fstrcpy(user, p+1); fstrcpy(domain, domuser); domain[PTR_DIFF(p, domuser)] = 0; strupper_m(domain); return True; } /* pull pwent info for a given user */ static BOOL wbinfo_get_userinfo(char *user) { struct winbindd_request request; struct winbindd_response response; NSS_STATUS result; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send request */ fstrcpy(request.data.username, user); result = winbindd_request_response(WINBINDD_GETPWNAM, &request, &response); if (result != NSS_STATUS_SUCCESS) return False; d_printf( "%s:%s:%d:%d:%s:%s:%s\n", response.data.pw.pw_name, response.data.pw.pw_passwd, response.data.pw.pw_uid, response.data.pw.pw_gid, response.data.pw.pw_gecos, response.data.pw.pw_dir, response.data.pw.pw_shell ); return True; } /* pull grent for a given group */ static BOOL wbinfo_get_groupinfo(char *group) { struct winbindd_request request; struct winbindd_response response; NSS_STATUS result; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send request */ fstrcpy(request.data.groupname, group); result = winbindd_request_response(WINBINDD_GETGRNAM, &request, &response); if ( result != NSS_STATUS_SUCCESS) return False; d_printf( "%s:%s:%d\n", response.data.gr.gr_name, response.data.gr.gr_passwd, response.data.gr.gr_gid ); return True; } /* List groups a user is a member of */ static BOOL wbinfo_get_usergroups(char *user) { struct winbindd_request request; struct winbindd_response response; NSS_STATUS result; int i; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send request */ fstrcpy(request.data.username, user); result = winbindd_request_response(WINBINDD_GETGROUPS, &request, &response); if (result != NSS_STATUS_SUCCESS) return False; for (i = 0; i < response.data.num_entries; i++) d_printf("%d\n", (int)((gid_t *)response.extra_data.data)[i]); SAFE_FREE(response.extra_data.data); 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(request); ZERO_STRUCT(response); /* Send request */ fstrcpy(request.data.sid, user_sid); result = winbindd_request_response(WINBINDD_GETUSERSIDS, &request, &response); if (result != NSS_STATUS_SUCCESS) return False; s = (const char *)response.extra_data.data; for (i = 0; i < response.data.num_entries; i++) { d_printf("%s\n", s); s += strlen(s) + 1; } SAFE_FREE(response.extra_data.data); return True; } static BOOL wbinfo_get_userdomgroups(const char *user_sid) { struct winbindd_request request; struct winbindd_response response; NSS_STATUS result; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send request */ fstrcpy(request.data.sid, user_sid); result = winbindd_request_response(WINBINDD_GETUSERDOMGROUPS, &request, &response); if (result != NSS_STATUS_SUCCESS) return False; if (response.data.num_entries != 0) printf("%s", (char *)response.extra_data.data); SAFE_FREE(response.extra_data.data); return True; } /* Convert NetBIOS name to IP */ static BOOL wbinfo_wins_byname(char *name) { struct winbindd_request request; struct winbindd_response response; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send request */ fstrcpy(request.data.winsreq, name); if (winbindd_request_response(WINBINDD_WINS_BYNAME, &request, &response) != NSS_STATUS_SUCCESS) { return False; } /* Display response */ d_printf("%s\n", response.data.winsresp); return True; } /* Convert IP to NetBIOS name */ static BOOL wbinfo_wins_byip(char *ip) { struct winbindd_request request; struct winbindd_response response; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send request */ fstrcpy(request.data.winsreq, ip); if (winbindd_request_response(WINBINDD_WINS_BYIP, &request, &response) != NSS_STATUS_SUCCESS) { return False; } /* Display response */ d_printf("%s\n", response.data.winsresp); return True; } /* List trusted domains */ static BOOL wbinfo_list_domains(BOOL list_all_domains) { struct winbindd_request request; struct winbindd_response response; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send request */ request.data.list_all_domains = list_all_domains; if (winbindd_request_response(WINBINDD_LIST_TRUSTDOM, &request, &response) != NSS_STATUS_SUCCESS) return False; /* Display response */ if (response.extra_data.data) { const char *extra_data = (char *)response.extra_data.data; fstring name; char *p; while(next_token(&extra_data, name, "\n", sizeof(fstring))) { p = strchr(name, '\\'); if (p == 0) { d_fprintf(stderr, "Got invalid response: %s\n", extra_data); return False; } *p = 0; d_printf("%s\n", name); } SAFE_FREE(response.extra_data.data); } return True; } /* List own domain */ static BOOL wbinfo_list_own_domain(void) { d_printf("%s\n", get_winbind_domain()); return True; } /* show sequence numbers */ 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_response(WINBINDD_SHOW_SEQUENCE, &request, &response) != NSS_STATUS_SUCCESS) return False; /* Display response */ if (response.extra_data.data) { char *extra_data = (char *)response.extra_data.data; d_printf("%s", extra_data); SAFE_FREE(response.extra_data.data); } 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); if ((strequal(domain_name, ".")) || (domain_name[0] == '\0')) fstrcpy(request.domain_name, get_winbind_domain()); else fstrcpy(request.domain_name, domain_name); /* Send request */ if (winbindd_request_response(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; } /* Get a foreign DC's name */ static BOOL wbinfo_getdcname(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_response(WINBINDD_GETDCNAME, &request, &response) != NSS_STATUS_SUCCESS) { d_fprintf(stderr, "Could not get dc name for %s\n", domain_name); return False; } /* Display response */ d_printf("%s\n", response.data.dc_name); return True; } /* Check trust account password */ static BOOL wbinfo_check_secret(void) { struct winbindd_response response; NSS_STATUS result; ZERO_STRUCT(response); result = winbindd_request_response(WINBINDD_CHECK_MACHACC, NULL, &response); d_printf("checking the trust secret via RPC calls %s\n", (result == NSS_STATUS_SUCCESS) ? "succeeded" : "failed"); if (result != NSS_STATUS_SUCCESS) d_fprintf(stderr, "error code was %s (0x%x)\n", response.data.auth.nt_status_string, response.data.auth.nt_status); return result == NSS_STATUS_SUCCESS; } /* Convert uid to sid */ static BOOL wbinfo_uid_to_sid(uid_t uid) { struct winbindd_request request; struct winbindd_response response; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send request */ request.data.uid = uid; if (winbindd_request_response(WINBINDD_UID_TO_SID, &request, &response) != NSS_STATUS_SUCCESS) return False; /* Display response */ d_printf("%s\n", response.data.sid.sid); return True; } /* Convert gid to sid */ static BOOL wbinfo_gid_to_sid(gid_t gid) { struct winbindd_request request; struct winbindd_response response; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send request */ request.data.gid = gid; if (winbindd_request_response(WINBINDD_GID_TO_SID, &request, &response) != NSS_STATUS_SUCCESS) return False; /* Display response */ d_printf("%s\n", response.data.sid.sid); return True; } /* Convert sid to uid */ static BOOL wbinfo_sid_to_uid(char *sid) { struct winbindd_request request; struct winbindd_response response; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send request */ fstrcpy(request.data.sid, sid); if (winbindd_request_response(WINBINDD_SID_TO_UID, &request, &response) != NSS_STATUS_SUCCESS) return False; /* Display response */ d_printf("%d\n", (int)response.data.uid); return True; } static BOOL wbinfo_sid_to_gid(char *sid) { struct winbindd_request request; struct winbindd_response response; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send request */ fstrcpy(request.data.sid, sid); if (winbindd_request_response(WINBINDD_SID_TO_GID, &request, &response) != NSS_STATUS_SUCCESS) return False; /* Display response */ d_printf("%d\n", (int)response.data.gid); return True; } static BOOL wbinfo_allocate_uid(void) { uid_t uid; if (!winbind_allocate_uid(&uid)) return False; d_printf("New uid: %d\n", uid); return True; } static BOOL wbinfo_allocate_gid(void) { gid_t gid; if (!winbind_allocate_gid(&gid)) return False; d_printf("New gid: %d\n", gid); return True; } /* Convert sid to string */ static BOOL wbinfo_lookupsid(char *sid) { struct winbindd_request request; struct winbindd_response response; ZERO_STRUCT(request); ZERO_STRUCT(response); /* Send off request */ fstrcpy(request.data.sid, sid); if (winbindd_request_response(WINBINDD_LOOKUPSID, &request, &response) != NSS_STATUS_SUCCESS) return False; /* Display response */ d_printf("%s%c%s %d\n", response.data.name.dom_name, winbind_separator(), response.data.name.name, response.data.name.type); return True; } /* Lookup a list of RIDs */ static BOOL wbinfo_lookuprids(char *domain, char *arg) { size_t i; DOM_SID sid; int num_rids; uint32 *rids; const char *p; char ridstr[32]; const char **names; enum lsa_SidType *types; const char *domain_name; TALLOC_CTX *mem_ctx; struct winbindd_request request; struct winbindd_response response; ZERO_STRUCT(request); ZERO_STRUCT(response); if ((domain == NULL) || (strequal(domain, ".")) || (domain[0] == '\0')) fstrcpy(request.domain_name, get_winbind_domain()); else fstrcpy(request.domain_name, domain); /* Send request */ if (winbindd_request_response(WINBINDD_DOMAIN_INFO, &request, &response) != NSS_STATUS_SUCCESS) { d_printf("Could not get domain sid for %s\n", request.domain_name); return False; } if (!string_to_sid(&sid, response.data.domain_info.sid)) { d_printf("Could not convert %s to sid\n", response.data.domain_info.sid); return False; } mem_ctx = talloc_new(NULL); if (mem_ctx == NULL) { d_printf("talloc_new failed\n"); return False; } num_rids = 0; rids = NULL; p = arg; while (next_token(&p, ridstr, " ,\n", sizeof(ridstr))) { uint32 rid = strtoul(ridstr, NULL, 10); ADD_TO_ARRAY(mem_ctx, uint32, rid, &rids, &num_rids); } if (rids == NULL) { TALLOC_FREE(mem_ctx); return False; } if (!winbind_lookup_rids(mem_ctx, &sid, num_rids, rids, &domain_name, &names, &types)) { d_printf("winbind_lookup_rids failed\n"); TALLOC_FREE(mem_ctx); return False; } d_printf("Domain: %s\n", domain_name); for (i=0; i