#include <string.h>
#include <sys/types.h>
#include <errno.h>
+#include <pwd.h>
#include "heap.h"
#include "passwd.h"
-#ifndef NOT_PASSWD_ONLY
-
-#include "buffer.h"
-
-/* index of fields */
-enum { iname, ipasswd, iuid, igid, icmt, idir, ishell };
-
-static const char pwfile[] = "/etc/passwd";
-
-#define is(s, i) (!strncmp(s, context->pw.starts[i], context->pw.lengths[i]) && !s[context->pw.lengths[i]])
-
-/* open the passwd file */
-static int oppw(struct tzplatform_context *context)
-{
- context->pw.pos = 0;
- return buffer_create(&context->pw.buffer, pwfile);
-}
-
-/* close the passwd file */
-static void clpw(struct tzplatform_context *context)
-{
- buffer_destroy(&context->pw.buffer);
-}
-
-/* read the passwd file */
-static int rdpw(struct tzplatform_context *context)
-{
- int col = 0;
- struct pw *pw = &context->pw;
- const char *head = pw->buffer.buffer + pw->pos;
- const char *end = pw->buffer.buffer + pw->buffer.length;
- while (head != end) {
- pw->starts[col] = head;
- while (head != end && *head != ':' && *head != '\n') head++;
- pw->lengths[col] = head - pw->starts[col];
- col++;
- if (col == 7) {
- if (head == end) {
- pw->pos = head - pw->buffer.buffer;
- return 1;
- }
- if (*head == '\n') {
- head++;
- pw->pos = head - pw->buffer.buffer;
- return 1;
- }
- while (head != end && *head != '\n') head++;
- if (head != end) head++;
- col = 0;
- } else {
- if (head != end) {
- if (*head++ == '\n')
- col = 0;
- }
- }
- }
- pw->pos = head - pw->buffer.buffer;
- return 0;
-}
-
-int pw_get(struct tzplatform_context *context, struct heap *heap, struct pwget **items)
-{
- size_t user, home;
- int result;
- int i, n, s;
- struct pw *pw = &context->pw;
-
- for (n = 0 ; items[n] != NULL ; n++)
- items[n]->set = 0;
-
- result = oppw(context);
- if (result == 0) {
- s = n;
- while (s && rdpw(context)) {
- user = home = HNULL;
- for (i = 0 ; i < n ; i++) {
- if (!items[i]->set && is(items[i]->id, iuid)) {
- if (user == HNULL) {
- user = heap_strndup(heap,
- pw->starts[iname], pw->lengths[iname]);
- home = heap_strndup(heap,
- pw->starts[idir], pw->lengths[idir]);
- }
- items[i]->set = 1;
- items[i]->user = user;
- items[i]->home = home;
- s--;
- }
- }
- }
- clpw(context);
- }
- return result;
-}
-
-int pw_has_uid(struct tzplatform_context *context, uid_t uid)
-{
- struct pw *pw = &context->pw;
-
- if (oppw(context) == 0) {
- while (rdpw(context)) {
- if (pw->lengths[iuid] && (int)uid == atoi(pw->starts[iuid])) {
- clpw(context);
- return 1;
- }
- }
- clpw(context);
- }
- return 0;
-}
-
-int pw_get_uid(struct tzplatform_context *context, const char *name, uid_t *uid)
-{
- int result = oppw(context);
- if (result == 0) {
- while (rdpw(context)) {
- if (is(name, iname)) {
- *uid = (uid_t)atoi(context->pw.starts[iuid]);
- clpw(context);
- return 0;
- }
- }
- clpw(context);
- result = -1;
- errno = EEXIST;
- }
- return result;
-}
-
-int pw_get_gid(struct tzplatform_context *context, const char *name, gid_t *gid)
-{
- int result = oppw(context);
- if (result == 0) {
- while (rdpw(context)) {
- if (is(name, iname)) {
- *gid = (gid_t)atoi(context->pw.starts[igid]);
- clpw(context);
- return 0;
- }
- }
- clpw(context);
- result = -1;
- errno = EEXIST;
- }
- return result;
-}
-
-#else
-
-#include <pwd.h>
-
#define BUFSIZE 4096
-int pw_get(struct heap *heap, struct pwget **items)
+int pw_get(struct tzplatform_context *context, struct heap *heap, struct pwget **items)
{
char buffer[BUFSIZE];
struct passwd entry, *pe;
return 0;
}
-int pw_has_uid(uid_t uid)
+int pw_has_uid(struct tzplatform_context *context, uid_t uid)
{
char buffer[BUFSIZE];
struct passwd entry, *pe;
return !result && pe;
}
-int pw_get_uid(const char *name, uid_t *uid)
+int pw_get_uid(struct tzplatform_context *context, const char *name, uid_t *uid)
{
char buffer[BUFSIZE];
struct passwd entry, *pe;
return result;
}
-int pw_get_gid(const char *name, gid_t *gid)
+int pw_get_gid(struct tzplatform_context *context, const char *name, gid_t *gid)
{
char buffer[BUFSIZE];
struct passwd entry, *pe;
return result;
}
-#endif
-
#ifdef TEST_PASSWD
#include <stdio.h>
int main(int argc, char**argv)
return 0;
}
#endif
-
-