home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / stg_v4.lzh / password.c < prev    next >
C/C++ Source or Header  |  1994-11-11  |  5KB  |  290 lines

  1. /*
  2.  * password - change user password
  3.  *
  4.  * 92/10/29 StG - new code from scratch for v4
  5. */
  6.  
  7. #include "stgnet.h"
  8. #include "pwd.h"
  9.  
  10. long lseek();
  11.  
  12. struct passwd *pw;
  13.  
  14. char buf[256];
  15.  
  16. char sysop_pw[32];
  17.  
  18. new(old)
  19. int old;
  20. {
  21.     char *p;
  22.     int zero=0;
  23.     char dir[64];
  24.  
  25.     setpwent();
  26.  
  27.     while (pw=getpwent())
  28.     {
  29.         if (!pw->pw_passwd || !*pw->pw_passwd || *pw->pw_passwd==' ')
  30.             strcpy(buf," no password ");
  31.         else if (strlen(pw->pw_passwd)==13)
  32.             strcpy(buf,pw->pw_passwd);
  33.         else
  34.             strcpy(buf,crypt(pw->pw_passwd,salt(pw->pw_name,pw->pw_passwd)));
  35.  
  36.         if (old)
  37.         {
  38.             /* swap exec & home dirs (old bug) */
  39.             p=pw->pw_xdir;
  40.             pw->pw_xdir=pw->pw_dir;
  41.             pw->pw_dir=p;
  42.  
  43.             /* name is after options field in old format */
  44.             p=strcut(pw->pw_gecos,',');
  45.             if (p)
  46.                 strcut(p,',');
  47.             else
  48.                 p="";
  49.             pw->pw_gecos=p;
  50.  
  51.             /* mangle user id from old format */
  52.             pw->pw_uid=(pw->pw_uid&0xFFFF0000)>>16;
  53.             if (pw->pw_uid)
  54.                 pw->pw_uid=((pw->pw_uid&0xFF00)<<8) + (pw->pw_uid&0x00FF) + 0x00010000;
  55.             else
  56.                 pw->pw_uid=zero++;
  57.  
  58.             if (!pw->pw_pri)
  59.                 pw->pw_pri=128;
  60.  
  61.             if (!pw->pw_xdir || !*pw->pw_xdir)
  62.                 pw->pw_xdir="/DD/CMDS";
  63.  
  64.             if (!pw->pw_dir || !*pw->pw_dir)
  65.                 pw->pw_dir=stringf(dir,"/DD/USER/%s",pw->pw_name);                
  66.  
  67.             if (!pw->pw_shell || !*pw->pw_shell)
  68.                 pw->pw_shell="smenu";
  69.         }
  70.  
  71.         stringf(b,"%s,%s,%d.%d,%d,%s,%s,%s,%s\n",
  72.             pw->pw_name,
  73.             buf,
  74.             (pw->pw_uid&0xFFFF0000)>>16,
  75.             (pw->pw_uid&0x0000FFFF),
  76.             pw->pw_pri,
  77.             pw->pw_xdir,
  78.             pw->pw_dir,
  79.             pw->pw_shell,
  80.             pw->pw_gecos);
  81.         writeln(1,b,256);
  82.     }
  83.     exit(0);
  84. }
  85.  
  86. main(argc,argv)
  87. char **argv;
  88. {
  89.     int hPW;
  90.     int n;
  91.     char *p;
  92.     long pos;
  93.     int uid;
  94.  
  95.     dash(argv)
  96.     {
  97.     case '#':
  98.         wstringf(2,"password: %s\n",STG_VER);
  99.         exit(0);
  100.  
  101.     case '?':
  102.         writeln(2,"password {user} - change user password\n",80);
  103.         STGVER;
  104.         exit(0);
  105.     case 'n':
  106.         if (getuid())
  107.             exit(164);
  108.         new(0);
  109.     case '3':
  110.         if (getuid())
  111.             exit(164);
  112.         new(1);
  113.     default:
  114.         stringf(b,"password: invalid option: %s\n",--*argv);
  115.         writeln(2,b,80);
  116.         exit(1);
  117.     }
  118.  
  119.     *sysop_pw=0;
  120.     pw=getpwuid(0);
  121.     if (pw)
  122.         strcpy(sysop_pw,pw->pw_passwd);
  123.  
  124.     if (*argv)
  125.     {
  126.         pw=getpwnam(*argv);
  127.         if (!pw)
  128.         {
  129.             writeln(2,stringf(b,"password: no such user %s\n",*argv),80);
  130.             exit(1);
  131.         }
  132.     }
  133.     else
  134.     {
  135.         pw=getpwuid(getuid());
  136.         if (!pw)
  137.         {
  138.             syslog(LOG_EMERG,"unknown user # %d",getuid());
  139.             writeln(2,"Who are you?\n",80);
  140.             exit(1);        
  141.         }
  142.     }
  143.  
  144.     stringf(b,"Changing password for %s\n",pw->pw_name);
  145.     writeln(2,b,80);
  146.  
  147.     if (setuid(0)==ERR)
  148.     {
  149.         syslog(LOG_ALERT,"cant setuid %m");
  150.         writeln(2,"password: internal error - contact sysop\n",80);
  151.         exit(1);
  152.     }
  153.  
  154.     /* locate password entry */
  155.     hPW=open(PWD_FILE,O_RDWR);
  156.     if (hPW==ERR)
  157.     {
  158.         syslog(LOG_ALERT,"cant open %s %m",PWD_FILE);
  159.         writeln(2,"password: internal error - contact sysop\n",80);
  160.         exit(1);
  161.     }
  162.  
  163.     while (n=readln(hPW,buf,256))
  164.     {
  165.         if (n==ERR)
  166.         {
  167.             syslog(LOG_ALERT,"read error %m");
  168.             writeln(2,"password: internal error - contact sysop\n",80);
  169.             exit(1);
  170.         }
  171.         pos=lseek(hPW,0L,1)-n;
  172.  
  173.         p=strcut(buf,',');
  174.         if (!p)
  175.         {
  176.             n=0;
  177.             break;
  178.         }
  179.         if (!strcmp(buf,pw->pw_name))
  180.             break;
  181.     }
  182.     if (!n)
  183.     {
  184.         syslog(LOG_ALERT,"cant find user %s",pw->pw_name);
  185.         writeln(2,"Who are you?\n",80);
  186.         exit(1);
  187.     }
  188.  
  189.     strcut(p,',');
  190.     n=strlen(p);
  191.     if (!n)
  192.     {
  193.         syslog(LOG_ERR,"zero length password for user %s",pw->pw_name);
  194.         if (pw->pw_uid<=15)
  195.             writeln(2,"Cant change zero length password - edit file manually please\n",80);
  196.         else
  197.             writeln(2,"Cant change your password - contact sysop\n",80);
  198.         exit(1);
  199.     }
  200.  
  201.     pos+=strlen(buf)+1;
  202.  
  203.     lseek(hPW,pos,0);
  204.  
  205.     if (*p==' ')
  206.         goto pass;
  207.  
  208.     writeln(2,"Enter old password for verification:\n",80);
  209.     writeln(2,"Password: ",10);
  210.     if (get_line(b,32,'*')==ERR)
  211.         exit(1);
  212.  
  213.     if (strlen(pw->pw_passwd)==13)
  214.     {
  215.         strlcs(b);
  216.         if (!strcmp(pw->pw_passwd,crypt(b,pw->pw_passwd)))
  217.             goto pass;
  218.  
  219.         if (!strcmp(sysop_pw,crypt(b,sysop_pw)))
  220.             goto pass;
  221.     }
  222.     else
  223.     {
  224.         if (!strcmp(pw->pw_passwd,b))
  225.             goto pass;
  226.     }
  227.  
  228.     syslog(LOG_BADPW,"%s (%s)",pw->pw_name,pw->pw_gecos);
  229.     writeln(2,"BAD PASSWORD\7\n",80);
  230.     exit(1);
  231.  
  232. pass:    
  233.     writeln(2,"Enter new password twice:\n",80);
  234.     writeln(2,"Password: ",10);
  235.     if (get_line(b,32,'*')==ERR)
  236.         exit(1);
  237.  
  238.     writeln(2,"Password: ",10);
  239.     if (get_line(b+64,32,'*')==ERR)
  240.         exit(1);
  241.  
  242.     if (strcmp(b,b+64))
  243.     {
  244.         writeln(2,"Please try again\n",80);
  245.         goto pass;
  246.     }
  247.  
  248.     if (!*b)
  249.     {
  250.         memset(b,32,128);
  251.         if (write(hPW,b,n)==ERR)
  252.         {
  253.             syslog(LOG_ALERT,"write error to pw file %m");
  254.             writeln(2,"password: internal write error - contact sysop\n",80);
  255.             exit(1);
  256.         }
  257.         writeln(2,"Password removed\n",80);
  258.         exit(0);
  259.     }
  260.  
  261.     if (n==13)
  262.     {
  263.         strlcs(b);
  264.         if (write(hPW,crypt(b,salt(pw->pw_name,b)),13)==ERR)
  265.         {
  266.             syslog(LOG_ALERT,"write error to pw file %m");
  267.             writeln(2,"password: internal write error - contact sysop\n",80);
  268.             exit(1);
  269.         }
  270.         writeln(2,"Password changed\n",80);
  271.         exit(0);
  272.     }
  273.  
  274.     if (strlen(b)>n)
  275.     {
  276.         writeln(2,stringf(b,"Your password can only be %d characters long\n",n),80);
  277.         goto pass;
  278.     }
  279.  
  280.     memset(b+strlen(b),32,64);
  281.     if (write(hPW,b,n)==ERR)
  282.     {
  283.         syslog(LOG_ALERT,"write error to pw file %m");
  284.         writeln(2,"password: internal write error - contact sysop\n",80);
  285.         exit(1);
  286.     }
  287.     writeln(2,"Password changed\n",80);
  288.     exit(0);
  289. }
  290.