home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume26 / pcomm-2.0.2 / part03 / xmodem.c < prev   
Encoding:
C/C++ Source or Header  |  1993-04-13  |  8.6 KB  |  441 lines

  1. /*
  2.  * Miscellaneous routines to support the xmodem file transfer protocols.
  3.  */
  4.  
  5. #define TMP_FILE    "trunXXXXXX"
  6.  
  7. #include <stdio.h>
  8. #include <signal.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include "config.h"
  12. #include "misc.h"
  13. #include "param.h"
  14. #include "xmodem.h"
  15.  
  16. #ifdef BSD
  17. #include <setjmp.h>
  18. jmp_buf gl_buf, rl_buf;
  19. #endif /* BSD */
  20.  
  21. static int uid_link(), uid_unlink();
  22.  
  23. /*
  24.  * Calculate the CRC for the given buffer
  25.  */
  26.  
  27. unsigned short
  28. calc_crc(buf, len)
  29. unsigned char *buf;
  30. int len;
  31. {
  32.     register int i;
  33.     unsigned short crc;
  34.     static unsigned short crctab[256] = {
  35.     0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7,
  36.     0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef,
  37.     0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6,
  38.     0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de,
  39.     0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485,
  40.     0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d,
  41.     0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4,
  42.     0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc,
  43.     0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823,
  44.     0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b,
  45.     0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12,
  46.     0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a,
  47.     0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41,
  48.     0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49,
  49.     0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70,
  50.     0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78,
  51.     0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f,
  52.     0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067,
  53.     0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e,
  54.     0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256,
  55.     0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d,
  56.     0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
  57.     0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c,
  58.     0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634,
  59.     0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab,
  60.     0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3,
  61.     0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a,
  62.     0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92,
  63.     0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9,
  64.     0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1,
  65.     0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8,
  66.     0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0};
  67.  
  68.     crc = 0;
  69.     for (i=0; i<len; i++)
  70.         crc = (crc<<8) ^ crctab[(crc>>8) ^ *buf++];
  71.  
  72.     return(crc);
  73. }
  74.  
  75. /*
  76.  * Calculate the checksum for the given buffer.
  77.  */
  78.  
  79. unsigned char
  80. calc_sum(buf, len)
  81. unsigned char *buf;
  82. int len;
  83. {
  84.     unsigned char sum;
  85.  
  86.     sum = 0;
  87.     while (--len >= 0)
  88.         sum += *buf++;
  89.  
  90.     return(sum);
  91. }
  92.  
  93. /*
  94.  * Get a single character from the line with a specified time-out period
  95.  * in seconds.  If the function times-out, it returns a -1.
  96.  */
  97.  
  98. static int gl_flag;
  99. static int gl_force();
  100.  
  101. int
  102. getc_line(sec)
  103. unsigned int sec;
  104. {
  105.     extern int fd;
  106.     char c;
  107.     unsigned int alarm();
  108.  
  109.     signal(SIGALRM, (SIG_TYPE(*) ()) gl_force);
  110.     gl_flag = 0;
  111.  
  112.     alarm(sec);
  113.  
  114. #ifdef BSD
  115.     if (setjmp(gl_buf))
  116.         return(-1);
  117. #endif /* BSD */
  118.  
  119.     if (read(fd, &c, 1) <= 0) {
  120.         alarm(0);
  121.         return(-1);
  122.     }
  123.     if (gl_flag)
  124.         return(-1);
  125.     alarm(0);
  126.     return(c & 0xff);
  127. }
  128.  
  129. /* ARGSUSED */
  130. static int
  131. gl_force(dummy)
  132. int dummy;
  133. {
  134. #ifdef BSD
  135.     longjmp(gl_buf, 1);
  136. #else /* BSD */
  137.     signal(SIGALRM, (SIG_TYPE(*) ()) gl_force);
  138.     gl_flag = 1;
  139.     return(0);
  140. #endif /* BSD */
  141. }
  142.  
  143. /*
  144.  * Same as above, but reads a bunch of characters.  The return code is
  145.  * now just a success/fail indicator.
  146.  */
  147.  
  148. static int rl_flag;
  149. static int rl_force();
  150.  
  151. int
  152. fread_line(buf, len, sec)
  153. unsigned char *buf;
  154. unsigned int len, sec;
  155. {
  156.     extern int fd;
  157.     int n;
  158.     unsigned int try, alarm();
  159.  
  160.     signal(SIGALRM, (SIG_TYPE(*) ()) rl_force);
  161.     rl_flag = 0;
  162.  
  163.     alarm(sec);
  164.     while (len) {
  165.                     /* read at most CLIST_SIZ chars */
  166.         try = (len > CLIST_SIZ) ? CLIST_SIZ : len;
  167. #ifdef BSD
  168.         if (setjmp(rl_buf))
  169.             return(-1);
  170. #endif /* BSD */
  171.         if ((n = read(fd, (char *) buf, try)) <= 0) {
  172.             alarm(0);
  173.             return(-1);
  174.         }
  175.         if (rl_flag)
  176.             return(-1);
  177.         len -= n;
  178.         buf = buf + n;
  179.     }
  180.     alarm(0);
  181.     return(0);
  182. }
  183.  
  184. /* ARGSUSED */
  185. static int
  186. rl_force(dummy)
  187. int dummy;
  188. {
  189. #ifdef BSD
  190.     longjmp(rl_buf, 1);
  191. #else /* BSD */
  192.     signal(SIGALRM, (SIG_TYPE(*) ()) rl_force);
  193.     rl_flag = 1;
  194.     return(0);
  195. #endif /* BSD */
  196. }
  197.  
  198. /*
  199.  * Put a character on the TTY line.  This serves no useful purpose other
  200.  * than making the code look pretty.
  201.  */
  202.  
  203. int
  204. putc_line(c)
  205. unsigned char c;
  206. {
  207.     extern int fd;
  208.  
  209.     return(write(fd, (char *) &c, 1));
  210. }
  211.  
  212. /*
  213.  * Cancel the file transfer.  Send several ^X's to the remote, followed
  214.  * by an equal number of backspaces (in case they have already aborted and
  215.  * we're really at the command line).
  216.  */
  217.  
  218. void
  219. cancel_xfer(up)
  220. int up;
  221. {
  222.     extern char file_name[15];
  223.  
  224.     if (!up && !strcmp(param->abort, "DELETE"))
  225.         unlink(file_name);
  226.  
  227.     putc_line(CAN);
  228.     putc_line(CAN);
  229.     putc_line(CAN);
  230.     putc_line(BS);
  231.     putc_line(BS);
  232.     putc_line(BS);
  233.     return;
  234. }
  235.  
  236. /*
  237.  * Shorten a file to a predetermined length.  Used to remove the ^Z
  238.  * padding from the end of files.  (Heaven help us, if one day a binary
  239.  * file actually has ^Z's as part of the end of the file).
  240.  */
  241.  
  242. int
  243. fix_length(file, len)
  244. char *file;
  245. long len;
  246. {
  247.     FILE *fp, *tempfp, *uid_fopen();
  248.     register int num;
  249.     char *mktemp(), tempfile[128], buf[BUFSIZ], *strcpy();
  250.     char *s, *strrchr(), *strcat();
  251.     struct stat stbuf;
  252.  
  253.     if (stat(file, &stbuf) < 0)
  254.         return(1);
  255.                     /* see if we have any work to do */
  256.     if (len >= stbuf.st_size)
  257.         return(0);
  258.  
  259. #ifdef HAVE_TRUNCATE
  260.     return(uid_truncate(file, len));
  261. #else /* HAVE_TRUNCATE */
  262.  
  263.     if (!(fp = uid_fopen(file, "r")))
  264.         return(1);
  265.  
  266.     /*
  267.      * The temporary file should be in the same directory as the
  268.      * file being received because otherwise we'd have no way of
  269.      * guaranteeing they would be in the same file system.  (Hard
  270.      * links across different file systems aren't allowed).
  271.      */
  272.     strcpy(tempfile, file);
  273.     if (s = strrchr(tempfile, '/'))
  274.         *++s = '\0';
  275.     else
  276.         strcpy(tempfile, "./");
  277.  
  278.     strcat(tempfile, TMP_FILE);
  279.     mktemp(tempfile);
  280.  
  281.     if (!(tempfp = uid_fopen(tempfile, "w"))) {
  282.         fclose(fp);
  283.         return(1);
  284.     }
  285.  
  286.     while (len != 0L) {
  287.         num = (len > BUFSIZ) ? BUFSIZ : (int) len;
  288.         fread(buf, sizeof(char), num, fp);
  289.         if (fwrite(buf, sizeof(char), num, tempfp) != num) {
  290.             fclose(fp);
  291.             fclose(tempfp);
  292.             return(1);
  293.         }
  294.         len -= (unsigned int) num;
  295.     }
  296.  
  297.     fclose(fp);
  298.     fclose(tempfp);
  299.  
  300.     if (uid_unlink(file) < 0)
  301.         return(1);
  302.  
  303.     if (uid_link(tempfile, file) < 0)
  304.         return(1);
  305.  
  306.     if (uid_unlink(tempfile) < 0)
  307.         return(1);
  308.  
  309.     return(0);
  310. #endif /* HAVE_TRUNCATE */
  311. }
  312.  
  313. static int
  314. uid_link(path1, path2)
  315. char *path1, *path2;
  316. {
  317.     int ret;
  318.  
  319. #ifdef SETUID_BROKE
  320.     int status;
  321.     void _exit();
  322.  
  323.     switch(fork()) {
  324.         case 0:
  325.             setuid(getuid());
  326.             setgid(getgid());
  327.             _exit(link(path1, path2));
  328.         case -1:
  329.             fprintf(stderr, "uid_link: Can't fork\n");
  330.             return(-1);
  331.         default:
  332.             if (wait(&status) == -1) {
  333.                 fprintf(stderr, "uid_link: wait failed\n");
  334.                 return(-1);
  335.             }
  336.             ret = status >> 8;
  337.     }
  338. #else /* SETUID_BROKE */
  339.     int euid, egid;
  340.  
  341.     euid = geteuid();
  342.     egid = getegid();
  343.  
  344.     setuid(getuid());
  345.     setgid(getgid());
  346.  
  347.     ret = link(path1, path2);
  348.  
  349.     setuid(euid);
  350.     setgid(egid);
  351. #endif /* SETUID_BROKE */
  352.     return(ret);
  353. }
  354.  
  355. static int
  356. uid_unlink(path)
  357. char *path;
  358. {
  359.     int ret;
  360.  
  361. #ifdef SETUID_BROKE
  362.     int status;
  363.     void _exit();
  364.  
  365.     switch(fork()) {
  366.         case 0:
  367.             setuid(getuid());
  368.             setgid(getgid());
  369.             _exit(unlink(path));
  370.         case -1:
  371.             fprintf(stderr, "uid_unlink: Can't fork\n");
  372.             return(-1);
  373.         default:
  374.             if (wait(&status) == -1) {
  375.                 fprintf(stderr, "uid_unlink: wait failed\n");
  376.                 return(-1);
  377.             }
  378.             ret = status >> 8;
  379.     }
  380. #else /* SETUID_BROKE */
  381.     int euid, egid;
  382.  
  383.     euid = geteuid();
  384.     egid = getegid();
  385.  
  386.     setuid(getuid());
  387.     setgid(getgid());
  388.  
  389.     ret = unlink(path);
  390.  
  391.     setuid(euid);
  392.     setgid(egid);
  393. #endif /* SETUID_BROKE */
  394.     return(ret);
  395. }
  396.  
  397. #ifdef HAVE_TRUNCATE
  398. static int
  399. uid_truncate(path, len)
  400. char *path;
  401. long len;
  402. {
  403.     int ret;
  404.  
  405. #ifdef SETUID_BROKE
  406.     int status;
  407.     void _exit();
  408.  
  409.     switch(fork()) {
  410.         case 0:
  411.             setuid(getuid());
  412.             setgid(getgid());
  413.             _exit(truncate(path, len));
  414.         case -1:
  415.             fprintf(stderr, "uid_truncate: Can't fork\n");
  416.             return(-1);
  417.         default:
  418.             if (wait(&status) == -1) {
  419.                 fprintf(stderr, "uid_truncate: wait failed\n");
  420.                 return(-1);
  421.             }
  422.             ret = status >> 8;
  423.     }
  424. #else /* SETUID_BROKE */
  425.     int euid, egid;
  426.  
  427.     euid = geteuid();
  428.     egid = getegid();
  429.  
  430.     setuid(getuid());
  431.     setgid(getgid());
  432.  
  433.     ret = truncate(path, len);
  434.  
  435.     setuid(euid);
  436.     setgid(egid);
  437. #endif /* SETUID_BROKE */
  438.     return(ret);
  439. }
  440. #endif /* HAVE_TRUNCATE */
  441.