static int del_line_matching(char **args, const char *filename) { if (ENABLE_FEATURE_DEL_USER_FROM_GROUP && args[2]) { return update_passwd(filename, args[2], NULL, args[1]); } return update_passwd(filename, args[1], NULL, NULL); }
int addgroup_main(int argc UNUSED_PARAM, char **argv) { unsigned opts; unsigned gid = 0; /* need to be root */ if (geteuid()) { bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); } #if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS applet_long_options = addgroup_longopts; #endif /* Syntax: * addgroup group * addgroup -g num group * addgroup user group * Check for min, max and missing args */ opt_complementary = "-1:?2:g+"; opts = getopt32(argv, "g:S", &gid); /* move past the commandline options */ argv += optind; //argc -= optind; #if ENABLE_FEATURE_ADDUSER_TO_GROUP if (argv[1]) { struct group *gr; if (opts & OPT_GID) { /* -g was there, but "addgroup -g num user group" * is a no-no */ bb_show_usage(); } /* check if group and user exist */ xuname2uid(argv[0]); /* unknown user: exit */ gr = xgetgrnam(argv[1]); /* unknown group: exit */ /* check if user is already in this group */ for (; *(gr->gr_mem) != NULL; (gr->gr_mem)++) { if (!strcmp(argv[0], *(gr->gr_mem))) { /* user is already in group: do nothing */ return EXIT_SUCCESS; } } if (update_passwd(bb_path_group_file, argv[1], NULL, argv[0]) < 0) { return EXIT_FAILURE; } # if ENABLE_FEATURE_SHADOWPASSWDS update_passwd(bb_path_gshadow_file, argv[1], NULL, argv[0]); # endif } else #endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */ { die_if_bad_username(argv[0]); new_group(argv[0], gid); } /* Reached only on success */ return EXIT_SUCCESS; }
static int set_passwd_ident_ok(const char *name, char cond) { USEREC usr; if (get_passwd(&usr, name) > 0) { usr.ident = cond; if (update_passwd(&usr) > 0) return 0; } return -1; }
/* append a new user to the passwd file */ static void new_group(char *group, gid_t gid) { struct group gr; char *p; /* make sure gid and group haven't already been allocated */ gr.gr_gid = gid; gr.gr_name = group; xgroup_study(&gr); /* add entry to group */ p = xasprintf("x:%u:", (unsigned) gr.gr_gid); if (update_passwd(bb_path_group_file, group, p, NULL) < 0) exit(EXIT_FAILURE); if (ENABLE_FEATURE_CLEAN_UP) free(p); #if ENABLE_FEATURE_SHADOWPASSWDS /* /etc/gshadow fields: * 1. Group name. * 2. Encrypted password. * If set, non-members of the group can join the group * by typing the password for that group using the newgrp command. * If the value is of this field ! then no user is allowed * to access the group using the newgrp command. A value of !! * is treated the same as a value of ! only it indicates * that a password has never been set before. If the value is null, * only group members can log into the group. * 3. Group administrators (comma delimited list). * Group members listed here can add or remove group members * using the gpasswd command. * 4. Group members (comma delimited list). */ /* Ignore errors: if file is missing we assume admin doesn't want it */ update_passwd(bb_path_gshadow_file, group, "!::", NULL); #endif }
int main() { FILE *temp; char temp1[8], temp2[8]; /* temp = fopen("temp.txt", "r"); fscanf(temp, "%s\t%s", temp1, temp2); printf("temp1:%s\n", temp1); printf("temp2:%s\n", temp2); fscanf(temp, "%s\t%s", temp1, temp2); printf("temp1:%s\n", temp1); printf("temp2:%s\n", temp2); */ create_database(); add_user("ameyam", "passwd"); add_user("more", "pwd"); update_passwd("ameyam", "pppp"); update_passwd("upenn", "sd"); add_user("upenn","td"); printf("%d\n", match("more", "pwd")); printf("%d\n", match("mia", "pwd")); printf("%d\n", match("upenn", "uu")); }
/******************************************************************* * update user passwd file * *******************************************************************/ void UpdateUserRec(int action, USEREC * curuser, BOARDHEADER * board) { FILE *fp; char pathRec[PATHLEN]; if (!strcmp(curuser->userid, "guest") #ifdef NSYSUBBS || !strcmp(curuser->userid, "supertomcat") #endif ) return; if (difftime(request_rec->atime, curuser->lastlogin) > (double) RELOGIN_INTERVAL) { curuser->numlogins++; if (curuser->userlevel < PERM_NORMAL) curuser->userlevel++; } if (curuser->userlevel < 0 || curuser->userlevel > PERM_SYSOP) /* debug */ curuser->userlevel = PERM_NORMAL; if (action == PostSend && !(board->brdtype & BRD_NOPOSTNUM)) curuser->numposts++; curuser->lastlogin = request_rec->atime; curuser->lastctype = CTYPE_WEBBBS; xstrncpy(curuser->lasthost, request_rec->fromhost, HOSTLEN); update_passwd(curuser); sethomefile(pathRec, curuser->userid, UFNAME_RECORDS); if ((fp = fopen(pathRec, "a")) != NULL) { fprintf(fp, "%s %s", request_rec->fromhost, ctime(&(request_rec->atime))); fclose(fp); } #if 0 log_visitor(curuser->userid, request_rec->fromhost, request_rec->atime, CTYPE_WEBBBS, FALSE); #endif }
int adduser_main(int argc UNUSED_PARAM, char **argv) { struct passwd pw; const char *usegroup = NULL; char *p; unsigned opts; #if ENABLE_FEATURE_ADDUSER_LONG_OPTIONS applet_long_options = adduser_longopts; #endif /* got root? */ if (geteuid()) { bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); } pw.pw_gecos = (char *)"Linux User,,,"; /* We assume that newly created users "inherit" root's shell setting */ pw.pw_shell = (char *)get_shell_name(); pw.pw_dir = NULL; /* at most two non-option args */ /* disable interactive passwd for system accounts */ opt_complementary = "?2:SD:u+"; if (sizeof(pw.pw_uid) == sizeof(int)) { opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &pw.pw_uid); } else { unsigned uid; opts = getopt32(argv, "h:g:s:G:DSHu:", &pw.pw_dir, &pw.pw_gecos, &pw.pw_shell, &usegroup, &uid); if (opts & OPT_UID) { pw.pw_uid = uid; } } argv += optind; pw.pw_name = argv[0]; if (!opts && argv[1]) { /* if called with two non-option arguments, adduser * will add an existing user to an existing group. */ return addgroup_wrapper(&pw, argv[1]); } /* fill in the passwd struct */ die_if_bad_username(pw.pw_name); if (!pw.pw_dir) { /* create string for $HOME if not specified already */ pw.pw_dir = xasprintf("/home/%s", argv[0]); } pw.pw_passwd = (char *)"x"; if (opts & OPT_SYSTEM_ACCOUNT) { if (!usegroup) { usegroup = "nogroup"; } if (!(opts & OPT_SHELL)) { pw.pw_shell = (char *) "/bin/false"; } } pw.pw_gid = usegroup ? xgroup2gid(usegroup) : -1; /* exits on failure */ /* make sure everything is kosher and setup uid && maybe gid */ passwd_study(&pw); p = xasprintf("x:%u:%u:%s:%s:%s", (unsigned) pw.pw_uid, (unsigned) pw.pw_gid, pw.pw_gecos, pw.pw_dir, pw.pw_shell); if (update_passwd(bb_path_passwd_file, pw.pw_name, p, NULL) < 0) { return EXIT_FAILURE; } if (ENABLE_FEATURE_CLEAN_UP) free(p); #if ENABLE_FEATURE_SHADOWPASSWDS /* /etc/shadow fields: * 1. username * 2. encrypted password * 3. last password change (unix date (unix time/24*60*60)) * 4. minimum days required between password changes * 5. maximum days password is valid * 6. days before password is to expire that user is warned * 7. days after password expires that account is disabled * 8. unix date when login expires (i.e. when it may no longer be used) */ /* fields: 2 3 4 5 6 78 */ p = xasprintf("!:%u:0:99999:7:::", (unsigned)(time(NULL)) / (24*60*60)); /* ignore errors: if file is missing we suppose admin doesn't want it */ update_passwd(bb_path_shadow_file, pw.pw_name, p, NULL); if (ENABLE_FEATURE_CLEAN_UP) free(p); #endif /* add to group */ addgroup_wrapper(&pw, usegroup); /* clear the umask for this process so it doesn't * screw up the permissions on the mkdir and chown. */ umask(0); if (!(opts & OPT_DONT_MAKE_HOME)) { /* set the owner and group so it is owned by the new user, * then fix up the permissions to 2755. Can't do it before * since chown will clear the setgid bit */ int mkdir_err = mkdir(pw.pw_dir, 0755); if (mkdir_err == 0) { /* New home. Copy /etc/skel to it */ const char *args[] = { "chown", "-R", xasprintf("%u:%u", (int)pw.pw_uid, (int)pw.pw_gid), pw.pw_dir, NULL }; /* Be silent on any errors (like: no /etc/skel) */ logmode = LOGMODE_NONE; copy_file("/etc/skel", pw.pw_dir, FILEUTILS_RECUR); logmode = LOGMODE_STDIO; chown_main(4, (char**)args); } if ((mkdir_err != 0 && errno != EEXIST) || chown(pw.pw_dir, pw.pw_uid, pw.pw_gid) != 0 || chmod(pw.pw_dir, 02755) != 0 /* set setgid bit on homedir */ ) { bb_simple_perror_msg(pw.pw_dir); } } if (!(opts & OPT_DONT_SET_PASS)) { /* interactively set passwd */ passwd_wrapper(pw.pw_name); } return EXIT_SUCCESS; }
int main(int argc, char **argv) { struct stat st; int flags; uid_t uid_min = UID_START, uid; char *path = _PATH_AFPDPWFILE; int i, err = 0; extern char *optarg; extern int optind; flags = ((uid = getuid()) == 0) ? OPT_ISROOT : 0; if (((flags & OPT_ISROOT) == 0) && (argc > 1)) { fprintf(stderr, "afppasswd (Netatalk %s)\n", VERSION); fprintf(stderr, "Usage: afppasswd [-acfn] [-u minuid] [-p path] [username]\n"); fprintf(stderr, " -a add a new user\n"); fprintf(stderr, " -c create and initialize password file or specific user\n"); fprintf(stderr, " -f force an action\n"); #ifdef USE_CRACKLIB fprintf(stderr, " -n disable cracklib checking of passwords\n"); #endif /* USE_CRACKLIB */ fprintf(stderr, " -u uid minimum uid to use, defaults to 100\n"); fprintf(stderr, " -p path path to afppasswd file\n"); return -1; } while ((i = getopt(argc, argv, OPTIONS)) != EOF) { switch (i) { case 'c': /* create and initialize password file or specific user */ flags |= OPT_CREATE; break; case 'a': /* add a new user */ flags |= OPT_ADDUSER; break; case 'f': /* force an action */ flags |= OPT_FORCE; break; case 'u': /* minimum uid to use. default is 100 */ uid_min = atoi(optarg); break; #ifdef USE_CRACKLIB case 'n': /* disable CRACKLIB check */ flags |= OPT_NOCRACK; break; #endif /* USE_CRACKLIB */ case 'p': /* path to afppasswd file */ path = optarg; break; default: err++; break; } } if (err || (optind + ((flags & OPT_CREATE) ? 0 : (flags & OPT_ISROOT)) != argc)) { #ifdef USE_CRACKLIB fprintf(stderr, "Usage: afppasswd [-acfn] [-u minuid] [-p path] [username]\n"); #else /* USE_CRACKLIB */ fprintf(stderr, "Usage: afppasswd [-acf] [-u minuid] [-p path] [username]\n"); #endif /* USE_CRACKLIB */ fprintf(stderr, " -a add a new user\n"); fprintf(stderr, " -c create and initialize password file or specific user\n"); fprintf(stderr, " -f force an action\n"); #ifdef USE_CRACKLIB fprintf(stderr, " -n disable cracklib checking of passwords\n"); #endif /* USE_CRACKLIB */ fprintf(stderr, " -u uid minimum uid to use, defaults to 100\n"); fprintf(stderr, " -p path path to afppasswd file\n"); return -1; } i = stat(path, &st); if (flags & OPT_CREATE) { if ((flags & OPT_ISROOT) == 0) { fprintf(stderr, "afppasswd: only root can create the password file.\n"); return -1; } if (!i && ((flags & OPT_FORCE) == 0)) { fprintf(stderr, "afppasswd: password file already exists.\n"); return -1; } return create_file(path, uid_min); } else { struct passwd *pwd = NULL; if (i < 0) { fprintf(stderr, "afppasswd: %s doesn't exist.\n", path); return -1; } /* if we're root, we need to specify the username */ pwd = (flags & OPT_ISROOT) ? getpwnam(argv[optind]) : getpwuid(uid); if (pwd) return update_passwd(path, pwd->pw_name, flags); fprintf(stderr, "afppasswd: can't get password entry.\n"); return -1; } }
int main(int argc, char *argv[]) { int ssock; char data_buf[BUFLEN]; char name[NAMELEN]; char pw[PASSLEN]; char curr_user[NAMELEN]; char *good = "Welcome to The Machine!\n"; char *evil = "Invalid identity, exiting!\n"; unsigned int send_mtype; char buffer[20] = "192.168.1.1"; struct sockaddr_in servaddr, cliaddr; socklen_t len = sizeof(servaddr); char servip[20]; int csock; /*create a socket*/ if ((ssock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("cannot create socket"); exit(1); } int optval = 1; setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval); // --- clear out memory and assign IP parameters --- // memset((char *) &servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = inet_addr(buffer); servaddr.sin_port = htons(PORTNUM); inet_ntop(AF_INET, &servaddr.sin_addr, servip, 20); printf("Starting server on: %s %d. \n", servip, ntohs(servaddr.sin_port)); fflush(stdout); // --- bind socket --- // if (bind(ssock, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0) { perror("Error Binding"); close(ssock); exit(5); } int err = getsockname(ssock, (struct sockaddr *) &servaddr, &len); if( listen(ssock, 5) < 0 ) { /* listen for a connection */ printf("listen() failed\n"); close(ssock); exit(5); } int namelen = sizeof(cliaddr); char cliip[20]; while(1) { printf("waiting\n"); /* Accept connection from client */ if((csock = accept(ssock, (struct sockaddr *) &cliaddr, &namelen)) < 0) { printf("accept() failed to accept client connection request.\n"); close(ssock); exit(6); } printf("Connection accepted"); inet_ntop(AF_INET, &cliaddr.sin_addr, cliip, 20); printf("Receiving message from: %s %d.", cliip, ntohs(cliaddr.sin_port)); int rb; do { /* Wait for client to initiate communication */ rb = recv(csock, data_buf, sizeof(data_buf), 0); /* wait for a client message to arrive */ if(rb < 0) { printf("recv() did not get client data\n"); close(csock); close(ssock); exit(7); } printf("recvd"); app_packet *read_packet; read_packet = (app_packet *)data_buf; printf("%d\n", read_packet->control_seq); switch(read_packet->control_seq) { case 100: /* Case of initial authentication */ sscanf(read_packet->payload, "%16[^:]:%s", name,pw); if( match(name,pw) == 0 ) { welcome(good); strcpy(curr_user, name); send_mtype = 200; //telling the user he is good. } else { goodbye(evil); send_mtype = 204; // telling the user is out. } break; case 101: /* Case to upate password of user */ update_passwd(curr_user, read_packet->payload); send_mtype = 201; break; case 102: /* Case of adding new user */ sscanf(read_packet->payload, "%16[^:]:%s", name,pw); add_user(name, pw); send_mtype = 202; break; case 103: /* Unused case for adding more features */ break; case 104: /* Unused case for adding more features */ break; default: printf("stuff happened\n"); } /* Create the response packet */ char raw_packet[BUFLEN]; app_packet *packet = (app_packet *)raw_packet; memset(raw_packet, 0, BUFLEN); packet->control_seq = send_mtype; /* Send the response to client */ if(send(csock, packet, sizeof(app_packet), 0) < 0) { /* echo the client message back to the client */ printf("send() failed to send data back to client.\n"); close(csock); close(ssock); exit(8); } } while(rb != 0); /* Go back to listening state for new messages from client */ } close(csock); close(ssock); }
int cmd_user(sqlite3 *db,char **errmsg,vpChat temp,int sockfd)//用语判别用户信息 { int flag; int sayflag; printf("cmd = %d\n",temp->cmd); switch(temp->cmd ) { case REG: { flag = read_user(db,errmsg,temp->name); if(flag == USERIN) { return REGNO;//注册重名 } else { reg_db(db,errmsg,temp->name,temp->passwd); temp->flag = read_id(db,errmsg,temp->name); return REGOK; } break; } case LOG: { int flagpasswd; flag = read_online_ok(db,errmsg,temp->name); if(flag == ONLINEIN) { return ONLINEIN; } else { flagpasswd = read_pass(db,errmsg,temp->name,temp->passwd); if(flagpasswd == PASSWDOK) { log_db(db,errmsg,temp->name,sockfd); return PASSWDOK; } else { return PASSWDNO; } } break; } case LOGID: { int flagpasswd; read_id_name(db,errmsg,temp);//询问有无此用户 flag = read_online_ok(db,errmsg,temp->name); if(flag == ONLINEIN) { return ONLINEIN; } else { flagpasswd = read_pass(db,errmsg,temp->name,temp->passwd); if(flagpasswd == PASSWDOK) { log_db(db,errmsg,temp->name,sockfd); return PASSWDOK; } else { return PASSWDNO; } } break; } case CHAT: { int tempfd; tempfd = read_online_fd(db,errmsg,temp->toname); if(tempfd == ONLINEOUT) { return ONLINEOUT; } else { sayflag = read_online_flag(db,errmsg,temp->name); if(sayflag == 0) { return MYFLAGNO; } else { temp->flag = flag; temp->sockfd = tempfd; insert_data_db(db,errmsg,temp->time,temp); return CHATOK; } } break; } case ALL: { sayflag = read_online_flag(db,errmsg,temp->name); if(sayflag == 0) { return MYFLAGNO; } else { insert_data_db(db,errmsg,temp->time,temp); return ALLOK; } break; } case SMILE: { sayflag = read_online_flag(db,errmsg,temp->name); if(sayflag == 0) { return MYFLAGNO; } else { insert_data_db(db,errmsg,temp->time,temp); return SMILEOK; } break; } case WELCOME: { sayflag = read_online_flag(db,errmsg,temp->name); if(sayflag == 0) { return MYFLAGNO; } else { insert_data_db(db,errmsg,temp->time,temp); return WELCOMEOK; } break; } case PASSWD: { flag = update_passwd(db,errmsg,temp->name,temp->passwd); return flag; break; } case BOOT: { int tempfd; tempfd = read_online_fd(db,errmsg,temp->toname); if(tempfd == ONLINEOUT) { return ONLINEOUT; } else { temp->sockfd = tempfd; return BOOTOK; } break; } case STEP: { int tempfd; tempfd = read_online_fd(db,errmsg,temp->toname); if(tempfd == ONLINEOUT) { return ONLINEOUT; } else { flag = update_flag(db,errmsg,temp->toname,0); if(flag == 1) { temp->sockfd = tempfd; } return flag; } break; } case BAN: { int tempfd; tempfd = read_online_fd(db,errmsg,temp->toname); if(tempfd == ONLINEOUT) { return ONLINEOUT; } else { sayflag = read_online_flag(db,errmsg,temp->toname); if(sayflag == 1) { return TOFLAGOK; } else { flag = update_flag(db,errmsg,temp->toname,1); if(flag == 1) { temp->sockfd = tempfd; } return flag; } } break; } case SET: { int tempfd; tempfd = read_online_fd(db,errmsg,temp->toname); if(tempfd == ONLINEOUT) { flag = delete_user(db,errmsg,temp->toname); return flag; } else { return ONLINEIN; } break; } case SEE: { return SEEOK; break; } case DATA: { return DATAOK; break; } case SEND: { int tempfd; tempfd = read_online_fd(db,errmsg,temp->toname); if(tempfd == ONLINEOUT) { return ONLINEOUT; } else { temp->sockfd = tempfd; return SENDOK; } break; } case CHANGE: { flag = read_user(db,errmsg,temp->toname); if(flag == USERIN) { return REGNO;//注册重名 } else { flag = update_user(db,errmsg,temp->name,temp->toname);//¿¿¿¿ if(flag == 1) { sayflag = update_db_data(db,errmsg,temp->name,temp->toname);//修改密码 return sayflag; } else { return 0; } } break; } } }
/* * add_passwd - add or update the encrypted password */ static int add_passwd (struct passwd *pwd, const char *password) { const struct spwd *sp; struct spwd spent; char *cp; #ifndef USE_PAM void *crypt_arg = NULL; if (crypt_method != NULL) { #ifdef USE_SHA_CRYPT if (sflg) { crypt_arg = &sha_rounds; } #endif /* USE_SHA_CRYPT */ } /* * In the case of regular password files, this is real easy - pwd * points to the entry in the password file. Shadow files are * harder since there are zillions of things to do ... */ if (!is_shadow) { return update_passwd (pwd, password); } #endif /* USE_PAM */ /* * Do the first and easiest shadow file case. The user already * exists in the shadow password file. */ sp = spw_locate (pwd->pw_name); #ifndef USE_PAM if (NULL != sp) { spent = *sp; if ( (NULL != crypt_method) && (0 == strcmp(crypt_method, "NONE"))) { spent.sp_pwdp = (char *)password; } else { const char *salt = crypt_make_salt (crypt_method, crypt_arg); cp = pw_encrypt (password, salt); if (NULL == cp) { fprintf (stderr, _("%s: failed to crypt password with salt '%s': %s\n"), Prog, salt, strerror (errno)); return 1; } spent.sp_pwdp = cp; } spent.sp_lstchg = (long) gettime () / SCALE; if (0 == spent.sp_lstchg) { /* Better disable aging than requiring a password * change */ spent.sp_lstchg = -1; } return (spw_update (&spent) == 0); } /* * Pick the next easiest case - the user has an encrypted password * which isn't equal to "x". The password was set to "x" earlier * when the entry was created, so this user would have to have had * the password set someplace else. */ if (strcmp (pwd->pw_passwd, "x") != 0) { return update_passwd (pwd, password); } #else /* USE_PAM */ /* * If there is already a shadow entry, do not touch it. * If there is already a passwd entry with a password, do not * touch it. * The password will be updated later for all users using PAM. */ if ( (NULL != sp) || (strcmp (pwd->pw_passwd, "x") != 0)) { return 0; } #endif /* USE_PAM */ /* * Now the really hard case - I need to create an entirely new * shadow password file entry. */ spent.sp_namp = pwd->pw_name; #ifndef USE_PAM if ((crypt_method != NULL) && (0 == strcmp(crypt_method, "NONE"))) { spent.sp_pwdp = (char *)password; } else { const char *salt = crypt_make_salt (crypt_method, crypt_arg); cp = pw_encrypt (password, salt); if (NULL == cp) { fprintf (stderr, _("%s: failed to crypt password with salt '%s': %s\n"), Prog, salt, strerror (errno)); return 1; } spent.sp_pwdp = cp; } #else /* * Lock the password. * The password will be updated later for all users using PAM. */ spent.sp_pwdp = "!"; #endif spent.sp_lstchg = (long) gettime () / SCALE; if (0 == spent.sp_lstchg) { /* Better disable aging than requiring a password change */ spent.sp_lstchg = -1; } spent.sp_min = getdef_num ("PASS_MIN_DAYS", 0); /* 10000 is infinity this week */ spent.sp_max = getdef_num ("PASS_MAX_DAYS", 10000); spent.sp_warn = getdef_num ("PASS_WARN_AGE", -1); spent.sp_inact = -1; spent.sp_expire = -1; spent.sp_flag = SHADOW_SP_FLAG_UNSET; return (spw_update (&spent) == 0); }
int chpasswd_main(int argc UNUSED_PARAM, char **argv) { char *name; const char *algo = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO; int opt; if (getuid() != 0) bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); opt = getopt32long(argv, "^" "emc:" "\0" "m--ec:e--mc:c--em", chpasswd_longopts, &algo ); while ((name = xmalloc_fgetline(stdin)) != NULL) { char *free_me; char *pass; int rc; pass = strchr(name, ':'); if (!pass) bb_error_msg_and_die("missing new password"); *pass++ = '\0'; xuname2uid(name); /* dies if there is no such user */ free_me = NULL; if (!(opt & OPT_ENC)) { char salt[MAX_PW_SALT_LEN]; if (opt & OPT_MD5) { /* Force MD5 if the -m flag is set */ algo = "md5"; } crypt_make_pw_salt(salt, algo); free_me = pass = pw_encrypt(pass, salt, 0); } /* This is rather complex: if user is not found in /etc/shadow, * we try to find & change his passwd in /etc/passwd */ #if ENABLE_FEATURE_SHADOWPASSWDS rc = update_passwd(bb_path_shadow_file, name, pass, NULL); if (rc > 0) /* password in /etc/shadow was updated */ pass = (char*)"x"; if (rc >= 0) /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */ #endif rc = update_passwd(bb_path_passwd_file, name, pass, NULL); /* LOGMODE_BOTH logs to syslog also */ logmode = LOGMODE_BOTH; if (rc < 0) bb_error_msg_and_die("an error occurred updating password for %s", name); if (rc) bb_error_msg("password for '%s' changed", name); logmode = LOGMODE_STDIO; free(name); free(free_me); } return EXIT_SUCCESS; }
int passwd_main(int argc UNUSED_PARAM, char **argv) { enum { OPT_algo = (1 << 0), /* -a - password algorithm */ OPT_lock = (1 << 1), /* -l - lock account */ OPT_unlock = (1 << 2), /* -u - unlock account */ OPT_delete = (1 << 3), /* -d - delete password */ OPT_lud = OPT_lock | OPT_unlock | OPT_delete, }; unsigned opt; int rc; const char *opt_a = CONFIG_FEATURE_DEFAULT_PASSWD_ALGO; const char *filename; char *myname; char *name; char *newp; struct passwd *pw; uid_t myuid; struct rlimit rlimit_fsize; char c; #if ENABLE_FEATURE_SHADOWPASSWDS /* Using _r function to avoid pulling in static buffers */ struct spwd spw; char buffer[256]; #endif logmode = LOGMODE_BOTH; openlog(applet_name, 0, LOG_AUTH); opt = getopt32(argv, "a:lud", &opt_a); //argc -= optind; argv += optind; myuid = getuid(); /* -l, -u, -d require root priv and username argument */ if ((opt & OPT_lud) && (myuid != 0 || !argv[0])) bb_show_usage(); /* Will complain and die if username not found */ myname = xstrdup(xuid2uname(myuid)); name = argv[0] ? argv[0] : myname; pw = xgetpwnam(name); if (myuid != 0 && pw->pw_uid != myuid) { /* LOGMODE_BOTH */ bb_error_msg_and_die("%s can't change password for %s", myname, name); } #if ENABLE_FEATURE_SHADOWPASSWDS { /* getspnam_r may return 0 yet set result to NULL. * At least glibc 2.4 does this. Be extra paranoid here. */ struct spwd *result = NULL; errno = 0; if (getspnam_r(pw->pw_name, &spw, buffer, sizeof(buffer), &result) != 0 || !result /* no error, but no record found either */ || strcmp(result->sp_namp, pw->pw_name) != 0 /* paranoia */ ) { if (errno != ENOENT) { /* LOGMODE_BOTH */ bb_perror_msg("no record of %s in %s, using %s", name, bb_path_shadow_file, bb_path_passwd_file); } /* else: /etc/shadow does not exist, * apparently we are on a shadow-less system, * no surprise there */ } else { pw->pw_passwd = result->sp_pwdp; } } #endif /* Decide what the new password will be */ newp = NULL; c = pw->pw_passwd[0] - '!'; if (!(opt & OPT_lud)) { if (myuid != 0 && !c) { /* passwd starts with '!' */ /* LOGMODE_BOTH */ bb_error_msg_and_die("can't change " "locked password for %s", name); } printf("Changing password for %s\n", name); newp = new_password(pw, myuid, opt_a); if (!newp) { logmode = LOGMODE_STDIO; bb_error_msg_and_die("password for %s is unchanged", name); } } else if (opt & OPT_lock) { if (!c) goto skip; /* passwd starts with '!' */ newp = xasprintf("!%s", pw->pw_passwd); } else if (opt & OPT_unlock) { if (c) goto skip; /* not '!' */ /* pw->pw_passwd points to static storage, * strdup'ing to avoid nasty surprizes */ newp = xstrdup(&pw->pw_passwd[1]); } else if (opt & OPT_delete) { newp = (char*)""; } rlimit_fsize.rlim_cur = rlimit_fsize.rlim_max = 512L * 30000; setrlimit(RLIMIT_FSIZE, &rlimit_fsize); bb_signals(0 + (1 << SIGHUP) + (1 << SIGINT) + (1 << SIGQUIT) , SIG_IGN); umask(077); xsetuid(0); #if ENABLE_FEATURE_SHADOWPASSWDS filename = bb_path_shadow_file; rc = update_passwd(bb_path_shadow_file, name, newp, NULL); if (rc > 0) /* password in /etc/shadow was updated */ newp = (char*) "x"; if (rc >= 0) /* 0 = /etc/shadow missing (not an error), >0 = passwd changed in /etc/shadow */ #endif { filename = bb_path_passwd_file; rc = update_passwd(bb_path_passwd_file, name, newp, NULL); } /* LOGMODE_BOTH */ if (rc < 0) bb_error_msg_and_die("can't update password file %s", filename); bb_error_msg("password for %s changed by %s", name, myname); /*if (ENABLE_FEATURE_CLEAN_UP) free(newp); - can't, it may be non-malloced */ skip: if (!newp) { bb_error_msg_and_die("password for %s is already %slocked", name, (opt & OPT_unlock) ? "un" : ""); } if (ENABLE_FEATURE_CLEAN_UP) free(myname); return 0; }
int main(int argc, char *argv[]) { char data_buf[512]; char name[NAMELEN]; char pw[PASSLEN]; char *good = "Welcome to The Machine!\n"; char *evil = "Invalid identity, exiting!\n"; u_char send_mtype; /* // make sure the database file exists /*create_database();*/ //------------------------------------------------------------- /* /*Find the default address used for communication, this address needs to be used by the client*/ // --- find out default IP used for communication ---- // //--------------------------------------------------------------- char buffer[20] = "192.168.42.249"; struct sockaddr_in servaddr, cliaddr; socklen_t len = sizeof(servaddr); char servip[20]; int csock; /*create a socket*/ ssock = socket(AF_INET, SOCK_STREAM, 0); // --- clear out memory and assign IP parameters --- // memset((char *) &servaddr, 0, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = inet_addr(buffer); servaddr.sin_port = htons(10551); inet_ntop(AF_INET, &servaddr.sin_addr, servip, 20); printf("Starting server on: %s %d. \n", servip, ntohs(servaddr.sin_port)); fflush(stdout); // --- bind socket --- // if (bind(ssock, (struct sockaddr *) &servaddr, sizeof(servaddr))) { perror("Error Binding"); } len = sizeof(servaddr); int err = getsockname(ssock, (struct sockaddr *) &servaddr, &len); if( listen(ssock, 1) != 0 ) { /* listen for a connection */ fprintf(stderr, "listen() failed\n"); exit(5); } // RECEIVING STUFF int namelen = sizeof(cliaddr); /* accept connection request */ char cliip[20]; if( (csock = accept(ssock, (struct sockaddr *) &cliaddr, &namelen)) == -1) { fprintf(stderr, "accept() failed to accept client connection request.\n"); exit(6); } inet_ntop(AF_INET, &cliaddr.sin_addr, cliip, 20); printf("Receiving message from: %s %d.", cliip, ntohs(cliaddr.sin_port)); fflush(stdout); while(1) { // PUT RECEIVED DATA INTO data_buf if( recv(csock, data_buf, sizeof(data_buf), 0) == -1 ) { /* wait for a client message to arrive */ fprintf(stderr, "recv() did not get client data\n"); exit(7); } // PROCESS DATA_BUF and decide what to do in next message struct app_packet *read_packet; read_packet = (struct app_packet *)data_buf; printf("%s\n", read_packet); printf("%d\n", read_packet->control_seq); switch(read_packet->control_seq) { case 100: sscanf(read_packet->payload, "%16[^:]:%s", name,pw); /* printf("%s\n", name); printf("%s\n", pw); */ if( match(name,pw) == 0 ) { welcome(good); strcpy(curr_user, name); send_mtype = 200; //telling the user he is good. } else if (match(name,pw) == 1) { goodbye(evil); send_mtype = 204; // telling the user is out. } break; case 101: update_passwd(curr_user, read_packet->payload); /*printf("%s\n", curr_user);*/ send_mtype = 201; break; case 102: sscanf(read_packet->payload, "%16[^:]:%s", name,pw); /* printf("%s\n", name); printf("%s\n", pw);*/ add_user(name, pw); send_mtype = 202; break; case 103: break; case 104: break; default: printf("stuff happened\n"); } /*printf("%s\n", read_packet->payload);*/ // see the sscanf example here /* char username[NAMELEN]; char password[PASSLEN]; sscanf(read_packet->payload, "%16[^:]:%s", username, password); printf("%s\n", username); printf("%s\n", password); */ // -- Send the message back to the client char raw_packet[BUFLEN]; struct app_packet *packet = (struct app_packet *)raw_packet; memset(raw_packet, 0, BUFLEN); packet->control_seq = send_mtype; //make a payload with ssprintf // make payload when printing command outputs, else no payload ;) if (packet->control_seq == 106) { sprintf(packet->payload, "%s:%s", argv[2], argv[3]); } /*strcpy(packet->payload, "Hey!"); */ // SEND DATA BACK TO CLIENT if( send( csock, packet, sizeof(struct app_packet), 0) < 0) { /* echo the client message back to the client */ fprintf(stderr, "send() failed to send data back to client.\n"); exit(8); } printf("message sent!\n"); } close(csock); close(ssock); }
int deluser_main(int argc, char **argv) { /* User or group name */ char *name; /* Username (non-NULL only in "delgroup USER GROUP" case) */ char *member; /* Name of passwd or group file */ const char *pfile; /* Name of shadow or gshadow file */ const char *sfile; /* Are we deluser or delgroup? */ int do_deluser = (ENABLE_DELUSER && (!ENABLE_DELGROUP || applet_name[3] == 'u')); if (geteuid() != 0) bb_error_msg_and_die(bb_msg_perm_denied_are_you_root); name = argv[1]; member = NULL; switch (argc) { case 3: if (!ENABLE_FEATURE_DEL_USER_FROM_GROUP || do_deluser) break; /* It's "delgroup USER GROUP" */ member = name; name = argv[2]; /* Fallthrough */ case 2: if (do_deluser) { /* "deluser USER" */ xgetpwnam(name); /* bail out if USER is wrong */ pfile = bb_path_passwd_file; if (ENABLE_FEATURE_SHADOWPASSWDS) sfile = bb_path_shadow_file; } else { struct group *gr; do_delgroup: /* "delgroup GROUP" or "delgroup USER GROUP" */ if (do_deluser < 0) { /* delgroup after deluser? */ gr = getgrnam(name); if (!gr) return EXIT_SUCCESS; } else { gr = xgetgrnam(name); /* bail out if GROUP is wrong */ } if (!member) { /* "delgroup GROUP" */ struct passwd *pw; struct passwd pwent; /* Check if the group is in use */ #define passwd_buf bb_common_bufsiz1 while (!getpwent_r(&pwent, passwd_buf, sizeof(passwd_buf), &pw)) { if (pwent.pw_gid == gr->gr_gid) bb_error_msg_and_die("'%s' still has '%s' as their primary group!", pwent.pw_name, name); } //endpwent(); } pfile = bb_path_group_file; if (ENABLE_FEATURE_SHADOWPASSWDS) sfile = bb_path_gshadow_file; } /* Modify pfile, then sfile */ do { if (update_passwd(pfile, name, NULL, member) == -1) return EXIT_FAILURE; if (ENABLE_FEATURE_SHADOWPASSWDS) { pfile = sfile; sfile = NULL; } } while (ENABLE_FEATURE_SHADOWPASSWDS && pfile); if (ENABLE_DELGROUP && do_deluser > 0) { /* "deluser USER" also should try to delete * same-named group. IOW: do "delgroup USER" */ // On debian deluser is a perl script that calls userdel. // From man userdel: // If USERGROUPS_ENAB is defined to yes in /etc/login.defs, userdel will // delete the group with the same name as the user. do_deluser = -1; goto do_delgroup; } return EXIT_SUCCESS; } /* Reached only if number of command line args is wrong */ bb_show_usage(); }