#include #include #include #include #include #include #include #include #include "netconf.h" #include #include "../paths.h" #include "hostinfo.h" #include "netconf.m" /* Try to find out if networking is somewhat configured Returne true if ok, false if not. status contains an error messsage if not ok. */ bool netconf_netok(char *status, char hostname[200]) { bool ret = true; char tmp[1000]; if (status == NULL) status = tmp; status[0] = '\0'; hostname[0] = '\0'; #if 1 HOSTINFO info; if (netconf_loadinfos(info)==-1 && info.a[0].confmode == INTER_MANUAL){ strcpy (status, MSG_U(E_NOPRIMNAME ,"No definition for the primary name\n" "in /etc/hosts.\n" "There is no entry with the name or alias\n" "\"loghost\".\n")); ret = false; } info.hostname.copy (hostname); #else ret = distrib_getvalnum ("NETWORKING",0); if (ret){ const char *pt = distrib_getval ("HOSTNAME"); if (pt != NULL){ strcpy (hostname,pt); }else{ strcpy (status,MSG_U(E_NOHOSTNAME,"Host name not defined")); } }else{ struct hostent *hst = gethostbyname(NAM_ETH0_LOGHOST); if (hst==NULL){ HOSTINFO info; if (netconf_loadinfos(info)==-1 || info.a[0].confmode == INTER_MANUAL){ strcpy (status,MSG_R(E_NOPRIMNAME)); ret = false; } info.a[0].name.copy (hostname); }else{ strcpy (hostname,hst->h_name); } } #endif return ret; } /* Try to find out if networking is somewhat configured Returne true if ok, false if not. status contains an error messsage if not ok. */ bool netconf_netok(char *status) { char hostname[200]; return netconf_netok (status,hostname); } static void thishost1_fqhn2host (const char *fqhn, char *hostpart) { const char *pt = strchr(fqhn,'.'); if (pt != NULL){ int len = (int)(pt-fqhn); memcpy (hostpart,fqhn,len); hostpart[len] = '\0'; }else{ strcpy (hostpart,fqhn); } } int thishost1_sethostname (const char *str) { int ret = -1; if (simul_isdemo()){ ret = 0; }else if (perm_rootaccess (MSG_U(P_SETHOSTNAME,"Setting the hostname"))){ #if 1 ret = sethostname (str,strlen(str)+1); #else char buf[100]; thishost1_fqhn2host (str, buf); ret = sethostname (buf,strlen(buf)+1); #endif } return ret; } /* Set the hostname in the kernel. Return -1 if anything goes wrong. */ int netconf_sethostname() { THISHOST thishost; int ret = -1; if (thishost.configok()){ const char *str = thishost.getname1(); char hostpart[100]; thishost1_fqhn2host (str,hostpart); char buf[100]; if (gethostname(buf,sizeof(buf)-1)!=-1 && strcmp(buf,hostpart)==0){ ret = 0; }else{ if (simul_ison()){ simul_addmsg (MSG_U(X_SETHOSTNAME ,"Setting hostname to %s\n"),str); ret = 0; }else{ ret = thishost1_sethostname (str); if (ret == -1){ xconf_error (MSG_U(E_CANTSETHOST ,"Can't set the host name to %s\n(%s)\n") ,str,strerror(errno)); }else{ net_prtlog (NETLOG_CMD,MSG_R(X_SETHOSTNAME),str); } } } } return ret; } /* Clone of the /bin/hostname command */ static int netconf_hostname (int argc, const char *argv[]) { /* #Specification: netconf / aliases / hostname netconf emulate the command /bin/hostname. Without argument it print the hostname. With one, it set the hostname. Option -f may be used to force a lookup of the second argument and use the first value returned by gethostbyname(). The argument may be a fully qualified name. Only the host part is used. */ int ret = -1; if (argc == 1){ char buf[100]; if (gethostname(buf,sizeof(buf)-1)==-1) strcpy (buf,"none"); printf ("%s\n",buf); ret = 0; }else if (argc == 2 && (strcmp(argv[1],"-d")==0 || strcmp(argv[1],"--domain")==0)){ THISHOST host; const char *n = host.getname1(); const char *pt = strchr(n,'.'); if (pt != NULL){ pt++; }else{ pt = ""; } printf ("%s\n",pt); ret = 0; }else if (argc == 2 && argv[1][0] != '-'){ ret = thishost1_sethostname (argv[1]); }else if (argc == 3 && strcmp(argv[1],"-f")==0){ const char *hostname = argv[2]; struct hostent *ent = gethostbyname(hostname); if (ent == NULL){ xconf_error (MSG_U(E_NOTKNOWN,"%s is not known\n"),hostname); }else{ ret = thishost1_sethostname (ent->h_name); } }else{ fprintf (stderr,MSG_U(E_HOSTUSAGE ,"usage: hostname [-f] [ host name ]\n" " hostname -d\n" " hostname --domain\n")); } return ret; } /* Clone of the /bin/dnsdomainname command */ int netconf_dnsdomainname (int argc, char *[]) { /* #Specification: netconf / aliases / hostname netconf emulate the command /bin/dnsdomainname. Without argument it print the dns domain name. With one, it set the hostname. Option -f may be used to force a lookup of the second argument and use the first value returned by gethostbyname(). The argument may be a fully qualified name. Only the host part is used. */ int ret = -1; if (argc == 1){ static const char *tb[]={"","-d",NULL}; ret = netconf_hostname (2,tb); }else{ fprintf (stderr,MSG_U(E_DNSDOMUSAGE ,"usage: dnsdomainname\n(No argument)\n")); } return ret; }