// {{{ user_set_password() /// Set the password for the specified username void user_set_password(user_t *o, char *username, volatile char* passwd) { FILE *f; struct spwd *sp = NULL; o->error[0]=0; sp = getspnam(username); if(!sp) { sstrncpy(o->error, "user_set_password() unknown user", USER_ERROR_SIZE); return; } /* salt */ struct timeval tv; static char salt[40]; salt[0] = '\0'; gettimeofday (&tv, (struct timezone *) 0); strcat(salt, l64a (tv.tv_usec)); strcat(salt, l64a (tv.tv_sec + getpid () + clock ())); if (strlen (salt) > 3 + 8) salt[11] = '\0'; /* shadow */ sp->sp_pwdp = (char*)crypt((const char*)passwd, salt); if(user_del_line(username, "/etc/shadow")!=0) { sstrncpy(o->error, "user_set_password() cannot modify /etc/shadow", USER_ERROR_SIZE); return; } f = fopen("/etc/shadow", "a+"); if(!f) { sstrncpy(o->error, "user_set_password(): cannot open /etc/shadow", USER_ERROR_SIZE); return; } if (putspent(sp, f) == -1) { sstrncpy(o->error, "user_add(): putspent() error", USER_ERROR_SIZE); return; } fclose(f); }
int do_test (int argc, char ** argv) { const struct a64l_test *at; long int l; const char *s; int status = 0; for (at = tests; at->base64 != NULL; ++at) { printf ("a64l (\"%s\")", at->base64); l = a64l (at->base64); if (l == at->value) puts ("\tOK"); else { printf ("\tBAD\n returns %ld, expected %ld\n", l, at->value); status = 1; } printf ("l64a (%ld)", at->value); s = l64a (at->value); if (strcmp (s, at->base64) == 0) puts ("\tOK"); else { printf ("\tBAD\n returns \"%s\", expected \"%s\"\n", s, at->base64); status = 1; } } return status ? EXIT_FAILURE : EXIT_SUCCESS; }
static /*@observer@*/const char *gensalt (size_t salt_size) { static char salt[32]; salt[0] = '\0'; assert (salt_size >= MIN_SALT_SIZE && salt_size <= MAX_SALT_SIZE); seedRNG (); strcat (salt, l64a (random())); do { strcat (salt, l64a (random())); } while (strlen (salt) < salt_size); salt[salt_size] = '\0'; return salt; }
extern Bool AuthUser( URL *auth, char *user, char *pass, char *other, char *id) { struct timeval tv; int fh; NETFILE *fp; Bool rc; char buff[SIZE_OTHER+1]; ENTER_FUNC; rc = FALSE; if ( strcmp(auth->protocol,"trust") == 0 ) { rc = TRUE; } else { if (user == NULL || pass == NULL) { return FALSE; } if ( !stricmp(auth->protocol,"glauth") ) { fh = ConnectIP_Socket(auth->port,SOCK_STREAM,auth->host); if ( fh > 0) { fp = SocketToNet(fh); SendString(fp,user); SendString(fp,pass); if ( ( rc = RecvBool(fp) ) ) { /* for protocol interchangebility; 'other' is not using*/ RecvnString(fp, sizeof(buff), buff); } CloseNet(fp); } else{ Warning("can not connect glauth server"); rc = FALSE; } } else if (!stricmp(auth->protocol,"api")) { rc = AuthAPI(user,pass,other,id); } else if (!stricmp(auth->protocol,"file")) { rc = AuthSingle(auth->file,user,pass,NULL); } else { rc = FALSE; } } if ( id != NULL ) { if ( rc ) { gettimeofday(&tv, (struct timezone *) 0); strcpy(id,crypt(user,l64a(tv.tv_sec + getpid() + clock()))); } else { *id = 0; } } LEAVE_FUNC; return (rc); }
void testValues() { f = 2; char* result; result = l64a(53L); //@ assert \valid(result); //@ assert f == 2; //@ assert vacuous: \false; }
/* * Return a different random string (a "call number" for a Call-ID in * SIP, RFC 3261) each time we are called. That string is good until the * next call, but may need to be copied if needed beyond that. */ char * SMnet::new_call_number() { char *p; long randnum; randnum = new_random_number(); p = l64a (randnum); if (!random_string) { random_string = new char[6+1]; } strncpy(random_string, p, 6); random_string[6] = '\0'; return random_string; }
/////////////////// // // // Main Function // // // /////////////////// int main(int argc, char *argv[]) { /* Declare local vars */ char *md; char pos; char ascii_encode[43]; int bin_data; /* Gets user input */ if (argc != 2) { fprintf(stderr, "Usage: %s <password>\n", argv[0]); return(1); }; /* Encrypts password */ SHA1(argv[1], strlen(argv[1]), md); /* encodes sha1 in base64 */ memset(ascii_encode, 0, 43); for(pos = 0; pos < 8; pos++) { if (md[pos*4] == 0) md[pos*4] = 1; memcpy(&bin_data, md+(pos*4), 4); memcpy(&ascii_encode[pos*6], l64a(htonl(bin_data)), 6); }; ascii_encode[42] = '\0'; for(pos = 0; pos < 41; pos++) if (ascii_encode[pos] == 0) ascii_encode[pos] = '0'; /* Displays results */ printf("Sha1:\t%s\n", ascii_encode); printf("Sha1 Size:\t%i\n", strlen(ascii_encode)); /* Ends program */ return(0); };
int main(int argc, char *argv[]) { char buffer, *char_data, out_data[6], string_name[100], *dot_ptr, *string_ptr; FILE *infile, *outfile; long byte_data, data; int byte_count, i, index; #if (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) int j; #endif /* (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) */ if(argc != 3 && argc != 4) { printf("Usage: uid2uidh infile outfile [string_prefix_name]\n"); } else { index = 1; if(infile = fopen( argv[index], "r")) { index++; if(outfile = fopen( argv[index], "w")) { index++; byte_count = 0; data = 0; if (argc == 4) { string_ptr = argv[3]; } else { strcpy(string_name, argv[1]); i=strlen(string_name); while ((i>0)&&(' '==string_name[i-1])) { i--; } while ((i>0)&&('/'==string_name[i-1])) { i--; } string_name[i]='\0'; while ((i>0)&&('/'!=string_name[i-1])) { i--; } string_ptr = string_name+i; if(dot_ptr = strchr(string_ptr, '.')) { *dot_ptr = 0; } } fprintf(outfile, "static char %s_uidh[] = \"", string_ptr); while(!feof(infile)) { if(1 == fread(&buffer, sizeof(char), 1, infile)) { #if defined (DEBUG_CODE) printf("%d\n", buffer); #endif /* defined (DEBUG_CODE) */ byte_data = buffer & 255; data += byte_data << (8 * byte_count); if(byte_count == 3) { if(!data) { out_data[0] = '.'; out_data[1] = '.'; out_data[2] = '.'; out_data[3] = '.'; out_data[4] = '.'; out_data[5] = '.'; } else { char_data = l64a(data); #if defined (DEBUG_CODE) printf("%c%c%c%c%c%c\n", char_data[0], char_data[1], char_data[2], char_data[3], char_data[4], char_data[5]); #endif /* defined (DEBUG_CODE) */ #if (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) if (!glibc_version_greater_than_2_2_4()) { for(i = 0 ; i < 6 ; i++) { if(char_data[i]) { out_data[5 - i] = char_data[i]; } else { for (j = 0 ; j < i ; j++) { out_data[j] = out_data[6 - i + j]; } for ( ; i < 6 ; i++) { out_data[i] = '.'; } } } } else { #endif /* (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) */ for( i = 0 ; i < 6 ; i++) { if(char_data[i]) { out_data[i] = char_data[i]; } else { for ( ; i < 6 ; i++) { out_data[i] = '.'; } } } #if (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) } #endif /* (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) */ } fwrite(out_data, sizeof(char), 6, outfile); #if defined (DEBUG_CODE) printf("%c%c%c%c%c%c\n", out_data[0], out_data[1], out_data[2], out_data[3], out_data[4], out_data[5]); #endif /* defined (DEBUG_CODE) */ data = 0; byte_count = 0; } else { byte_count++; } } } fprintf(outfile, "\";\n"); fclose (outfile); } else { fprintf(stderr,"uid2uidh. Unable to open output file %s\n", argv[index]); } fclose (infile); } else { fprintf(stderr,"uid2uidh. Unable to open input file %s\n", argv[index]); } } return(0); }
int main (int argc, char **argv) { const struct passwd *pw; struct passwd pwent; const struct spwd *spwd; #ifdef ATT_AGE char newage[5]; #endif char *Prog = argv[0]; setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); if (!spw_file_present ()) /* shadow not installed, do nothing */ exit (0); if (!pw_lock ()) { fprintf (stderr, _("%s: can't lock passwd file\n"), Prog); fail_exit (5); } passwd_locked++; if (!pw_open (O_RDWR)) { fprintf (stderr, _("%s: can't open passwd file\n"), Prog); fail_exit (1); } if (!spw_lock ()) { fprintf (stderr, _("%s: can't open shadow file\n"), Prog); fail_exit (5); } shadow_locked++; if (!spw_open (O_RDWR)) { fprintf (stderr, _("%s: can't open shadow file\n"), Prog); fail_exit (1); } pw_rewind (); while ((pw = pw_next ())) { if (!(spwd = spw_locate (pw->pw_name))) continue; pwent = *pw; /* * Update password if non-shadow is "x". */ if (strcmp (pw->pw_passwd, SHADOW_PASSWD_STRING) == 0) pwent.pw_passwd = spwd->sp_pwdp; /* * Password aging works differently in the two different * systems. With shadow password files you apparently must * have some aging information. The maxweeks or minweeks * may not map exactly. In pwconv we set max == 10000, * which is about 30 years. Here we have to undo that * kludge. So, if maxdays == 10000, no aging information is * put into the new file. Otherwise, the days are converted * to weeks and so on. */ #ifdef ATT_AGE if (spwd->sp_max > (63 * WEEK / SCALE) && spwd->sp_max < 10000) spwd->sp_max = (63 * WEEK / SCALE); /* 10000 is infinity */ if (spwd->sp_min >= 0 && spwd->sp_min <= 63 * 7 && spwd->sp_max >= 0 && spwd->sp_max <= 63 * 7) { if (spwd->sp_lstchg == -1) spwd->sp_lstchg = 0; spwd->sp_max /= WEEK / SCALE; /* turn it into weeks */ spwd->sp_min /= WEEK / SCALE; spwd->sp_lstchg /= WEEK / SCALE; strncpy (newage, l64a (spwd->sp_lstchg * (64L * 64L) + spwd->sp_min * (64L) + spwd->sp_max), 5); pwent.pw_age = newage; } else pwent.pw_age = ""; #endif /* ATT_AGE */ if (!pw_update (&pwent)) { fprintf (stderr, _("%s: can't update entry for user %s\n"), Prog, pwent.pw_name); fail_exit (3); } } if (!spw_close ()) { fprintf (stderr, _("%s: can't update shadow password file\n"), Prog); fail_exit (3); } if (!pw_close ()) { fprintf (stderr, _("%s: can't update password file\n"), Prog); fail_exit (3); } if (unlink (SHADOW) != 0) { fprintf (stderr, _("%s: can't delete shadow password file\n"), Prog); fail_exit (3); } spw_unlock (); pw_unlock (); return 0; }
void runFailure() { l64a(-3L); }
void runSuccess() { l64a(5L); }
// {{{ user_add() /// Create a valid user account void user_add(user_t *o, char *username, volatile char *passwd) { o->error[0]=0; struct passwd p; struct passwd *pw; struct spwd sp; FILE *f; int min = 1000; int max = 65000; char home[256]; snprintf(home, sizeof(home), "/home/%s", username); p.pw_name = (char *)username; p.pw_passwd = "x"; p.pw_uid = USER_DEFAULT_ID; p.pw_gid = USER_GROUP_ID; p.pw_gecos = "OpenDomo User"; p.pw_dir = home; p.pw_shell = "/bin/sh"; f = fopen("/etc/passwd", "r"); /* check user and get valid id */ while ((pw = fgetpwent(f))) { if (strcmp(pw->pw_name, p.pw_name) == 0) { sstrncpy(o->error, "user_add(): user exists", USER_ERROR_SIZE); return; } if ((pw->pw_uid >= p.pw_uid) && (pw->pw_uid < max) && (pw->pw_uid >= min)) { p.pw_uid = pw->pw_uid + 1; } } fclose(f); f = fopen("/etc/passwd", "a+"); if(!f) { sstrncpy(o->error, "user_add(): cannot open /etc/passwd",USER_ERROR_SIZE); return; } /* add to passwd */ if (putpwent(&p, f) == -1) { sstrncpy(o->error, "user_add(): putpwent() error", USER_ERROR_SIZE); return; } fclose(f); /* salt */ struct timeval tv; static char salt[40]; salt[0] = '\0'; gettimeofday (&tv, (struct timezone *) 0); strcat(salt, l64a (tv.tv_usec)); strcat(salt, l64a (tv.tv_sec + getpid () + clock ())); if (strlen (salt) > 3 + 8) salt[11] = '\0'; /* shadow */ sp.sp_namp = p.pw_name; sp.sp_pwdp = (char*)crypt((const char*)passwd, salt); sp.sp_min = 0; sp.sp_max = (10000L * DAY) / SCALE; sp.sp_lstchg = time((time_t *) 0) / SCALE; sp.sp_warn = -1; sp.sp_expire = -1; sp.sp_inact = -1; sp.sp_flag = -1; /* add to shadow */ f = fopen("/etc/shadow", "a+"); if(!f) { sstrncpy(o->error, "user_add(): cannot open /etc/shadow",USER_ERROR_SIZE); return; } if (putspent(&sp, f) == -1) { sstrncpy(o->error, "user_add(): putspent() error",USER_ERROR_SIZE); return; } fclose(f); /* Create home */ mkdir(home, 0700); chown(home, p.pw_uid, USER_GROUP_ID); }
static size_t write_base64_data(FILE *outfile, int byte_count, long data) /******************************************************************************* LAST MODIFIED : 20 June 2003 DESCRIPTION : ==============================================================================*/ { char *char_data, bytes_out, out_data[6]; size_t bytes_written, i; #if (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) int j; #endif /* (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) */ switch(byte_count) { case 1: { bytes_out = 2; } break; case 2: { bytes_out = 3; } break; case 3: { bytes_out = 5; } break; case 4: { bytes_out = 6; } break; default: { printf("write_base64_data: Unsupported byte_count %d\n", byte_count); exit(1); } break; } if(!data) { for (i = 0 ; i < bytes_out ; i++) { out_data[i] = '.'; } } else { char_data = l64a(data); #if defined (CMISS_DEBUG) printf("'%c%c%c%c%c%c'\n", char_data[0], char_data[1], char_data[2], char_data[3], char_data[4], char_data[5]); #endif /* defined (CMISS_DEBUG) */ #if (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) if (!glibc_version_greater_than_2_2_4()) { for(i = 0 ; i < bytes_out ; i++) { if(char_data[i]) { out_data[bytes_out - 1 - i] = char_data[i]; } else { for (j = 0 ; j < i ; j++) { out_data[j] = out_data[bytes_out - i + j]; } for ( ; i < bytes_out ; i++) { out_data[i] = '.'; } } } } else { #endif /* (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) */ for( i = 0 ; i < bytes_out ; i++) { if(char_data[i]) { out_data[i] = char_data[i]; } else { for ( ; i < bytes_out ; i++) { out_data[i] = '.'; } } } #if (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) } #endif /* (defined (BYTE_ORDER)) && (1234==BYTE_ORDER) */ } bytes_written = fwrite(out_data, sizeof(char), bytes_out, outfile); #if defined (CMISS_DEBUG) printf("'%c%c%c%c%c%c'\n", out_data[0], out_data[1], out_data[2], out_data[3], out_data[4], out_data[5]); #endif /* defined (CMISS_DEBUG) */ return (bytes_written); }
int main () { struct passwd *pw; struct passwd *sgetpwent (); FILE *pwd; FILE *npwd; struct spwd *spwd; int fd; char newage[5]; if (! (pwd = fopen (PWDFILE, "r"))) { perror (PWDFILE); return (1); } unlink ("npasswd"); if ((fd = open ("npasswd", O_WRONLY|O_CREAT|O_EXCL, 0600)) < 0 || ! (npwd = fdopen (fd, "w"))) { perror ("npasswd"); return (1); } while (fgets (buf, BUFSIZ, pwd) == buf) { buf[strlen (buf) - 1] = '\0'; /* remove '\n' character */ if (buf[0] == '#') { /* comment line */ (void) fprintf (npwd, "%s\n", buf); continue; } if (! (pw = sgetpwent (buf))) { /* copy bad lines verbatim */ (void) fprintf (npwd, "%s\n", buf); continue; } setspent (); /* rewind shadow file */ if (! (spwd = getspnam (pw->pw_name))) { (void) fprintf (npwd, "%s\n", buf); continue; } pw->pw_passwd = spwd->sp_pwdp; /* * Password aging works differently in the two different systems. * With shadow password files you apparently must have some aging * information. The maxweeks or minweeks may not map exactly. * In pwconv we set max == 10000, which is about 30 years. Here * we have to undo that kludge. So, if maxdays == 10000, no aging * information is put into the new file. Otherwise, the days are * converted to weeks and so on. */ #ifdef ATT_AGE if (spwd->sp_max > (63*WEEK) && spwd->sp_max < 10000) spwd->sp_max = (63*WEEK); /* 10000 is infinity */ if (spwd->sp_min >= 0 && spwd->sp_min <= 63*7 && spwd->sp_max >= 0 && spwd->sp_max <= 63*7) { if (spwd->sp_lstchg == -1) spwd->sp_lstchg = 0; spwd->sp_max /= WEEK; /* turn it into weeks */ spwd->sp_min /= WEEK; spwd->sp_lstchg /= WEEK; strncpy (newage, l64a (spwd->sp_lstchg * (64L*64L) + spwd->sp_min * (64L) + spwd->sp_max), 5); pw->pw_age = newage; } else pw->pw_age = ""; #endif /* ATT_AGE */ if (putpwent (pw, npwd)) { perror ("pwunconv: write error"); exit (1); } } endspent (); if (ferror (npwd)) { perror ("pwunconv"); (void) unlink ("npasswd"); } (void) fclose (npwd); (void) fclose (pwd); return (0); }