[BACK]Return to net.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / distrib / utils / sysinst

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/distrib/utils/sysinst/Attic/net.c between version 1.38 and 1.38.2.4

version 1.38, 1999/03/19 14:49:07 version 1.38.2.4, 1999/07/06 23:56:51
Line 16 
Line 16 
  *    documentation and/or other materials provided with the distribution.   *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software   * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:   *    must display the following acknowledgement:
  *      This product includes software develooped for the NetBSD Project by   *      This product includes software developed for the NetBSD Project by
  *      Piermont Information Systems Inc.   *      Piermont Information Systems Inc.
  * 4. The name of Piermont Information Systems Inc. may not be used to endorse   * 4. The name of Piermont Information Systems Inc. may not be used to endorse
  *    or promote products derived from this software without specific prior   *    or promote products derived from this software without specific prior
Line 56  int network_up = 0;
Line 56  int network_up = 0;
 /* URL encode unsafe characters.  */  /* URL encode unsafe characters.  */
   
 static char *url_encode __P((char *dst, const char *src, size_t len,  static char *url_encode __P((char *dst, const char *src, size_t len,
                                 const char *safe_chars));                                  const char *safe_chars,
                                   int encode_leading_slash));
   
 /* Get the list of network interfaces. */  /* Get the list of network interfaces. */
   
 static void get_ifconfig_info __P((void));  static void get_ifconfig_info __P((void));
 static void get_ifinterface_info __P((void));  static void get_ifinterface_info __P((void));
   
 /* external */  static void write_etc_hosts(FILE *f);
 const char* target_prefix __P((void));  
   
 /*  /*
  * URL encode unsafe characters.  See RFC 1738.   * URL encode unsafe characters.  See RFC 1738.
Line 74  const char* target_prefix __P((void));
Line 75  const char* target_prefix __P((void));
  * The result is always a nul-terminated string even if it had to be   * The result is always a nul-terminated string even if it had to be
  * truncated to avoid overflowing the available space.   * truncated to avoid overflowing the available space.
  *   *
    * This url_encode() function does not operate on complete URLs, it
    * operates on strings that make up parts of URLs.  For example, in a
    * URL like "ftp://username:password@host/path", the username, password,
    * host and path should each be encoded separately before they are
    * joined together with the punctuation characters.
    *
    * In most ordinary use, the path portion of a URL does not start with
    * a slash; the slash is a separator between the host portion and the
    * path portion, and is dealt with by software outside the url_encode()
    * function.  However, it is valid for url_encode() to be passed a
    * string that does begin with a slash.  For example, the string might
    * represent a password, or a path part of a URL that the user really
    * does want to begin with a slash.
    *
  * len is the length of the destination buffer.  The result will be   * len is the length of the destination buffer.  The result will be
  * truncated if necessary to fit in the destination buffer.   * truncated if necessary to fit in the destination buffer.
  *   *
  * safe_chars is a string of characters that should not be encoded.  Any   * safe_chars is a string of characters that should not be encoded.  If
  * characters in this string, as well as any alphanumeric characters,   * safe_chars is non-NULL, any characters in safe_chars as well as any
  * will be copied from src to dst without encoding.  Some potentially   * alphanumeric characters will be copied from src to dst without
  * useful settings for this parameter are:   * encoding.  Some potentially useful settings for this parameter are:
  *   *
  *      NULL or ""      Everything except alphanumerics are encoded   *      NULL            Everything is encoded (even alphanumerics)
    *      ""              Everything except alphanumerics are encoded
  *      "/"             Alphanumerics and '/' remain unencoded   *      "/"             Alphanumerics and '/' remain unencoded
  *      "$-_.+!*'(),"   Consistent with a strict reading of RFC 1738   *      "$-_.+!*'(),"   Consistent with a strict reading of RFC 1738
  *      "$-_.+!*'(),/"  As above, except '/' is not encoded   *      "$-_.+!*'(),/"  As above, except '/' is not encoded
  *      "-_.+!,/"       As above, except shell special characters are encoded   *      "-_.+!,/"       As above, except shell special characters are encoded
  *   *
    * encode_leading_slash is a flag that determines whether or not to
    * encode a leading slash in a string.  If this flag is set, and if the
    * first character in the src string is '/', then the leading slash will
    * be encoded (as "%2F"), even if '/' is one of the characters in the
    * safe_chars string.  Note that only the first character of the src
    * string is affected by this flag, and that leading slashes are never
    * deleted, but either retained unchanged or encoded.
    *
  * Unsafe and reserved characters are defined in RFC 1738 section 2.2.   * Unsafe and reserved characters are defined in RFC 1738 section 2.2.
  * The most important parts are:   * The most important parts are:
  *   *
Line 108  const char* target_prefix __P((void));
Line 132  const char* target_prefix __P((void));
   
 static char *  static char *
 url_encode(char *dst, const char *src, size_t len,  url_encode(char *dst, const char *src, size_t len,
         const char *safe_chars)          const char *safe_chars, int encode_leading_slash)
 {  {
         char *p = dst;          char *p = dst;
   
         if (safe_chars == NULL)          /*
                 safe_chars = "";           * If encoding of a leading slash was desired, and there was in
            * fact one or more leading slashes, encode one in the output string.
            */
           if (encode_leading_slash && *src == '/') {
                   if (len < 3)
                           goto done;
                   sprintf(p, "%%%02X", '/');
                   src++;
                   p += 3;
                   len -= 3;
           }
   
         while (--len > 0 && *src != '\0') {          while (--len > 0 && *src != '\0') {
                 if (isalnum(*src) || strchr(safe_chars, *src)) {                  if (safe_chars != NULL &&
                       (isalnum(*src) || strchr(safe_chars, *src))) {
                         *p++ = *src++;                          *p++ = *src++;
                 } else {                  } else {
                         /* encode this char */                          /* encode this char */
Line 126  url_encode(char *dst, const char *src, s
Line 162  url_encode(char *dst, const char *src, s
                         len -= 2;                          len -= 2;
                 }                  }
         }          }
   done:
         *p = '\0';          *p = '\0';
         return dst;          return dst;
 }  }
   
   static const char *ignored_if_names[] = {
           "eon",                  /* netiso */
           "gre",                  /* net */
           "ipip",                 /* netinet */
           "lo",                   /* net */
   #if 0
           "mdecap",               /* netinet -- never in IF list (?) XXX */
   #endif
           "nsip",                 /* netns */
           "ppp",                  /* net */
           "sl",                   /* net */
           "strip",                /* net */
           "tun",                  /* net */
           /* XXX others? */
           NULL,
   };
   
 static void  static void
 get_ifconfig_info()  get_ifconfig_info()
 {  {
         char *textbuf;          char *textbuf;
         int   textsize;          char *t, *nt, *ndest;
         char *t;          const char **ignore;
           int textsize, len;
   
         /* Get ifconfig information */          /* Get ifconfig information */
   
Line 147  get_ifconfig_info()
Line 202  get_ifconfig_info()
                 exit(1);                  exit(1);
         }          }
         (void)strtok(textbuf,"\n");          (void)strtok(textbuf,"\n");
         strncpy(net_devices, textbuf, textsize<STRSIZE ? textsize : STRSIZE);  
         net_devices[STRSIZE] = 0;  
         free(textbuf);  
   
         /* Remove lo0 and anything after ... */          nt = textbuf;
         t = strstr(net_devices, "lo0");          ndest = net_devices;
         if (t != NULL)          *ndest = '\0';
                 *t = 0;          while ((t = strsep(&nt, " ")) != NULL) {
                   for (ignore = ignored_if_names; *ignore != NULL; ignore++) {
                           len = strlen(*ignore);
                           if (strncmp(t, *ignore, len) == 0 &&
                               isdigit((unsigned char)t[len]))
                                   goto loop;
                   }
   
                   if (strlen(ndest) + 1 + strlen(t) + 1 > STRSIZE)
                           break;                  /* would overflow */
   
                   strcat(ndest, t);
                   strcat(ndest, " ");     /* net_devices needs trailing space! */
   loop:
                   t = nt;
           }
           free(textbuf);
 }  }
   
 /* Fill in defaults network values for the selected interface */  /* Fill in defaults network values for the selected interface */
Line 216  config_network()
Line 284  config_network()
         if (network_up)          if (network_up)
                 return (1);                  return (1);
   
         network_up = 1;  
         net_devices[0] = '\0';          net_devices[0] = '\0';
         get_ifconfig_info();          get_ifconfig_info();
         if (strlen(net_devices) == 0) {          if (strlen(net_devices) == 0) {
Line 225  config_network()
Line 292  config_network()
                 process_menu(MENU_ok);                  process_menu(MENU_ok);
                 return (-1);                  return (-1);
         }          }
           network_up = 1;
   
         strncpy(defname, net_devices, 255);          strncpy(defname, net_devices, 255);
         tp = defname;          tp = defname;
         strsep(&tp, " ");          strsep(&tp, " ");
Line 316  config_network()
Line 385  config_network()
                 fclose(f);                  fclose(f);
         }          }
   
         run_prog(0, 0, "/sbin/ifconfig lo0 127.0.0.1");          run_prog(0, 0, NULL, "/sbin/ifconfig lo0 127.0.0.1");
   
         /*          /*
          * ifconfig does not allow media specifiers on IFM_MANUAL interfaces.           * ifconfig does not allow media specifiers on IFM_MANUAL interfaces.
          * Our UI gies no way to set an option back to null-string if it           * Our UI gies no way to set an option back to null-string if it
          * gets accidentally set.           * gets accidentally set.
          * good way to re-set the media media to null-string.           * good way to reset the media to null-string.
          * Check for plausible alternatives.           * Check for plausible alternatives.
          */           */
         if (strcmp(net_media, "<default>") == 0 ||          if (strcmp(net_media, "<default>") == 0 ||
Line 336  config_network()
Line 405  config_network()
         }          }
   
         if (*net_media != '\0')          if (*net_media != '\0')
                 run_prog(0, 0, "/sbin/ifconfig %s inet %s netmask %s media %s",                  run_prog(0, 0, NULL,
                       "/sbin/ifconfig %s inet %s netmask %s media %s",
                           net_dev, net_ip, net_mask, net_media);                            net_dev, net_ip, net_mask, net_media);
         else          else
                 run_prog(0, 0, "/sbin/ifconfig %s inet %s netmask %s", net_dev,                  run_prog(0, 0, NULL,
                       "/sbin/ifconfig %s inet %s netmask %s", net_dev,
                           net_ip, net_mask);                            net_ip, net_mask);
   
         /* Set host name */          /* Set host name */
Line 348  config_network()
Line 419  config_network()
   
         /* Set a default route if one was given */          /* Set a default route if one was given */
         if (strcmp(net_defroute, "") != 0) {          if (strcmp(net_defroute, "") != 0) {
                 run_prog(0, 0, "/sbin/route -n flush");                  run_prog(0, 0, NULL,
                 run_prog(0, 0, "/sbin/route -n add default %s",                      "/sbin/route -n flush");
                   run_prog(0, 0, NULL,
                       "/sbin/route -n add default %s",
                           net_defroute);                            net_defroute);
         }          }
   
Line 359  config_network()
Line 432  config_network()
          */           */
   
         if (strcmp(net_namesvr, "") != 0 && network_up)          if (strcmp(net_namesvr, "") != 0 && network_up)
                 network_up = !run_prog(0, 1, "/sbin/ping -c 2 %s",                  network_up = !run_prog(0, 1, NULL,
                       "/sbin/ping -v -c 5 -w 5 -o -n %s",
                                         net_namesvr);                                          net_namesvr);
   
         if (strcmp(net_defroute, "") != 0 && network_up)          if (strcmp(net_defroute, "") != 0 && network_up)
                 network_up = !run_prog(0, 1, "/sbin/ping -c 2 %s",                  network_up = !run_prog(0, 1, NULL,
                       "/sbin/ping -v -c 5 -w 5 -o -n %s",
                                         net_defroute);                                          net_defroute);
         fflush(NULL);          fflush(NULL);
   
Line 380  get_via_ftp()
Line 455  get_via_ftp()
         char filename[SSTRSIZE];          char filename[SSTRSIZE];
         int  ret;          int  ret;
   
         while (!config_network()) {          while ((ret = config_network()) <= 0) {
                   if (ret < 0)
                           return (-1);
                 msg_display(MSG_netnotup);                  msg_display(MSG_netnotup);
                 process_menu(MENU_yesno);                  process_menu(MENU_yesno);
                 if (!yesno)                  if (!yesno)
Line 395  get_via_ftp()
Line 472  get_via_ftp()
         strncat(ftp_dir, machine, STRSIZE - strlen(ftp_dir));          strncat(ftp_dir, machine, STRSIZE - strlen(ftp_dir));
         strncat(ftp_dir, ftp_prefix, STRSIZE - strlen(ftp_dir));          strncat(ftp_dir, ftp_prefix, STRSIZE - strlen(ftp_dir));
         process_menu(MENU_ftpsource);          process_menu(MENU_ftpsource);
   
         list = dist_list;          list = dist_list;
         while (list->name) {          while (list->name) {
                 if (!list->getit) {                  if (!list->getit) {
Line 412  get_via_ftp()
Line 489  get_via_ftp()
                  * "@", ":" and "/" need quoting).  Let's be                   * "@", ":" and "/" need quoting).  Let's be
                  * paranoid and also encode ftp_user and ftp_dir.  (For                   * paranoid and also encode ftp_user and ftp_dir.  (For
                  * example, ftp_dir could easily contain '~', which is                   * example, ftp_dir could easily contain '~', which is
                  * unsafe by a strict reading of RFC 1738).  There's                   * unsafe by a strict reading of RFC 1738).
                  * no need to encode the ftp_host or filename parts  
                  * of the URL for consumption by ftp, but we may need  
                  * to protect them from the shell, so we wrap the  
                  * whole URL in quotes for the shell.  
                  */                   */
                 if (strcmp ("ftp", ftp_user) == 0)                  if (strcmp ("ftp", ftp_user) == 0)
                         ret = run_prog(0, 1, "/usr/bin/ftp -a 'ftp://%s/%s/%s'",                          ret = run_prog(0, 1, NULL,
                               "/usr/bin/ftp -a ftp://%s/%s/%s",
                             ftp_host,                              ftp_host,
                             url_encode(ftp_dir_encoded, ftp_dir, STRSIZE,                              url_encode(ftp_dir_encoded, ftp_dir, STRSIZE,
                                         RFC1738_SAFE_LESS_SHELL_PLUS_SLASH),                                          RFC1738_SAFE_LESS_SHELL_PLUS_SLASH, 1),
                             filename);                              filename);
                 else {                  else {
                         ret = run_prog(0, 1, "/usr/bin/ftp 'ftp://%s:%s@%s/%s/%s'",                          ret = run_prog(0, 1, NULL,
                               "/usr/bin/ftp ftp://%s:%s@%s/%s/%s",
                             url_encode(ftp_user_encoded, ftp_user, STRSIZE,                              url_encode(ftp_user_encoded, ftp_user, STRSIZE,
                                         RFC1738_SAFE_LESS_SHELL),                                          RFC1738_SAFE_LESS_SHELL, 0),
                             url_encode(ftp_pass_encoded, ftp_pass, STRSIZE,                              url_encode(ftp_pass_encoded, ftp_pass, STRSIZE,
                                         RFC1738_SAFE_LESS_SHELL),                                          NULL, 0),
                             ftp_host,                              ftp_host,
                             url_encode(ftp_dir_encoded, ftp_dir, STRSIZE,                              url_encode(ftp_dir_encoded, ftp_dir, STRSIZE,
                                         RFC1738_SAFE_LESS_SHELL_PLUS_SLASH),                                          RFC1738_SAFE_LESS_SHELL_PLUS_SLASH, 1),
                             filename);                              filename);
                 }                  }
                 if (ret) {                  if (ret) {
                         /* Error getting the file.  Bad host name ... ? */                          /* Error getting the file.  Bad host name ... ? */
                         msg_display(MSG_ftperror_cont);                          msg_display(MSG_ftperror_cont);
                         getchar();                          getchar();
                         puts(CL);                          puts(CL);               /* XXX */
                           wclear(stdscr);
                         wrefresh(stdscr);                          wrefresh(stdscr);
                         msg_display(MSG_ftperror);                          msg_display(MSG_ftperror);
                         process_menu(MENU_yesno);                          process_menu(MENU_yesno);
Line 451  get_via_ftp()
Line 527  get_via_ftp()
                         list++;                          list++;
   
         }          }
         puts(CL); /* Just to make sure. */          puts(CL);               /* XXX */
           wclear(stdscr);
         wrefresh(stdscr);          wrefresh(stdscr);
 #ifndef DEBUG  #ifndef DEBUG
         chdir("/");     /* back to current real root */          chdir("/");     /* back to current real root */
Line 462  get_via_ftp()
Line 539  get_via_ftp()
 int  int
 get_via_nfs()  get_via_nfs()
 {  {
           int ret;
   
         while (!config_network()) {          while ((ret = config_network()) <= 0) {
                   if (ret < 0)
                           return (-1);
                 msg_display(MSG_netnotup);                  msg_display(MSG_netnotup);
                 process_menu(MENU_yesno);                  process_menu(MENU_yesno);
                 if (!yesno)                  if (!yesno)
Line 474  get_via_nfs()
Line 554  get_via_nfs()
         process_menu(MENU_nfssource);          process_menu(MENU_nfssource);
 again:  again:
   
         run_prog(0, 0, "/sbin/umount /mnt2");          run_prog(0, 0, NULL,
               "/sbin/umount /mnt2");
   
         /* Mount it */          /* Mount it */
         if (run_prog(0, 0, "/sbin/mount -r -o -i,-r=1024 -t nfs %s:%s /mnt2",          if (run_prog(0, 0, NULL,
               "/sbin/mount -r -o -i,-r=1024 -t nfs %s:%s /mnt2",
             nfs_host, nfs_dir)) {              nfs_host, nfs_dir)) {
                 msg_display(MSG_nfsbadmount, nfs_host, nfs_dir);                  msg_display(MSG_nfsbadmount, nfs_host, nfs_dir);
                 process_menu(MENU_nfsbadmount);                  process_menu(MENU_nfsbadmount);
Line 505  again:
Line 587  again:
 }  }
   
 /*  /*
    * write the new contents of /etc/hosts to the specified file
    */
   static void
   write_etc_hosts(FILE *f)
   {
           int l;
   
           fprintf(f, "#\n");
           fprintf(f, "# Added by NetBSD sysinst\n");
           fprintf(f, "#\n");
   
           fprintf(f, "127.0.0.1   localhost\n");
   
           fprintf(f, "%s\t", net_ip);
           l = strlen(net_host) - strlen(net_domain);
           if (l <= 0 ||
               net_host[l - 1] != '.' ||
               strcasecmp(net_domain, net_host + l) != 0) {
                   /* net_host isn't an FQDN. */
                   fprintf(f, "%s.%s ", net_host, net_domain);
           }
           fprintf(f, "%s\n", net_host);
   }
   
   /*
  * Write the network config info the user entered via menus into the   * Write the network config info the user entered via menus into the
  * config files in the target disk.  Be careful not to lose any   * config files in the target disk.  Be careful not to lose any
  * information we don't immediately add back, in case the install   * information we don't immediately add back, in case the install
Line 542  mnt_net_config(void)
Line 649  mnt_net_config(void)
                          */                           */
                         f = target_fopen("/etc/hosts", "a");                          f = target_fopen("/etc/hosts", "a");
                         if (f != 0) {                          if (f != 0) {
                                 (void)fprintf(f, msg_string(MSG_etc_hosts),                                  write_etc_hosts(f);
                                     net_ip, net_host, net_domain, net_host);  
                                 (void)fclose(f);                                  (void)fclose(f);
                                 if (scripting) {                                  if (scripting) {
                                         (void)fprintf(script, "cat <<EOF >>%s/etc/hosts\n", target_prefix());                                          (void)fprintf(script, "cat <<EOF >>%s/etc/hosts\n", target_prefix());
                                         (void)fprintf(script, msg_string(MSG_etc_hosts),                                          write_etc_hosts(script);
                                             net_ip, net_host, net_domain, net_host);  
                                         (void)fprintf(script, "EOF\n");                                          (void)fprintf(script, "EOF\n");
                                 }                                  }
                         }                          }

Legend:
Removed from v.1.38  
changed lines
  Added in v.1.38.2.4

CVSweb <webmaster@jp.NetBSD.org>