Annotation of 3BSD/cmd/chsh.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * chsh
                      3:  */
                      4: #include <stdio.h>
                      5: #include <signal.h>
                      6: #include <pwd.h>
                      7: 
                      8: char   passwd[] = "/etc/passwd";
                      9: char   temp[]   = "/etc/ptmp";
                     10: struct passwd *pwd;
                     11: struct passwd *getpwent();
                     12: int    endpwent();
                     13: char   *crypt();
                     14: char   *getpass();
                     15: char   *pw;
                     16: char   pwbuf[10];
                     17: char   buf[BUFSIZ];
                     18: 
                     19: main(argc, argv)
                     20: char *argv[];
                     21: {
                     22:        char *p;
                     23:        int i;
                     24:        char saltc[2];
                     25:        long salt;
                     26:        int u,fi,fo;
                     27:        int insist;
                     28:        int ok, flags;
                     29:        int c;
                     30:        int pwlen;
                     31:        FILE *tf;
                     32: 
                     33:        insist = 0;
                     34:        if(argc < 2 || argc > 3) {
                     35:                printf("Usage: chsh user [ /bin/csh ]\n");
                     36:                goto bex;
                     37:        }
                     38:        if (argc == 2)
                     39:                argv[2] = "";
                     40:        else if (strcmp(argv[2], "/bin/csh") && getuid()) {
                     41:                printf("Only /bin/csh may be specified\n");
                     42:                exit(1);
                     43:        }
                     44:        while((pwd=getpwent()) != NULL){
                     45:                if(strcmp(pwd->pw_name,argv[1]) == 0){
                     46:                        u = getuid();
                     47:                        if(u!=0 && u != pwd->pw_uid){
                     48:                                printf("Permission denied.\n");
                     49:                                goto bex;
                     50:                                }
                     51:                        break;
                     52:                        }
                     53:                }
                     54:        endpwent();
                     55:        signal(SIGHUP, 1);
                     56:        signal(SIGINT, 1);
                     57:        signal(SIGQUIT, 1);
                     58: 
                     59:        if(access(temp, 0) >= 0) {
                     60:                printf("Temporary file busy -- try again\n");
                     61:                goto bex;
                     62:        }
                     63:        if((tf=fopen(temp,"w")) == NULL) {
                     64:                printf("Cannot create temporary file\n");
                     65:                goto bex;
                     66:        }
                     67: 
                     68: /*
                     69:  *     copy passwd to temp, replacing matching lines
                     70:  *     with new shell.
                     71:  */
                     72: 
                     73:        while((pwd=getpwent()) != NULL) {
                     74:                if(strcmp(pwd->pw_name,argv[1]) == 0) {
                     75:                        u = getuid();
                     76:                        if(u != 0 && u != pwd->pw_uid) {
                     77:                                printf("Permission denied.\n");
                     78:                                goto out;
                     79:                        }
                     80:                        pwd->pw_shell = argv[2];
                     81:                }
                     82:                fprintf(tf,"%s:%s:%d:%d:%s:%s:%s\n",
                     83:                        pwd->pw_name,
                     84:                        pwd->pw_passwd,
                     85:                        pwd->pw_uid,
                     86:                        pwd->pw_gid,
                     87:                        pwd->pw_gecos,
                     88:                        pwd->pw_dir,
                     89:                        pwd->pw_shell);
                     90:        }
                     91:        endpwent();
                     92:        fclose(tf);
                     93: 
                     94: /*
                     95:  *     copy temp back to passwd file
                     96:  */
                     97: 
                     98:        if((fi=open(temp,0)) < 0) {
                     99:                printf("Temp file disappeared!\n");
                    100:                goto out;
                    101:        }
                    102:        if((fo=creat(passwd, 0644)) < 0) {
                    103:                printf("Cannot recreat passwd file.\n");
                    104:                goto out;
                    105:        }
                    106:        while((u=read(fi,buf,sizeof(buf))) > 0) write(fo,buf,u);
                    107: 
                    108: out:
                    109:        unlink(temp);
                    110: 
                    111: bex:
                    112:        exit(1);
                    113: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.