+/*******************************************************************
+ This function uses the username map file and tries to map a UNIX
+ user name to an DOS name. (Sort of the reverse of the
+ map_username() function.) Since more than one DOS name can map
+ to the UNIX name, to reverse the mapping you have to specify
+ which corresponding DOS name you want; that's where the name_idx
+ parameter comes in. Returns the string requested or NULL if it
+ fails or can't complete the request for any reason. This doesn't
+ handle group names (starting with '@') or names starting with
+ '+' or '&'. If they are encountered, they are skipped.
+********************************************************************/
+
+static char *unmap_unixname(char *unix_user_name, int name_idx)
+{
+ char *mapfile = lp_username_map();
+ char **lines;
+ static pstring tok;
+ int i;
+
+ if (!*unix_user_name) return NULL;
+ if (!*mapfile) return NULL;
+
+ lines = file_lines_load(mapfile, NULL);
+ if (!lines) {
+ DEBUG(0,("unmap_unixname: can't open username map %s\n", mapfile));
+ return NULL;
+ }
+
+ DEBUG(5,("unmap_unixname: scanning username map %s, index: %d\n", mapfile, name_idx));
+
+ for (i=0; lines[i]; i++) {
+ char *unixname = lines[i];
+ char *dosname = strchr(unixname,'=');
+
+ if (!dosname)
+ continue;
+
+ *dosname++ = 0;
+
+ while (isspace(*unixname))
+ unixname++;
+ if ('!' == *unixname) {
+ unixname++;
+ while (*unixname && isspace(*unixname))
+ unixname++;
+ }
+
+ if (!*unixname || strchr("#;",*unixname))
+ continue;
+
+ if (strncmp(unixname, unix_user_name, strlen(unix_user_name)))
+ continue;
+
+ /* We have matched the UNIX user name */
+
+ while(next_token(&dosname, tok, LIST_SEP, sizeof(tok))) {
+ if (!strchr("@&+", *tok)) {
+ name_idx--;
+ if (name_idx < 0 ) {
+ break;
+ }
+ }
+ }
+
+ if (name_idx >= 0) {
+ DEBUG(0,("unmap_unixname: index too high - not that many DOS names\n"));
+ file_lines_free(lines);
+ return NULL;
+ } else {
+ file_lines_free(lines);
+ return tok;
+ }
+ }
+
+ DEBUG(0,("unmap_unixname: Couldn't find the UNIX user name\n"));
+ file_lines_free(lines);
+ return NULL;
+}
+
+/*******************************************************************
+ This function sets up a list of users taken from the list of
+ users that UNIX knows about, as well as all the user names that
+ Samba maps to a valid UNIX user name. (This should work with
+ /etc/passwd or NIS.)
+********************************************************************/
+
+static BOOL get_passwd_entries(SAM_USER_INFO_21 *pw_buf,
+ int start_idx,
+ int *total_entries, int *num_entries,
+ int max_num_entries,
+ uint16 acb_mask)
+{
+ static struct passwd *pwd = NULL;
+ static uint32 pw_rid;
+ static BOOL orig_done = False;
+ static int current_idx = 0;
+ static int mapped_idx = 0;
+
+ DEBUG(5, ("get_passwd_entries: retrieving a list of UNIX users\n"));
+
+ (*num_entries) = 0;
+ (*total_entries) = 0;
+
+ if (pw_buf == NULL) return False;
+
+ if (current_idx == 0) {
+ setpwent();
+ }
+
+ /* These two cases are inefficient, but should be called very rarely */
+ /* they are the cases where the starting index isn't picking up */
+ /* where we left off last time. It is efficient when it starts over */
+ /* at zero though. */
+ if (start_idx > current_idx) {
+ /* We aren't far enough; advance to start_idx */
+ while (current_idx < start_idx) {
+ char *unmap_name;
+
+ if(!orig_done) {
+ if ((pwd = getpwent()) == NULL) break;
+ current_idx++;
+ orig_done = True;
+ }
+
+ while (((unmap_name = unmap_unixname(pwd->pw_name, mapped_idx)) != NULL) &&
+ (current_idx < start_idx)) {
+ current_idx++;
+ mapped_idx++;
+ }
+
+ if (unmap_name == NULL) {
+ orig_done = False;
+ mapped_idx = 0;
+ }
+ }
+ } else if (start_idx < current_idx) {
+ /* We are already too far; start over and advance to start_idx */
+ endpwent();
+ setpwent();
+ current_idx = 0;
+ mapped_idx = 0;
+ orig_done = False;
+ while (current_idx < start_idx) {
+ char *unmap_name;
+
+ if(!orig_done) {
+ if ((pwd = getpwent()) == NULL) break;
+ current_idx++;
+ orig_done = True;
+ }
+
+ while (((unmap_name = unmap_unixname(pwd->pw_name, mapped_idx)) != NULL) &&
+ (current_idx < start_idx)) {
+ current_idx++;
+ mapped_idx++;
+ }
+
+ if (unmap_name == NULL) {
+ orig_done = False;
+ mapped_idx = 0;
+ }
+ }
+ }
+
+ /* now current_idx == start_idx */
+ while ((*num_entries) < max_num_entries) {
+ int user_name_len;
+ char *unmap_name;
+
+ /* This does the original UNIX user itself */
+ if(!orig_done) {
+ if ((pwd = getpwent()) == NULL) break;
+ user_name_len = strlen(pwd->pw_name);
+ pw_rid = pdb_uid_to_user_rid(pwd->pw_uid);
+ ZERO_STRUCTP(&pw_buf[(*num_entries)]);
+ init_unistr2(&(pw_buf[(*num_entries)].uni_user_name), pwd->pw_name, user_name_len);
+ init_uni_hdr(&(pw_buf[(*num_entries)].hdr_user_name), user_name_len);
+ pw_buf[(*num_entries)].user_rid = pw_rid;
+ memset((char *)pw_buf[(*num_entries)].nt_pwd, '\0', 16);
+
+ pw_buf[(*num_entries)].acb_info = ACB_NORMAL;
+
+ DEBUG(5, ("get_passwd_entries: entry idx %d user %s, rid 0x%x\n", (*num_entries), pwd->pw_name, pw_rid));
+
+ (*num_entries)++;
+ (*total_entries)++;
+ current_idx++;
+ orig_done = True;
+ }
+
+ /* This does all the user names that map to the UNIX user */
+ while (((unmap_name = unmap_unixname(pwd->pw_name, mapped_idx)) != NULL) &&
+ (*num_entries < max_num_entries)) {
+ user_name_len = strlen(unmap_name);
+ ZERO_STRUCTP(&pw_buf[(*num_entries)]);
+ init_unistr2(&(pw_buf[(*num_entries)].uni_user_name), unmap_name, user_name_len);
+ init_uni_hdr(&(pw_buf[(*num_entries)].hdr_user_name), user_name_len);
+ pw_buf[(*num_entries)].user_rid = pw_rid;
+ memset((char *)pw_buf[(*num_entries)].nt_pwd, '\0', 16);
+
+ pw_buf[(*num_entries)].acb_info = ACB_NORMAL;
+
+ DEBUG(5, ("get_passwd_entries: entry idx %d user %s, rid 0x%x\n", (*num_entries), pwd->pw_name, pw_rid));
+
+ (*num_entries)++;
+ (*total_entries)++;
+ current_idx++;
+ mapped_idx++;
+ }
+
+ if (unmap_name == NULL) {
+ /* done with 'aliases', go on to next UNIX user */
+ orig_done = False;
+ mapped_idx = 0;
+ }
+ }
+
+ if (pwd == NULL) {
+ /* totally done, reset everything */
+ endpwent();
+ current_idx = 0;
+ mapped_idx = 0;
+ }
+
+ return (*num_entries) > 0;
+}
+