1

Is there any C API for validating user name ?

I am taking input of user_name which might not be present at that point in time. Since the user name needs to follow POSIX rules, is there any C API which can do the validation?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Arpit
  • 4,259
  • 10
  • 38
  • 43
  • 1
    See also the following question: http://stackoverflow.com/questions/6949667/what-are-the-real-rules-for-linux-usernames-on-centos-6-and-rhel-6 – Rory Hunter Apr 05 '12 at 10:23

1 Answers1

1

I presume you are trying to figure out whether a user name exists on the system you are running on. For that, you are looking for getwpent()/getpwnam() -- check the manual page for details.

Although these return more information than you want, getpwnam() will also (easily) tell you if there is or isn't a password file entry corresponding to a given user name.

(If you are simply looking to make sure a user name contains only valid characters, just make sure it only contains [A-Za-z0-9_]+)

Perry
  • 4,363
  • 1
  • 17
  • 20
  • The user name should probably not be pure numeric; at the least, you can expect confusion if the user name looks like a number (doubly so if the UID is not the same number as the name — yes, you can do it; no, you should not do it!). – Jonathan Leffler Oct 24 '13 at 00:46
  • Yes, that's true. I suppose the right regexp is [A-Za-z_][A-Za-z_0-9]* – Perry Nov 07 '13 at 15:51