int main(int argc, char *argv[]) { const char *masks[] = { "255.255.255.255", "255.255.255.0", "255.255.0.0", "255.0.0.0" }; struct in_addr addr; int i, len; for (i = 0; i < 4; i++) { printf("subnet %-16s prefixlen %u\n", masks[i], netmask2prefixlen(masks[i])); } printf("\n"); for (i = 0; i < 4; i++) { len = (i + 1) * 8; addr.s_addr = htonl(~(0xfffffffflu >> len)); printf("prefixlen %-2u netmask %s\n", len, inet_ntoa(addr)); } return 0; }
/** Reload the account list */ static int npppd_auth_reload_acctlist(npppd_auth_base *base) { CSVREADER_STATUS status; int linno, ncols, usersz, nuser, eof, off; const char **cols, *passwd, *callnum; char line[8192]; csvreader *csv; npppd_auth_user *user; struct in_addr ip4, ip4mask; slist users; FILE *file; struct stat st; if (base->acctlist_ready != 0 && lstat(base->acctlist_path, &st) == 0) { if (st.st_mtime == base->last_load) return 0; base->last_load = st.st_mtime; } slist_init(&users); csv = NULL; if ((file = priv_fopen(base->acctlist_path)) == NULL) { /* hash is empty if file is not found. */ if (errno == ENOENT) hash_delete_all(base->users_hash, 1); npppd_auth_base_log(base, (errno == ENOENT)? LOG_DEBUG : LOG_ERR, "Open %s failed: %m", base->acctlist_path); return 0; } if ((csv = csvreader_create()) == NULL) { npppd_auth_base_log(base, LOG_ERR, "Loading a account list failed: csvreader_create(): %m"); goto fail; } for (linno = 0, eof = 0; !eof;) { ip4.s_addr = 0; ip4mask.s_addr = 0xffffffffL; if (fgets(line, sizeof(line), file) != NULL) { int linelen; linelen = strlen(line); if (linelen <= 0) { npppd_auth_base_log(base, LOG_ERR, "Loading a account list failed: lineno=%d " "line too short", linno + 1); goto fail; } if (line[linelen - 1] != '\n' && !feof(file)) { npppd_auth_base_log(base, LOG_ERR, "Loading a account list failed: lineno=%d " "line too long", linno + 1); goto fail; } status = csvreader_parse(csv, line); } else { if (!feof(file)) { npppd_auth_base_log(base, LOG_ERR, "Loading a account list failed: %m"); goto fail; } status = csvreader_parse_flush(csv); eof = 1; } if (status != CSVREADER_NO_ERROR) { if (status == CSVREADER_OUT_OF_MEMORY) npppd_auth_base_log(base, LOG_ERR, "Loading a account list failed: %m"); else npppd_auth_base_log(base, LOG_ERR, "Loading a account list " "failed: lineno=%d parse error", linno); goto fail; } ncols = csvreader_get_number_of_column(csv); if ((cols = csvreader_get_column(csv)) == NULL) continue; linno++; /* count up here because line number is treated as CSV. */ if (linno == 1) { /* skip a title line */ continue; } if (ncols < 1) { npppd_auth_base_log(base, LOG_ERR, "account list lineno=%d has only %d fields.", linno, ncols); continue; } if (strlen(cols[0]) <= 0) continue; /* skip if the user-name is empty */ if (ncols >= 3) { if (*cols[2] != '\0' && inet_aton(cols[2], &ip4) != 1) { npppd_auth_base_log(base, LOG_ERR, "account list lineno=%d parse error: " "invalid 'Framed-IP-Address' field: %s", linno, cols[2]); continue; } } if (ncols >= 4) { if ((*cols[3] != '\0' && inet_aton(cols[3], &ip4mask) != 1) || netmask2prefixlen(htonl(ip4mask.s_addr)) < 0) { npppd_auth_base_log(base, LOG_ERR, "account list lineno=%d parse error: " "invalid 'Framed-IP-Netmask' field: %s", linno, cols[3]); continue; } } passwd = ""; if (cols[1] != NULL) passwd = cols[1]; callnum = ""; if (ncols >= 6 && cols[5] != NULL) callnum = cols[5]; usersz = sizeof(npppd_auth_user); usersz += strlen(cols[0]) + 1; usersz += strlen(passwd) + 1; usersz += strlen(callnum) + 1; if ((user = malloc(usersz)) == NULL) { npppd_auth_base_log(base, LOG_ERR, "Loading a account list failed: %m"); goto fail; } memset(user, 0, usersz); off = 0; user->username = user->space + off; off += strlcpy(user->username, cols[0], usersz - off); ++off; user->password = user->space + off; off += strlcpy(user->password, passwd, usersz - off); ++off; user->calling_number = user->space + off; strlcpy(user->calling_number, callnum, usersz - off); user->framed_ip_address = ip4; user->framed_ip_netmask = ip4mask; slist_add(&users, user); } hash_delete_all(base->users_hash, 1); nuser = 0; for (slist_itr_first(&users); slist_itr_has_next(&users);) { user = slist_itr_next(&users); if (hash_lookup(base->users_hash, user->username) != NULL) { npppd_auth_base_log(base, LOG_WARNING, "Record for user='******' is redefined, the first " "record will be used.", user->username); free(user); goto next_user; } if (hash_insert(base->users_hash, user->username, user) != 0) { npppd_auth_base_log(base, LOG_ERR, "Loading a account list failed: hash_insert(): %m"); goto fail; } nuser++; next_user: slist_itr_remove(&users); } slist_fini(&users); csvreader_destroy(csv); fclose(file); npppd_auth_base_log(base, LOG_INFO, "Loaded users from='%s' successfully. %d users", base->acctlist_path, nuser); base->acctlist_ready = 1; return 0; fail: fclose(file); if (csv != NULL) csvreader_destroy(csv); hash_delete_all(base->users_hash, 1); for (slist_itr_first(&users); slist_itr_has_next(&users);) { user = slist_itr_next(&users); free(user); } slist_fini(&users); return 1; }