static void complete_sync(struct sync_record *s) { XFILE *f; char *server; char *type_str; unsigned type; char *comment; char line[1024]; const char *ptr; int count=0; f = x_fopen(s->fname,O_RDONLY, 0); if (!f) return; while (!x_feof(f)) { TALLOC_CTX *frame = NULL; if (!fgets_slash(line,sizeof(line),f)) continue; ptr = line; frame = talloc_stackframe(); if (!next_token_talloc(frame,&ptr,&server,NULL) || !next_token_talloc(frame,&ptr,&type_str,NULL) || !next_token_talloc(frame,&ptr,&comment,NULL)) { TALLOC_FREE(frame); continue; } sscanf(type_str, "%X", &type); complete_one(s, server, type, comment); count++; TALLOC_FREE(frame); } x_fclose(f); unlink(s->fname); DEBUG(2,("sync with %s(%s) for workgroup %s completed (%d records)\n", s->server, inet_ntoa(s->ip), s->workgroup, count)); }
bool getlmhostsent(TALLOC_CTX *ctx, XFILE *fp, char **pp_name, int *name_type, struct sockaddr_storage *pss) { char line[1024]; *pp_name = NULL; while(!x_feof(fp) && !x_ferror(fp)) { char *ip = NULL; char *flags = NULL; char *extra = NULL; char *name = NULL; const char *ptr; char *ptr1 = NULL; int count = 0; *name_type = -1; if (!fgets_slash(line,sizeof(line),fp)) { continue; } if (*line == '#') { continue; } ptr = line; if (next_token_talloc(ctx, &ptr, &ip, NULL)) ++count; if (next_token_talloc(ctx, &ptr, &name, NULL)) ++count; if (next_token_talloc(ctx, &ptr, &flags, NULL)) ++count; if (next_token_talloc(ctx, &ptr, &extra, NULL)) ++count; if (count <= 0) continue; if (count > 0 && count < 2) { DEBUG(0,("getlmhostsent: Ill formed hosts line [%s]\n", line)); continue; } if (count >= 4) { DEBUG(0,("getlmhostsent: too many columns " "in lmhosts file (obsolete syntax)\n")); continue; } if (!flags) { flags = talloc_strdup(ctx, ""); if (!flags) { continue; } } DEBUG(4, ("getlmhostsent: lmhost entry: %s %s %s\n", ip, name, flags)); if (strchr_m(flags,'G') || strchr_m(flags,'S')) { DEBUG(0,("getlmhostsent: group flag " "in lmhosts ignored (obsolete)\n")); continue; } if (!interpret_string_addr(pss, ip, AI_NUMERICHOST)) { DEBUG(0,("getlmhostsent: invalid address " "%s.\n", ip)); } /* Extra feature. If the name ends in '#XX', * where XX is a hex number, then only add that name type. */ if((ptr1 = strchr_m(name, '#')) != NULL) { char *endptr; ptr1++; *name_type = (int)strtol(ptr1, &endptr, 16); if(!*ptr1 || (endptr == ptr1)) { DEBUG(0,("getlmhostsent: invalid name " "%s containing '#'.\n", name)); continue; } *(--ptr1) = '\0'; /* Truncate at the '#' */ } *pp_name = talloc_strdup(ctx, name); if (!*pp_name) { return false; } return true; } return false; }
/* handle standard printcap - moved from pcap_printer_fn() */ bool std_pcap_cache_reload(const char *pcap_name, struct pcap_cache **_pcache) { XFILE *pcap_file; char *pcap_line; struct pcap_cache *pcache = NULL; if ((pcap_file = x_fopen(pcap_name, O_RDONLY, 0)) == NULL) { DEBUG(0, ("Unable to open printcap file %s for read!\n", pcap_name)); return false; } for (; (pcap_line = fgets_slash(NULL, 1024, pcap_file)) != NULL; free(pcap_line)) { char name[MAXPRINTERLEN+1]; char comment[62]; char *p, *q; if (*pcap_line == '#' || *pcap_line == 0) continue; /* now we have a real printer line - cut at the first : */ if ((p = strchr_m(pcap_line, ':')) != NULL) *p = 0; /* * now find the most likely printer name and comment * this is pure guesswork, but it's better than nothing */ for (*name = *comment = 0, p = pcap_line; p != NULL; p = q) { bool has_punctuation; if ((q = strchr_m(p, '|')) != NULL) *q++ = 0; has_punctuation = (strchr_m(p, ' ') || strchr_m(p, '\t') || strchr_m(p, '"') || strchr_m(p, '\'') || strchr_m(p, ';') || strchr_m(p, ',') || strchr_m(p, '(') || strchr_m(p, ')')); if (strlen(p) > strlen(comment) && has_punctuation) { strlcpy(comment, p, sizeof(comment)); continue; } if (strlen(p) <= MAXPRINTERLEN && *name == '\0' && !has_punctuation) { strlcpy(name, p, sizeof(name)); continue; } if (!strchr_m(comment, ' ') && strlen(p) > strlen(comment)) { strlcpy(comment, p, sizeof(comment)); continue; } } if ((*name != '\0') && !pcap_cache_add_specific(&pcache, name, comment, NULL)) { x_fclose(pcap_file); pcap_cache_destroy_specific(&pcache); return false; } } x_fclose(pcap_file); *_pcache = pcache; return true; }
BOOL map_username(char *user) { static BOOL initialised=False; static fstring last_from,last_to; FILE *f; char *mapfile = lp_username_map(); char *s; pstring buf; BOOL mapped_user = False; if (!*user) return False; if (!*mapfile) return False; if (!initialised) { *last_from = *last_to = 0; initialised = True; } if (strequal(user,last_to)) return False; if (strequal(user,last_from)) { DEBUG(3,("Mapped user %s to %s\n",user,last_to)); fstrcpy(user,last_to); return True; } f = sys_fopen(mapfile,"r"); if (!f) { DEBUG(0,("can't open username map %s\n",mapfile)); return False; } DEBUG(4,("Scanning username map %s\n",mapfile)); while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) { char *unixname = s; char *dosname = strchr(unixname,'='); BOOL return_if_mapped = False; if (!dosname) continue; *dosname++ = 0; while (isspace(*unixname)) unixname++; if ('!' == *unixname) { return_if_mapped = True; unixname++; while (*unixname && isspace(*unixname)) unixname++; } if (!*unixname || strchr("#;",*unixname)) continue; { int l = strlen(unixname); while (l && isspace(unixname[l-1])) { unixname[l-1] = 0; l--; } } if (strchr(dosname,'*') || user_in_list(user,dosname)) { DEBUG(3,("Mapped user %s to %s\n",user,unixname)); mapped_user = True; fstrcpy(last_from,user); sscanf(unixname,"%s",user); fstrcpy(last_to,user); if(return_if_mapped) { fclose(f); return True; } } } fclose(f); /* * Setup the last_from and last_to as an optimization so * that we don't scan the file again for the same user. */ fstrcpy(last_from,user); fstrcpy(last_to,user); return mapped_user; }
int main(int argc, char *argv[]) { char *p, *u; const char *libd = dyn_LIBDIR; pstring line, wd; int opt; extern char *optarg; extern int optind; dbf = x_stdout; smbw_setup_shared(); while ((opt = getopt(argc, argv, "W:U:R:d:P:l:hL:")) != EOF) { switch (opt) { case 'L': libd = optarg; break; case 'W': smbw_setshared("WORKGROUP", optarg); break; case 'l': smbw_setshared("LOGFILE", optarg); break; case 'P': smbw_setshared("PREFIX", optarg); break; case 'd': smbw_setshared("DEBUG", optarg); break; case 'U': p = strchr_m(optarg,'%'); if (p) { *p=0; smbw_setshared("PASSWORD",p+1); } smbw_setshared("USER", optarg); break; case 'R': smbw_setshared("RESOLVE_ORDER",optarg); break; case 'h': default: smbsh_usage(); } } if (!smbw_getshared("USER")) { printf("Username: "******"USER", u); } if (!smbw_getshared("PASSWORD")) { p = getpass("Password: "******"PASSWORD", p); } setenv("PS1", "smbsh$ ", 1); sys_getwd(wd); slprintf(line,sizeof(line)-1,"PWD_%d", (int)getpid()); smbw_setshared(line, wd); slprintf(line,sizeof(line)-1,"%s/smbwrapper.so", libd); setenv("LD_PRELOAD", line, 1); slprintf(line,sizeof(line)-1,"%s/smbwrapper.32.so", libd); if (file_exist(line, NULL)) { slprintf(line,sizeof(line)-1,"%s/smbwrapper.32.so:DEFAULT", libd); setenv("_RLD_LIST", line, 1); slprintf(line,sizeof(line)-1,"%s/smbwrapper.so:DEFAULT", libd); setenv("_RLDN32_LIST", line, 1); } else { slprintf(line,sizeof(line)-1,"%s/smbwrapper.so:DEFAULT", libd); setenv("_RLD_LIST", line, 1); } { char *shellpath = getenv("SHELL"); if(shellpath) execl(shellpath,"smbsh",NULL); else execl("/bin/sh","smbsh",NULL); } printf("launch failed!\n"); return 1; }
bool aix_cache_reload(void) { int iEtat; XFILE *pfile; char *line = NULL, *p; char *name = NULL; TALLOC_CTX *ctx = talloc_init("aix_cache_reload"); if (!ctx) { return false; } DEBUG(5, ("reloading aix printcap cache\n")); if ((pfile = x_fopen(lp_printcapname(), O_RDONLY, 0)) == NULL) { DEBUG(0,( "Unable to open qconfig file %s for read!\n", lp_printcapname())); TALLOC_FREE(ctx); return false; } iEtat = 0; /* scan qconfig file for searching <printername>: */ for (;(line = fgets_slash(NULL, 1024, pfile)); free(line)) { if (*line == '*' || *line == 0) continue; switch (iEtat) { case 0: /* locate an entry */ if (*line == '\t' || *line == ' ') continue; if ((p = strchr_m(line, ':'))) { char *saveptr; *p = '\0'; p = strtok_r(line, ":", &saveptr); if (strcmp(p, "bsh") != 0) { name = talloc_strdup(ctx, p); if (!name) { SAFE_FREE(line); x_fclose(pfile); TALLOC_FREE(ctx); return false; } iEtat = 1; continue; } } break; case 1: /* scanning device stanza */ if (*line == '*' || *line == 0) continue; if (*line != '\t' && *line != ' ') { /* name is found without stanza device */ /* probably a good printer ??? */ iEtat = 0; if (!pcap_cache_add(name, NULL)) { SAFE_FREE(line); x_fclose(pfile); TALLOC_FREE(ctx); return false; } continue; } if (strstr_m(line, "backend")) { /* it's a device, not a virtual printer */ iEtat = 0; } else if (strstr_m(line, "device")) { /* it's a good virtual printer */ iEtat = 0; if (!pcap_cache_add(name, NULL)) { SAFE_FREE(line); x_fclose(pfile); TALLOC_FREE(ctx); return false; } continue; } break; } } x_fclose(pfile); TALLOC_FREE(ctx); return true; }
bool map_username(TALLOC_CTX *ctx, const char *user_in, char **p_user_out) { XFILE *f; char *mapfile = lp_username_map(talloc_tos()); char *s; char buf[512]; bool mapped_user = False; char *cmd = lp_username_map_script(talloc_tos()); *p_user_out = NULL; if (!user_in) return false; /* Initially make a copy of the incoming name. */ *p_user_out = talloc_strdup(ctx, user_in); if (!*p_user_out) { return false; } if (strequal(user_in,get_last_to())) return false; if (strequal(user_in,get_last_from())) { DEBUG(3,("Mapped user %s to %s\n",user_in,get_last_to())); TALLOC_FREE(*p_user_out); *p_user_out = talloc_strdup(ctx, get_last_to()); return true; } if (fetch_map_from_gencache(ctx, user_in, p_user_out)) { return true; } /* first try the username map script */ if ( *cmd ) { char **qlines; char *command = NULL; int numlines, ret, fd; command = talloc_asprintf(ctx, "%s \"%s\"", cmd, user_in); if (!command) { return false; } DEBUG(10,("Running [%s]\n", command)); ret = smbrun(command, &fd); DEBUGADD(10,("returned [%d]\n", ret)); TALLOC_FREE(command); if ( ret != 0 ) { if (fd != -1) close(fd); return False; } numlines = 0; qlines = fd_lines_load(fd, &numlines, 0, ctx); DEBUGADD(10,("Lines returned = [%d]\n", numlines)); close(fd); /* should be either no lines or a single line with the mapped username */ if (numlines && qlines) { DEBUG(3,("Mapped user %s to %s\n", user_in, qlines[0] )); set_last_from_to(user_in, qlines[0]); store_map_in_gencache(ctx, user_in, qlines[0]); TALLOC_FREE(*p_user_out); *p_user_out = talloc_strdup(ctx, qlines[0]); if (!*p_user_out) { return false; } } TALLOC_FREE(qlines); return numlines != 0; } /* ok. let's try the mapfile */ if (!*mapfile) return False; f = x_fopen(mapfile,O_RDONLY, 0); if (!f) { DEBUG(0,("can't open username map %s. Error %s\n",mapfile, strerror(errno) )); return False; } DEBUG(4,("Scanning username map %s\n",mapfile)); while((s=fgets_slash(buf,sizeof(buf),f))!=NULL) { char *unixname = s; char *dosname = strchr_m(unixname,'='); char **dosuserlist; bool return_if_mapped = False; if (!dosname) continue; *dosname++ = 0; unixname = skip_space(unixname); if ('!' == *unixname) { return_if_mapped = True; unixname = skip_space(unixname+1); } if (!*unixname || strchr_m("#;",*unixname)) continue; { int l = strlen(unixname); while (l && isspace((int)unixname[l-1])) { unixname[l-1] = 0; l--; } } /* skip lines like 'user = '******'*') || user_in_list(ctx, user_in, (const char **)dosuserlist)) { DEBUG(3,("Mapped user %s to %s\n",user_in,unixname)); mapped_user = True; set_last_from_to(user_in, unixname); store_map_in_gencache(ctx, user_in, unixname); TALLOC_FREE(*p_user_out); *p_user_out = talloc_strdup(ctx, unixname); if (!*p_user_out) { TALLOC_FREE(dosuserlist); x_fclose(f); return false; } if ( return_if_mapped ) { TALLOC_FREE(dosuserlist); x_fclose(f); return True; } } TALLOC_FREE(dosuserlist); } x_fclose(f); /* * If we didn't successfully map a user in the loop above, * setup the last_from and last_to as an optimization so * that we don't scan the file again for the same user. */ if (!mapped_user) { DEBUG(8, ("The user '%s' has no mapping. " "Skip it next time.\n", user_in)); set_last_from_to(user_in, user_in); store_map_in_gencache(ctx, user_in, user_in); } return mapped_user; }
/******************************************************** Parse the next line in the lmhosts file. *********************************************************/ BOOL getlmhostsent (FILE * fp, pstring name, int *name_type, struct in_addr * ipaddr) { pstring line; while (!feof (fp) && !ferror (fp)) { pstring ip, flags, extra; char *ptr; int count = 0; *name_type = -1; if (!fgets_slash (line, sizeof (pstring), fp)) continue; if (*line == '#') continue; pstrcpy (ip, ""); pstrcpy (name, ""); pstrcpy (flags, ""); ptr = line; if (next_token (&ptr, ip, NULL, sizeof (ip))) ++count; if (next_token (&ptr, name, NULL, sizeof (pstring))) ++count; if (next_token (&ptr, flags, NULL, sizeof (flags))) ++count; if (next_token (&ptr, extra, NULL, sizeof (extra))) ++count; if (count <= 0) continue; if (count > 0 && count < 2) { DEBUG (0, ("getlmhostsent: Ill formed hosts line [%s]\n", line)); continue; } if (count >= 4) { DEBUG (0, ("getlmhostsent: too many columns in lmhosts file (obsolete syntax)\n")); continue; } DEBUG (4, ("getlmhostsent: lmhost entry: %s %s %s\n", ip, name, flags)); if (strchr (flags, 'G') || strchr (flags, 'S')) { DEBUG (0, ("getlmhostsent: group flag in lmhosts ignored (obsolete)\n")); continue; } *ipaddr = *interpret_addr2 (ip); /* Extra feature. If the name ends in '#XX', where XX is a hex number, then only add that name type. */ if ((ptr = strchr (name, '#')) != NULL) { char *endptr; ptr++; *name_type = (int) strtol (ptr, &endptr, 16); if (!*ptr || (endptr == ptr)) { DEBUG (0, ("getlmhostsent: invalid name %s containing '#'.\n", name)); continue; } *(--ptr) = '\0'; /* Truncate at the '#' */ } return True; } return False; }