Digital Stronghold - Software Engineering Blog




February 19, 2006

Verify user in /etc/passwd

Filed under: Progressive Studies

This is a product of wonder.

In file verify.c

#include <stdio.h>
#include <string.h>
	
#define MAX_LENGTH	1024
	
int is_local(char *user)
{
        FILE *fd;
        char line[MAX_LENGTH];
        int local = 0;
	
        if(!(fd = fopen("/etc/passwd", "r"))) {
                puts("Can't read /etc/passwd, exiting.");
                exit(1);
        }
	
        while(fgets(line, MAX_LENGTH, fd) > 0) {
                if(!strncmp(line, user, strlen(user))) {
			local = 1;
			break;
                }
	}
	fclose(fd);
	return local;
}

Use at your own risk.

Disclaimer: Please refer to my first post ‘General notice’

Posted using Scott Yang’s mtsend.py python script. Thanks to Niel for his cool vim mappings.

1 Comment »

The URI to TrackBack this entry is: http://eradicus.blogsome.com/2006/02/19/verify-user-in-etcpasswd/trackback/

  1. Cool! There is a problem with the code though. If you have a user, say, “sophie”, but no user “sop”, and you tested for “sop” using this, I think you will get a 1. :) You ought to test with the first colon too.

    (And, of course, there’s always pwd.h.)

    Comment by Kristina Lim — March 27, 2006 @ 10:33 am

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Theme designed by Joset Anthony Zamora


Digital Stronghold

↑ Get Headline Animator