<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: passwd c program in Operating System - Linux</title>
    <link>https://community.hpe.com/t5/operating-system-linux/passwd-c-program/m-p/3609255#M104273</link>
    <description>Try: &lt;A href="http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/npasswd-1.2.4/" target="_blank"&gt;http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/npasswd-1.2.4/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;TIOCNOTTY is a linux or bsd thing. Do a google on &lt;BR /&gt;&lt;BR /&gt;+TIOCNOTTY +iotcl +hp-ux&lt;BR /&gt;&lt;BR /&gt;live free or die&lt;BR /&gt;harry d brown jr</description>
    <pubDate>Tue, 23 Aug 2005 06:36:18 GMT</pubDate>
    <dc:creator>harry d brown jr</dc:creator>
    <dc:date>2005-08-23T06:36:18Z</dc:date>
    <item>
      <title>passwd c program</title>
      <link>https://community.hpe.com/t5/operating-system-linux/passwd-c-program/m-p/3609254#M104272</link>
      <description>I wanted to change a users password inside a C program. I tried the following source code i found on the net but HPUX doesnt use ioctl(TIOCNOTTY). Is there any other way i can do it. I work on HPUX 11i with PA Risc. I would like to avoid altering the password file.&lt;BR /&gt;&lt;BR /&gt;#include &lt;FCNTL.H&gt;&lt;BR /&gt;#include &lt;SYS&gt;&lt;BR /&gt;#include &lt;PWD.H&gt;&lt;BR /&gt;#include &lt;ERRNO.H&gt;&lt;BR /&gt;#include &lt;STDIO.H&gt;&lt;BR /&gt;#include &lt;UNISTD.H&gt;&lt;BR /&gt;#include &lt;STRING.H&gt;&lt;BR /&gt;&lt;BR /&gt;#define PASSWD "passwd"&lt;BR /&gt;#define SMBPASSWD "/usr/local/bin/smbpasswd"&lt;BR /&gt;#define PROMPT1 "Type a new password: "&lt;BR /&gt;#define PROMPT2 "Type the same password again: "&lt;BR /&gt;#define MINPWLEN 6&lt;BR /&gt;#define STRLEN 1024&lt;BR /&gt;&lt;BR /&gt;int change(char *program, char *user, char *pwd, FILE *mystderr);&lt;BR /&gt;&lt;BR /&gt;int main (int argc,char **argv)&lt;BR /&gt;{&lt;BR /&gt; int fd;&lt;BR /&gt; struct passwd *pwentry;&lt;BR /&gt; char name[STRLEN];&lt;BR /&gt; char newpw[STRLEN];&lt;BR /&gt; int reallyroot = 0;&lt;BR /&gt; char *cp;&lt;BR /&gt; FILE *mystderr;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt; /* do we have the appropriate permissions? */&lt;BR /&gt; if (geteuid() != 0) &lt;BR /&gt; {&lt;BR /&gt;  fprintf(stderr, "This program cannot run unless it is SUID-root, exiting...\n");&lt;BR /&gt;  exit(1);&lt;BR /&gt; }&lt;BR /&gt; if (getuid() == 0)&lt;BR /&gt;  reallyroot = 1;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt; /* get the appropriate username */&lt;BR /&gt; if (argc &amp;gt; 1) &lt;BR /&gt; {&lt;BR /&gt;  if (reallyroot) &lt;BR /&gt;  {&lt;BR /&gt;   /* if root, we can specify a username */&lt;BR /&gt;   strncpy(name, *++argv, STRLEN);&lt;BR /&gt;  } &lt;BR /&gt;  else &lt;BR /&gt;  {&lt;BR /&gt;   fprintf(stderr, "Only the root user can specify a name, exiting...\n");&lt;BR /&gt;   exit(1);&lt;BR /&gt;  }&lt;BR /&gt; } &lt;BR /&gt; else &lt;BR /&gt; {&lt;BR /&gt;  /* pick up the current user's username */&lt;BR /&gt;  if ((pwentry = getpwuid(getuid())) == NULL) &lt;BR /&gt;  {&lt;BR /&gt;   fprintf(stderr, "Failed getting name entry for UID=%d, exiting...\n",getuid());&lt;BR /&gt;   exit(1);&lt;BR /&gt;  }&lt;BR /&gt;  strncpy(name, pwentry-&amp;gt;pw_name, STRLEN);&lt;BR /&gt; }&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt; /* get a password and clean any cr/lf stuff */&lt;BR /&gt; if (isatty(0)) &lt;BR /&gt; {&lt;BR /&gt;  /* interactive, so use a no-echo prompt twice */&lt;BR /&gt;  fprintf(stderr, "Changing password for user '%s'\n", name);&lt;BR /&gt;  cp = getpass(PROMPT1);&lt;BR /&gt;  strncpy(newpw, cp, STRLEN);&lt;BR /&gt;  cp = getpass(PROMPT2);&lt;BR /&gt;  if (strcmp(newpw, cp)) {&lt;BR /&gt;  fprintf(stderr, "The two versions don't match, exiting...\n");&lt;BR /&gt;  exit(1);&lt;BR /&gt; }&lt;BR /&gt; }&lt;BR /&gt; else &lt;BR /&gt; {&lt;BR /&gt;  /* noninteractive, so just get it from stdin */&lt;BR /&gt;  if (read(0, newpw, STRLEN) &amp;lt;= 0) &lt;BR /&gt;  {&lt;BR /&gt;   fprintf(stderr, "Failed to read a new password, exiting...\n");&lt;BR /&gt;   exit(1);&lt;BR /&gt;  }&lt;BR /&gt; }&lt;BR /&gt; cp=newpw;&lt;BR /&gt; if( *cp!='\n' &amp;amp;&amp;amp; *cp!='\r' &amp;amp;&amp;amp; cp-newpw&amp;lt;= 0) &lt;BR /&gt; {&lt;BR /&gt;  fprintf(stderr, "No password entered, exiting...\n");&lt;BR /&gt;  exit(1);&lt;BR /&gt; }&lt;BR /&gt; if ( ! reallyroot) &lt;BR /&gt; {&lt;BR /&gt;  if (strlen(newpw) &amp;lt; MINPWLEN) &lt;BR /&gt;  {&lt;BR /&gt;   fprintf(stderr, "Password must be at least %d characters long, exiting...\n", MINPWLEN);&lt;BR /&gt;   exit(1);&lt;BR /&gt;  }&lt;BR /&gt; }&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt; /* get a private stderr, then close stderr/stdout to silence pwd programs */&lt;BR /&gt; if ((fd = dup(2)) &amp;lt; 0) &lt;BR /&gt; {&lt;BR /&gt;  fprintf(stderr, "Strange! Couldn't dup error-output fd, exiting...\n");&lt;BR /&gt;  exit(1);&lt;BR /&gt; }&lt;BR /&gt; if ((mystderr = fdopen(fd, "w")) == NULL) &lt;BR /&gt; {&lt;BR /&gt;  fprintf(stderr, "Strange! Couldn't fdopen new stderr fd, exiting...\n");&lt;BR /&gt;  exit(1);&lt;BR /&gt; }&lt;BR /&gt; close(1);&lt;BR /&gt; close(2);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt; /* detach from controlling tty to get password programs to read stdin */&lt;BR /&gt; if ((fd = open("/dev/tty", O_RDWR | O_NOCTTY)) &amp;gt;= 0) &lt;BR /&gt; {&lt;BR /&gt;  if (ioctl(fd, TIOCNOTTY) &amp;lt; 0) &lt;BR /&gt;  {&lt;BR /&gt;   fprintf(mystderr, "Failed to detach from /dev/tty: %s, exiting...\n",&lt;BR /&gt;   strerror(errno));&lt;BR /&gt;   exit(1);&lt;BR /&gt;  }&lt;BR /&gt;  close(fd);&lt;BR /&gt; }&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt; /* shuffle UIDs for permissions - we know we are running SUID-root */&lt;BR /&gt; if (setuid(geteuid()) != 0) &lt;BR /&gt; {&lt;BR /&gt;  fprintf(stderr, "Failed to properly set UID, exiting...\n");&lt;BR /&gt;  exit(1);&lt;BR /&gt; }&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt; /* change the Unix password */&lt;BR /&gt; if (isatty(0))&lt;BR /&gt;  fprintf(mystderr, "Changing Unix password...\n");&lt;BR /&gt; if ( ! change(PASSWD, name, newpw, mystderr))&lt;BR /&gt;  exit(1);&lt;BR /&gt; if (isatty(0))&lt;BR /&gt;  fprintf(mystderr, "\tSuccessfully changed Unix password.\n");&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt; /* change the SMB password */&lt;BR /&gt; if (isatty(0))&lt;BR /&gt;  fprintf(mystderr, "Changing SMB/Windows password...\n");&lt;BR /&gt; if ( ! change(SMBPASSWD, name, newpw, mystderr))&lt;BR /&gt;  exit(1);&lt;BR /&gt; if (isatty(0))&lt;BR /&gt;  fprintf(mystderr, "\tSuccessfully changed SMB/Windows password.\n");&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt; exit(0);&lt;BR /&gt;}&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;int change(char *program,char *user,char *pwd,FILE *mystderr)&lt;BR /&gt;{&lt;BR /&gt; char cmd[STRLEN];&lt;BR /&gt; FILE *cmdpipe;&lt;BR /&gt; int status;&lt;BR /&gt;&lt;BR /&gt; /* open a pipe to and then feed the password program, slowly */&lt;BR /&gt; strncpy(cmd, program, STRLEN);&lt;BR /&gt; strncat(cmd, " ", STRLEN - 1);&lt;BR /&gt; strncat(cmd, user, STRLEN - strlen(cmd));&lt;BR /&gt; if ((cmdpipe = popen(cmd, "w")) == NULL) &lt;BR /&gt; {&lt;BR /&gt;  fprintf(mystderr, "Failed to open pipe to '%s', exiting...\n", cmd);&lt;BR /&gt;  return 0;&lt;BR /&gt; }&lt;BR /&gt; sleep(3);&lt;BR /&gt; fprintf(cmdpipe, "%s\n", pwd); fflush(cmdpipe); sleep(2);&lt;BR /&gt; fprintf(cmdpipe, "%s\n", pwd); fflush(cmdpipe); sleep(2);&lt;BR /&gt; if ((status = pclose(cmdpipe)) != 0) &lt;BR /&gt; {&lt;BR /&gt;  fprintf(mystderr, "Program '%s' returned error code %d, exiting...\n",cmd, status);&lt;BR /&gt;  return 0;&lt;BR /&gt; }&lt;BR /&gt;&lt;BR /&gt; return 1;&lt;BR /&gt;} &lt;BR /&gt;&lt;/STRING.H&gt;&lt;/UNISTD.H&gt;&lt;/STDIO.H&gt;&lt;/ERRNO.H&gt;&lt;/PWD.H&gt;&lt;/SYS&gt;&lt;/FCNTL.H&gt;</description>
      <pubDate>Tue, 23 Aug 2005 02:19:14 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/passwd-c-program/m-p/3609254#M104272</guid>
      <dc:creator>Andreas Tsamis</dc:creator>
      <dc:date>2005-08-23T02:19:14Z</dc:date>
    </item>
    <item>
      <title>Re: passwd c program</title>
      <link>https://community.hpe.com/t5/operating-system-linux/passwd-c-program/m-p/3609255#M104273</link>
      <description>Try: &lt;A href="http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/npasswd-1.2.4/" target="_blank"&gt;http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/npasswd-1.2.4/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;TIOCNOTTY is a linux or bsd thing. Do a google on &lt;BR /&gt;&lt;BR /&gt;+TIOCNOTTY +iotcl +hp-ux&lt;BR /&gt;&lt;BR /&gt;live free or die&lt;BR /&gt;harry d brown jr</description>
      <pubDate>Tue, 23 Aug 2005 06:36:18 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-linux/passwd-c-program/m-p/3609255#M104273</guid>
      <dc:creator>harry d brown jr</dc:creator>
      <dc:date>2005-08-23T06:36:18Z</dc:date>
    </item>
  </channel>
</rss>

