int commonio_remove(struct commonio_db *db, const char *name) { struct commonio_entry *p; if (!db->isopen || db->readonly) { errno = EINVAL; return 0; } p = find_entry_by_name(db, name); if (!p) { errno = ENOENT; return 0; } commonio_del_entry(db, p); if (p->line) free(p->line); if (p->eptr) db->ops->free(p->eptr); return 1; }
/* * Sort entries in db according to order in another. */ int commonio_sort_wrt(struct commonio_db *shadow, struct commonio_db *passwd) { struct commonio_entry *head = NULL, *pw_ptr, *spw_ptr; const char *name; for (pw_ptr = passwd->head; pw_ptr; pw_ptr = pw_ptr->next) { if (pw_ptr->eptr == NULL) continue; name = passwd->ops->getname(pw_ptr->eptr); for (spw_ptr = shadow->head; spw_ptr; spw_ptr = spw_ptr->next) if (strcmp(name, shadow->ops->getname(spw_ptr->eptr)) == 0) break; if (spw_ptr == NULL) continue; commonio_del_entry(shadow, spw_ptr); spw_ptr->next = head; head = spw_ptr; } for (spw_ptr = head; spw_ptr; spw_ptr = head) { head = head->next; if (shadow->head) shadow->head->prev = spw_ptr; spw_ptr->next = shadow->head; shadow->head = spw_ptr; } shadow->head->prev = NULL; shadow->changed = 1; return 0; }
void __spw_del_entry(const struct commonio_entry *ent) { commonio_del_entry(&shadow_db, ent); }
/* * This file was originally distributed as part of * shadow-utils-4.0.17-12.fc6.src.rpm and has been modified * in WRLinux for inclusion in rpm. */ #include "system.h" #include "shadow_config.h" #ident "$Id: groupio.c,v 1.4 2010/03/21 14:56:59 jbj Exp $" #include "prototypes.h" #include "defines.h" #include "commonio.h" #include "groupio.h" extern int putgrent (const struct group *, FILE *); extern struct group *sgetgrent (const char *); struct group *__gr_dup (const struct group *grent) { struct group *gr; int i; if (!(gr = (struct group *) malloc (sizeof *gr))) return NULL; *gr = *grent; if (!(gr->gr_name = strdup (grent->gr_name))) return NULL; if (!(gr->gr_passwd = strdup (grent->gr_passwd))) return NULL; for (i = 0; grent->gr_mem[i]; i++); gr->gr_mem = (char **) malloc ((i + 1) * sizeof (char *)); if (!gr->gr_mem) return NULL; for (i = 0; grent->gr_mem[i]; i++) { gr->gr_mem[i] = strdup (grent->gr_mem[i]); if (!gr->gr_mem[i]) return NULL; } gr->gr_mem[i] = NULL; return gr; } static void *group_dup (const void *ent) { const struct group *gr = (const struct group *) ent; return __gr_dup (gr); } static void group_free (void *ent) { struct group *gr = (struct group *) ent; free (gr->gr_name); free (gr->gr_passwd); while (*(gr->gr_mem)) { free (*(gr->gr_mem)); gr->gr_mem++; } free (gr); } static const char *group_getname (const void *ent) { const struct group *gr = (const struct group *) ent; return gr->gr_name; } static void *group_parse (const char *line) { return (void *) sgetgrent (line); } static int group_put (const void *ent, FILE * file) { const struct group *gr = (const struct group *) ent; return (putgrent (gr, file) == -1) ? -1 : 0; } static struct commonio_ops group_ops = { group_dup, group_free, group_getname, group_parse, group_put, fgetsx, fputsx }; static struct commonio_db group_db = { GROUP_FILE, /* filename */ &group_ops, /* ops */ NULL, /* fp */ NULL, /* head */ NULL, /* tail */ NULL, /* cursor */ 0, /* changed */ 0, /* isopen */ 0, /* locked */ 0 /* readonly */ }; int gr_name (const char *filename) { return commonio_setname (&group_db, filename); } int gr_lock (void) { return commonio_lock (&group_db); } int gr_open (int mode) { return commonio_open (&group_db, mode); } const struct group *gr_locate (const char *name) { return (const struct group *) commonio_locate (&group_db, name); } int gr_update (const struct group *gr) { return commonio_update (&group_db, (const void *) gr); } int gr_remove (const char *name) { return commonio_remove (&group_db, name); } int gr_rewind (void) { return commonio_rewind (&group_db); } const struct group *gr_next (void) { return (const struct group *) commonio_next (&group_db); } int gr_close (void) { return commonio_close (&group_db); } int gr_unlock (void) { return commonio_unlock (&group_db); } void __gr_set_changed (void) { group_db.changed = 1; } struct commonio_entry *__gr_get_head (void); /* XXX gcc warning. */ struct commonio_entry *__gr_get_head (void) { return group_db.head; } struct commonio_db *__gr_get_db (void); /* XXX gcc warning. */ struct commonio_db *__gr_get_db (void) { return &group_db; } void __gr_del_entry (const struct commonio_entry *ent); /* XXX gcc warning. */ void __gr_del_entry (const struct commonio_entry *ent) { commonio_del_entry (&group_db, ent); }
/* * remove_range: remove a range of subuids from an owning uid's list * of authorized subuids. * @db: database to work on * @owner: owning uid whose range is being removed * @start: start of the range to be removed * @count: number of uids in the range. * * Returns 0 on failure, 1 on success. Failure means that we needed to * create a new range to represent the new limits, and failed doing so. */ static int remove_range (struct commonio_db *db, const char *owner, unsigned long start, unsigned long count) { struct commonio_entry *ent; unsigned long end; if (count == 0) { return 1; } end = start + count - 1; for (ent = db->head; NULL != ent; ent = ent->next) { struct subordinate_range *range = ent->eptr; unsigned long first; unsigned long last; /* Skip unparsed entries */ if (NULL == range) { continue; } first = range->start; last = first + range->count - 1; /* Skip entries with a different owner */ if (0 != strcmp (range->owner, owner)) { continue; } /* Skip entries outside of the range to remove */ if ((end < first) || (start > last)) { continue; } if (start <= first) { if (end >= last) { /* to be removed: [start, end] * range: [first, last] */ /* entry completely contained in the * range to remove */ commonio_del_entry (db, ent); } else { /* to be removed: [start, end] * range: [first, last] */ /* Remove only the start of the entry */ range->start = end + 1; range->count = (last - range->start) + 1; ent->changed = true; db->changed = true; } } else { if (end >= last) { /* to be removed: [start, end] * range: [first, last] */ /* Remove only the end of the entry */ range->count = start - range->start; ent->changed = true; db->changed = true; } else { /* to be removed: [start, end] * range: [first, last] */ /* Remove the middle of the range * This requires to create a new range */ struct subordinate_range tail; tail.owner = range->owner; tail.start = end + 1; tail.count = (last - tail.start) + 1; if (commonio_append (db, &tail) == 0) { return 0; } range->count = start - range->start; ent->changed = true; db->changed = true; } } } return 1; }
void __pw_del_entry (const struct commonio_entry *ent) { commonio_del_entry (&passwd_db, ent); }