📄 util.c
字号:
int set_filelen(int fd, SMB_OFF_T len){/* According to W. R. Stevens advanced UNIX prog. Pure 4.3 BSD cannot extend a file with ftruncate. Provide alternate implementation for this */#ifdef HAVE_FTRUNCATE_EXTEND return sys_ftruncate(fd, len);#else SMB_STRUCT_STAT st; char c = 0; SMB_OFF_T currpos = sys_lseek(fd, (SMB_OFF_T)0, SEEK_CUR); if(currpos == -1) return -1; /* Do an fstat to see if the file is longer than the requested size (call ftruncate), or shorter, in which case seek to len - 1 and write 1 byte of zero */ if(sys_fstat(fd, &st)<0) return -1;#ifdef S_ISFIFO if (S_ISFIFO(st.st_mode)) return 0;#endif if(st.st_size == len) return 0; if(st.st_size > len) return sys_ftruncate(fd, len); if(sys_lseek(fd, len-1, SEEK_SET) != len -1) return -1; if(write(fd, &c, 1)!=1) return -1; /* Seek to where we were */ if(sys_lseek(fd, currpos, SEEK_SET) != currpos) return -1; return 0;#endif}#ifdef HPUX/****************************************************************************this is a version of setbuffer() for those machines that only have setvbuf****************************************************************************/ void setbuffer(FILE *f,char *buf,int bufsize){ setvbuf(f,buf,_IOFBF,bufsize);}#endif#endif /* 0 *//****************************************************************************parse out a filename from a path name. Assumes dos style filenames.****************************************************************************/static char *filename_dos(char *path,char *buf){ char *p = strrchr(path,'\\'); if (!p) pstrcpy(buf,path); else pstrcpy(buf,p+1); return(buf);}/****************************************************************************expand a pointer to be a particular size****************************************************************************/void *Realloc(void *p,size_t size){ void *ret=NULL; if (size == 0) { if (p) free(p); DEBUG(5,("Realloc asked for 0 bytes\n")); return NULL; } if (!p) ret = (void *)malloc(size); else ret = (void *)realloc(p,size);#ifdef MEM_MAN { extern FILE *dbf; smb_mem_write_info(ret, dbf); }#endif if (!ret) DEBUG(0,("Memory allocation error: failed to expand to %d bytes\n",(int)size)); return(ret);}/****************************************************************************get my own name and IP****************************************************************************/BOOL get_myname(char *my_name,struct in_addr *ip){ struct hostent *hp; pstring hostname; *hostname = 0; /* get my host name */ if (gethostname(hostname, sizeof(hostname)) == -1) { DEBUG(0,("gethostname failed\n")); return False; } /* Ensure null termination. */ hostname[sizeof(hostname)-1] = '\0'; /* get host info */ if ((hp = Get_Hostbyname(hostname)) == 0) { DEBUG(0,( "Get_Hostbyname: Unknown host %s\n",hostname)); return False; } if (my_name) { /* split off any parts after an initial . */ char *p = strchr(hostname,'.'); if (p) *p = 0; fstrcpy(my_name,hostname); } if (ip) putip((char *)ip,(char *)hp->h_addr); return(True);}/****************************************************************************true if two IP addresses are equal****************************************************************************/BOOL ip_equal(struct in_addr ip1,struct in_addr ip2){ uint32 a1,a2; a1 = ntohl(ip1.s_addr); a2 = ntohl(ip2.s_addr); return(a1 == a2);}/****************************************************************************interpret a protocol description string, with a default****************************************************************************/int interpret_protocol(char *str,int def){ if (strequal(str,"NT1")) return(PROTOCOL_NT1); if (strequal(str,"LANMAN2")) return(PROTOCOL_LANMAN2); if (strequal(str,"LANMAN1")) return(PROTOCOL_LANMAN1); if (strequal(str,"CORE")) return(PROTOCOL_CORE); if (strequal(str,"COREPLUS")) return(PROTOCOL_COREPLUS); if (strequal(str,"CORE+")) return(PROTOCOL_COREPLUS); DEBUG(0,("Unrecognised protocol level %s\n",str)); return(def);}/****************************************************************************interpret an internet address or name into an IP address in 4 byte form****************************************************************************/uint32 interpret_addr(char *str){ struct hostent *hp; uint32 res; int i; BOOL pure_address = True; if (strcmp(str,"0.0.0.0") == 0) return(0); if (strcmp(str,"255.255.255.255") == 0) return(0xFFFFFFFF); for (i=0; pure_address && str[i]; i++) if (!(isdigit((int)str[i]) || str[i] == '.')) pure_address = False; /* if it's in the form of an IP address then get the lib to interpret it */ if (pure_address) { res = inet_addr(str); } else { /* otherwise assume it's a network name of some sort and use Get_Hostbyname */ if ((hp = Get_Hostbyname(str)) == 0) { DEBUG(3,("Get_Hostbyname: Unknown host. %s\n",str)); return 0; } if(hp->h_addr == NULL) { DEBUG(3,("Get_Hostbyname: host address is invalid for host %s\n",str)); return 0; } putip((char *)&res,(char *)hp->h_addr); } if (res == (uint32)-1) return(0); return(res);}/******************************************************************* a convenient addition to interpret_addr() ******************************************************************/struct in_addr *interpret_addr2(char *str){ static struct in_addr ret; uint32 a = interpret_addr(str); ret.s_addr = a; return(&ret);}/******************************************************************* check if an IP is the 0.0.0.0 ******************************************************************/BOOL zero_ip(struct in_addr ip){ uint32 a; putip((char *)&a,(char *)&ip); return(a == 0);}/******************************************************************* matchname - determine if host name matches IP address ******************************************************************/BOOL matchname(char *remotehost,struct in_addr addr){ struct hostent *hp; int i; if ((hp = Get_Hostbyname(remotehost)) == 0) { DEBUG(0,("Get_Hostbyname(%s): lookup failure.\n", remotehost)); return False; } /* * Make sure that gethostbyname() returns the "correct" host name. * Unfortunately, gethostbyname("localhost") sometimes yields * "localhost.domain". Since the latter host name comes from the * local DNS, we just have to trust it (all bets are off if the local * DNS is perverted). We always check the address list, though. */ if (strcasecmp(remotehost, hp->h_name) && strcasecmp(remotehost, "localhost")) { DEBUG(0,("host name/name mismatch: %s != %s\n", remotehost, hp->h_name)); return False; } /* Look up the host address in the address list we just got. */ for (i = 0; hp->h_addr_list[i]; i++) { if (memcmp(hp->h_addr_list[i], (caddr_t) & addr, sizeof(addr)) == 0) return True; } /* * The host name does not map to the original host address. Perhaps * someone has compromised a name server. More likely someone botched * it, but that could be dangerous, too. */ DEBUG(0,("host name/address mismatch: %s != %s\n", inet_ntoa(addr), hp->h_name)); return False;}#if (defined(HAVE_NETGROUP) && defined(WITH_AUTOMOUNT))/****************************************************************** Remove any mount options such as -rsize=2048,wsize=2048 etc. Based on a fix from <Thomas.Hepper@icem.de>.*******************************************************************/static void strip_mount_options( pstring *str){ if (**str == '-') { char *p = *str; while(*p && !isspace(*p)) p++; while(*p && isspace(*p)) p++; if(*p) { pstring tmp_str; pstrcpy(tmp_str, p); pstrcpy(*str, tmp_str); } }}/******************************************************************* Patch from jkf@soton.ac.uk Split Luke's automount_server into YP lookup and string splitter so can easily implement automount_path(). As we may end up doing both, cache the last YP result. *******************************************************************/#ifdef WITH_NISPLUS_HOMEstatic char *automount_lookup(char *user_name){ static fstring last_key = ""; static pstring last_value = ""; char *nis_map = (char *)lp_nis_home_map_name(); char nis_domain[NIS_MAXNAMELEN + 1]; char buffer[NIS_MAXATTRVAL + 1]; nis_result *result; nis_object *object; entry_obj *entry; strncpy(nis_domain, (char *)nis_local_directory(), NIS_MAXNAMELEN); nis_domain[NIS_MAXNAMELEN] = '\0'; DEBUG(5, ("NIS+ Domain: %s\n", nis_domain)); if (strcmp(user_name, last_key)) { slprintf(buffer, sizeof(buffer)-1, "[%s=%s]%s.%s", "key", user_name, nis_map, nis_domain); DEBUG(5, ("NIS+ querystring: %s\n", buffer)); if (result = nis_list(buffer, RETURN_RESULT, NULL, NULL)) { if (result->status != NIS_SUCCESS) { DEBUG(3, ("NIS+ query failed: %s\n", nis_sperrno(result->status))); fstrcpy(last_key, ""); pstrcpy(last_value, ""); } else { object = result->objects.objects_val; if (object->zo_data.zo_type == ENTRY_OBJ) { entry = &object->zo_data.objdata_u.en_data; DEBUG(5, ("NIS+ entry type: %s\n", entry->en_type)); DEBUG(3, ("NIS+ result: %s\n", entry->en_cols.en_cols_val[1].ec_value.ec_value_val)); pstrcpy(last_value, entry->en_cols.en_cols_val[1].ec_value.ec_value_val); string_sub(last_value, "&", user_name); fstrcpy(last_key, user_name); } } } nis_freeresult(result); } strip_mount_options(&last_value); DEBUG(4, ("NIS+ Lookup: %s resulted in %s\n", user_name, last_value)); return last_value;}#else /* WITH_NISPLUS_HOME */static char *automount_lookup(char *user_name){ static fstring last_key = ""; static pstring last_value = ""; int nis_error; /* returned by yp all functions */ char *nis_result; /* yp_match inits this */ int nis_result_len; /* and set this */ char *nis_domain; /* yp_get_default_domain inits this */ char *nis_map = (char *)lp_nis_home_map_name(); if ((nis_error = yp_get_default_domain(&nis_domain)) != 0) { DEBUG(3, ("YP Error: %s\n", yperr_string(nis_error))); return last_value; } DEBUG(5, ("NIS Domain: %s\n", nis_domain)); if (!strcmp(user_name, last_key)) { nis_result = last_value; nis_result_len = strlen(last_value); nis_error = 0; } else { if ((nis_error = yp_match(nis_domain, nis_map, user_name, strlen(user_name), &nis_result, &nis_result_len)) != 0) { DEBUG(3, ("YP Error: \"%s\" while looking up \"%s\" in map \"%s\"\n", yperr_string(nis_error), user_name, nis_map)); } if (!nis_error && nis_result_len >= sizeof(pstring)) { nis_result_len = sizeof(pstring)-1; } fstrcpy(last_key, user_name); strncpy(last_value, nis_result, nis_result_len); last_value[nis_result_len] = '\0'; } strip_mount_options(&last_value); DEBUG(4, ("YP Lookup: %s resulted in %s\n", user_name, last_value)); return last_value;}#endif /* WITH_NISPLUS_HOME */#endif/******************************************************************* Patch from jkf@soton.ac.uk This is Luke's original function with the NIS lookup code moved out to a separate function.*******************************************************************/static char *automount_server(char *user_name){ static pstring server_name; /* use the local machine name as the default */ /* this will be the default if WITH_AUTOMOUNT is not used or fails */ pstrcpy(server_name, local_machine);#if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT)) if (lp_nis_home_map()) { int home_server_len; char *automount_value = automount_lookup(user_name); home_server_len = strcspn(automount_value,":"); DEBUG(5, ("NIS lookup succeeded. Home server length: %d\n",home_server_len)); if (home_server_len > sizeof(pstring)) { home_server_len = sizeof(pstring); } strncpy(server_name, automount_value, home_server_len); server_name[home_server_len] = '\0'; }#endif DEBUG(4,("Home server: %s\n", server_name)); return server_name;}/******************************************************************* Patch from jkf@soton.ac.uk Added this to implement %p (NIS auto-map version of %H)*******************************************************************/static char *automount_path(char *user_name){ static pstring server_path; /* use the passwd entry as the default */ /* this will be the default if WITH_AUTOMOUNT is not used or fails */ /* pstrcpy() copes with get_home_dir() returning NULL */ pstrcpy(server_path, get_home_dir(user_name));#if (defined(HAVE_NETGROUP) && defined (WITH_AUTOMOUNT)) if (lp_nis_home_map()) { char *home_path_start; char *automount_value = automount_lookup(user_name); home_path_start = strchr(automount_value,':'); if (home_path_start != NULL) { DEBUG(5, ("NIS lookup succeeded. Home path is: %s\n", home_path_start?(home_path_start+1):"")); pstrcpy(server_path, home_path_start+1); } }#endif DEBUG(4,("Home server path: %s\n", server_path)); return server_path;}/*******************************************************************sub strings with useful parametersRewritten by Stefaan A Eeckels <Stefaan.Eeckels@ecc.lu> andPaul Rippin <pr3245@nopc.eurostat.cec.be>********************************************************************/void standard_sub_basic(char *str){ char *s, *p; char pidstr[10]; struct passwd *pass; char *username = sam_logon_in_ssb ? samlogon_user : sesssetup_user; for (s = str ; s && *s && (p = strchr(s,'%')); s = p ) { switch (*(p+1)) { case 'G' : { if ((pass = Get_Pwnam(username,False))!=NULL) { string_sub(p,"%G",gidtoname(pass->pw_gid));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -